🔨 Fix openApi

This commit is contained in:
Sébastien Clément 2022-05-25 14:53:42 +00:00 committed by GitHub
parent 217352cf66
commit 2f96885714
6 changed files with 32 additions and 14 deletions

View File

@ -11,6 +11,8 @@ import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.security.SecurityRequirements;
@RestController
@RequestMapping("/api/v2/auth")
@CrossOrigin(origins = "*", maxAge = 3600)
@ -25,6 +27,7 @@ public class AuthController {
}
@PostMapping("/discord")
@SecurityRequirements(value = {})
public JwtResponse loginDiscord(@Validated @RequestBody Login login) {
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(login.redirectUri(), login.code())

View File

@ -7,10 +7,13 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Hidden;
@RestController
@RequestMapping("/api/v2")
@CrossOrigin(origins = "*", maxAge = 3600)
@Hidden
public class CrossOptionController {
/**

View File

@ -2,6 +2,7 @@ package net.Broken.Api.Controllers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.security.SecurityRequirements;
import net.Broken.DB.Entity.UserEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.CrossOrigin;
@ -12,12 +13,10 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v2/hello")
@CrossOrigin(origins = "*", maxAge = 3600)
public class HelloController {
@GetMapping("world")
@Operation(security = { @SecurityRequirement(name = "jwt") })
public String helloWorld(Authentication authentication){
UserEntity principal = (UserEntity) authentication.getPrincipal();
return "Hello " + principal.getUsername();

View File

@ -1,18 +1,33 @@
package net.Broken.Api.OpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.context.annotation.Bean;
@Configuration
public class OpenApiConfig {
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI().components(new Components()
.addSecuritySchemes("jwt",
new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT").name("JWT")))
.addSecurityItem(new SecurityRequirement().addList("jwt"));
}
}
@Bean
public OpenAPI customOpenAPI() {
final String securitySchemeName = "JWT";
// final String apiTitle = String.format("%s API", StringUtils.capitalize(moduleName));
return new OpenAPI()
.addSecurityItem(new SecurityRequirement().addList(securitySchemeName))
.components(
new Components()
.addSecuritySchemes(securitySchemeName,
new SecurityScheme()
.name(securitySchemeName)
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT")
)
);
}
}

View File

@ -27,6 +27,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
.authorizeRequests()
.antMatchers("/api/v2/auth/**").permitAll()
.antMatchers("/swagger-ui/**").permitAll()
.antMatchers("/swagger-ui.html").permitAll()
.antMatchers("/v3/api-docs/**").permitAll()
.anyRequest().denyAll();

View File

@ -5,7 +5,6 @@ import io.jsonwebtoken.*;
import io.jsonwebtoken.security.Keys;
import net.Broken.DB.Entity.UserEntity;
import net.Broken.DB.Repository.UserRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.security.Key;
@ -16,8 +15,6 @@ import java.util.UUID;
@Service
public class JwtService {
@Value("${security.jwt.secret}")
private String jwtSecret;
private final Key jwtKey;