update
This commit is contained in:
parent
d5dc891d30
commit
61e95da778
@ -1,9 +1,5 @@
|
|||||||
package com.example.demo.config;
|
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.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@ -25,16 +21,6 @@ import java.util.*;
|
|||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
public class SecurityConfig {
|
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/**"};
|
private static final String[] WHITE_LIST = {"/swagger-ui/**", "/v3/api-docs/**", "/swagger/**"};
|
||||||
|
|
||||||
// @Value("${spring.security.oauth2.resourceserver.jwt.issuer-uri}")
|
// @Value("${spring.security.oauth2.resourceserver.jwt.issuer-uri}")
|
||||||
@ -47,9 +33,7 @@ public class SecurityConfig {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||||
http.exceptionHandling(
|
http.authorizeHttpRequests(authorizeRequests ->
|
||||||
exception -> exception.authenticationEntryPoint(customAuthenticationEntryPoint))
|
|
||||||
.authorizeHttpRequests(authorizeRequests ->
|
|
||||||
authorizeRequests.requestMatchers(WHITE_LIST).permitAll().anyRequest().authenticated()
|
authorizeRequests.requestMatchers(WHITE_LIST).permitAll().anyRequest().authenticated()
|
||||||
)
|
)
|
||||||
.sessionManagement(sessionManagement ->
|
.sessionManagement(sessionManagement ->
|
||||||
|
@ -1,32 +1,23 @@
|
|||||||
package com.example.demo.config;
|
package com.example.demo.config;
|
||||||
|
|
||||||
import io.swagger.v3.oas.models.OpenAPI;
|
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
|
||||||
import io.swagger.v3.oas.models.info.Info;
|
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
|
||||||
import org.springframework.context.annotation.Bean;
|
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;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
@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 {
|
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();
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
@ -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());
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,6 +2,7 @@ package com.example.demo.pojo;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import lombok.Getter;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
import org.springframework.http.MediaType;
|
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.bind.annotation.RestControllerAdvice;
|
||||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
||||||
|
|
||||||
|
@Getter
|
||||||
@RestControllerAdvice
|
@RestControllerAdvice
|
||||||
public class ResponseAdvice implements ResponseBodyAdvice<Object> {
|
public class ResponseAdvice implements ResponseBodyAdvice<Object> {
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
@ -22,27 +24,19 @@ public class ResponseAdvice implements ResponseBodyAdvice<Object> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
|
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
|
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
|
||||||
try {
|
|
||||||
if (body instanceof String) {
|
if (body instanceof String) {
|
||||||
return objectMapper.writeValueAsString(ResponseMessage.success(body));
|
return body;
|
||||||
|
// return objectMapper.writeValueAsString(ResponseMessage.success(body));
|
||||||
}
|
}
|
||||||
if (body instanceof ResponseMessage) {
|
if (body instanceof ResponseMessage) {
|
||||||
return body;
|
return body;
|
||||||
}
|
}
|
||||||
return ResponseMessage.success(body);
|
return ResponseMessage.success(body);
|
||||||
} catch (JsonProcessingException e) {
|
|
||||||
// 如果序列化失败,直接返回原始的 body
|
|
||||||
return body;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public ObjectMapper getObjectMapper() {
|
|
||||||
return objectMapper;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,13 +18,6 @@ spring:
|
|||||||
flyway:
|
flyway:
|
||||||
baseline-on-migrate: false # 阻止启动时执行 flyway
|
baseline-on-migrate: false # 阻止启动时执行 flyway
|
||||||
locations: classpath:migrations
|
locations: classpath:migrations
|
||||||
doc:
|
|
||||||
api-docs:
|
|
||||||
enabled: true
|
|
||||||
path: /v3/api-docs
|
|
||||||
swagger-ui:
|
|
||||||
enabled: true
|
|
||||||
path: /swagger
|
|
||||||
shell:
|
shell:
|
||||||
interactive:
|
interactive:
|
||||||
enabled: false
|
enabled: false
|
||||||
@ -38,4 +31,16 @@ spring:
|
|||||||
time-zone: PRC
|
time-zone: PRC
|
||||||
server:
|
server:
|
||||||
port: 8088
|
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
|
Loading…
Reference in New Issue
Block a user