From 61e95da77887853d8531107fbecfa8051401ad3b Mon Sep 17 00:00:00 2001 From: ivamp Date: Sun, 17 Nov 2024 06:56:24 +0800 Subject: [PATCH] update --- .../example/demo/config/SecurityConfig.java | 18 +-------- .../com/example/demo/config/SpringDoc.java | 39 +++++++------------ .../CustomAuthenticationEntryPoint.java | 21 ---------- .../com/example/demo/pojo/ResponseAdvice.java | 24 +++++------- src/main/resources/application.yml | 21 ++++++---- 5 files changed, 38 insertions(+), 85 deletions(-) delete mode 100644 src/main/java/com/example/demo/exception/CustomAuthenticationEntryPoint.java diff --git a/src/main/java/com/example/demo/config/SecurityConfig.java b/src/main/java/com/example/demo/config/SecurityConfig.java index 64836cb..bd52492 100644 --- a/src/main/java/com/example/demo/config/SecurityConfig.java +++ b/src/main/java/com/example/demo/config/SecurityConfig.java @@ -1,9 +1,5 @@ package com.example.demo.config; -import com.example.demo.exception.CustomAuthenticationEntryPoint; -import lombok.Getter; -import lombok.Setter; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -25,16 +21,6 @@ import java.util.*; @Configuration @EnableWebSecurity public class SecurityConfig { - @Getter - @Setter - private CustomAuthenticationEntryPoint customAuthenticationEntryPoint; - - @Autowired - public SecurityConfig(CustomAuthenticationEntryPoint customAuthenticationEntryPoint) { - this.customAuthenticationEntryPoint = customAuthenticationEntryPoint; - } - - private static final String[] WHITE_LIST = {"/swagger-ui/**", "/v3/api-docs/**", "/swagger/**"}; // @Value("${spring.security.oauth2.resourceserver.jwt.issuer-uri}") @@ -47,9 +33,7 @@ public class SecurityConfig { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { - http.exceptionHandling( - exception -> exception.authenticationEntryPoint(customAuthenticationEntryPoint)) - .authorizeHttpRequests(authorizeRequests -> + http.authorizeHttpRequests(authorizeRequests -> authorizeRequests.requestMatchers(WHITE_LIST).permitAll().anyRequest().authenticated() ) .sessionManagement(sessionManagement -> diff --git a/src/main/java/com/example/demo/config/SpringDoc.java b/src/main/java/com/example/demo/config/SpringDoc.java index bf40b32..408b822 100644 --- a/src/main/java/com/example/demo/config/SpringDoc.java +++ b/src/main/java/com/example/demo/config/SpringDoc.java @@ -1,32 +1,23 @@ package com.example.demo.config; -import io.swagger.v3.oas.models.OpenAPI; -import io.swagger.v3.oas.models.info.Info; -import org.springframework.context.annotation.Bean; +import io.swagger.v3.oas.annotations.OpenAPIDefinition; +import io.swagger.v3.oas.annotations.enums.SecuritySchemeType; +import io.swagger.v3.oas.annotations.info.Info; +import io.swagger.v3.oas.annotations.security.OAuthFlow; +import io.swagger.v3.oas.annotations.security.OAuthFlows; +import io.swagger.v3.oas.annotations.security.OAuthScope; +import io.swagger.v3.oas.annotations.security.SecurityScheme; import org.springframework.context.annotation.Configuration; @Configuration +@OpenAPIDefinition(info = @Info(title = "Foo API", + description = "Foo description", version = "v1")) +@SecurityScheme(name = "security_auth", type = SecuritySchemeType.OAUTH2, + flows = @OAuthFlows(authorizationCode = @OAuthFlow( + authorizationUrl = "${springdoc.oAuthFlow.authorizationUrl}" + , tokenUrl = "${springdoc.oAuthFlow.tokenUrl}", scopes = { + @OAuthScope(name = "springdoc.read", description = "read scope"), + @OAuthScope(name = "springdoc.write", description = "write scope") }))) public class SpringDoc { - @Bean - public OpenAPI openAPI() { - return new OpenAPI().info(new Info() - .title("Demo API") - ); - } -// -// @Bean -// public GroupedOpenApi publicApi() { -// return GroupedOpenApi.builder() -// .group("api") -// .pathsToMatch("/api/**") -// .build(); -// } -// @Bean -// public GroupedOpenApi adminApi() { -// return GroupedOpenApi.builder() -// .group("admin") -// .pathsToMatch("/admin/**") -// .build(); -// } } diff --git a/src/main/java/com/example/demo/exception/CustomAuthenticationEntryPoint.java b/src/main/java/com/example/demo/exception/CustomAuthenticationEntryPoint.java deleted file mode 100644 index 2fd693a..0000000 --- a/src/main/java/com/example/demo/exception/CustomAuthenticationEntryPoint.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.example.demo.exception; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.security.core.AuthenticationException; -import org.springframework.security.web.AuthenticationEntryPoint; -import org.springframework.stereotype.Component; - -import java.io.IOException; - -@Component -public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint { - @Override - public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException { - response.setStatus(HttpStatus.UNAUTHORIZED.value()); - response.setHeader(HttpHeaders.WWW_AUTHENTICATE, "Bearer error=\"invalid_token\", error_description=\"" + authException.getMessage() + "\""); - response.getWriter().write("Unauthorized: " + authException.getMessage()); - } -} diff --git a/src/main/java/com/example/demo/pojo/ResponseAdvice.java b/src/main/java/com/example/demo/pojo/ResponseAdvice.java index d4c2f8f..edb3f70 100644 --- a/src/main/java/com/example/demo/pojo/ResponseAdvice.java +++ b/src/main/java/com/example/demo/pojo/ResponseAdvice.java @@ -2,6 +2,7 @@ package com.example.demo.pojo; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.Getter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.MethodParameter; import org.springframework.http.MediaType; @@ -11,6 +12,7 @@ import org.springframework.http.server.ServerHttpResponse; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; +@Getter @RestControllerAdvice public class ResponseAdvice implements ResponseBodyAdvice { private final ObjectMapper objectMapper; @@ -22,27 +24,19 @@ public class ResponseAdvice implements ResponseBodyAdvice { @Override public boolean supports(MethodParameter returnType, Class> converterType) { - return false; + return true; } @Override public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { - try { - if (body instanceof String) { - return objectMapper.writeValueAsString(ResponseMessage.success(body)); - } - if (body instanceof ResponseMessage) { - return body; - } - return ResponseMessage.success(body); - } catch (JsonProcessingException e) { - // 如果序列化失败,直接返回原始的 body + if (body instanceof String) { + return body; +// return objectMapper.writeValueAsString(ResponseMessage.success(body)); + } + if (body instanceof ResponseMessage) { return body; } - } - - public ObjectMapper getObjectMapper() { - return objectMapper; + return ResponseMessage.success(body); } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 4c06cfa..25e510a 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -18,13 +18,6 @@ spring: flyway: baseline-on-migrate: false # 阻止启动时执行 flyway locations: classpath:migrations - doc: - api-docs: - enabled: true - path: /v3/api-docs - swagger-ui: - enabled: true - path: /swagger shell: interactive: enabled: false @@ -38,4 +31,16 @@ spring: time-zone: PRC server: port: 8088 - +springdoc: + swagger-ui: + # swagger-ui地址 + path: /swagger/index.html + enabled: true + # 配置本地访问页面(注释) + config-url: /swagger/api-docs/swagger-config + # 取消默认Swagger访问页面 + disable-swagger-default-url: true + # 修复Failed to load remote configuration. + url: /swagger/api-docs + api-docs: + path: /swagger/api-docs \ No newline at end of file