feat: 新版优化
This commit is contained in:
parent
ea79cc5ae4
commit
0cf030a57a
@ -154,7 +154,7 @@ public class WebSecurityConfig {
|
|||||||
.accessDeniedHandler(new HttpStatusServerAccessDeniedHandler(HttpStatus.UNAUTHORIZED)))
|
.accessDeniedHandler(new HttpStatusServerAccessDeniedHandler(HttpStatus.UNAUTHORIZED)))
|
||||||
.csrf(spec -> spec.disable())
|
.csrf(spec -> spec.disable())
|
||||||
.addFilterAt(
|
.addFilterAt(
|
||||||
configure(authenticationManager, authenticationAuditor, authenticationConverter),
|
configure(authenticationManager, authenticationAuditor, authenticationConverter, tokenProvider),
|
||||||
SecurityWebFiltersOrder.FORM_LOGIN)
|
SecurityWebFiltersOrder.FORM_LOGIN)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
@ -207,12 +207,12 @@ public class WebSecurityConfig {
|
|||||||
*/
|
*/
|
||||||
private AuthenticationWebFilter configure(ReactiveAuthenticationManager authenticationManager,
|
private AuthenticationWebFilter configure(ReactiveAuthenticationManager authenticationManager,
|
||||||
AuthenticationAuditor authenticationAuditor,
|
AuthenticationAuditor authenticationAuditor,
|
||||||
ServerAuthenticationConverter authenticationConverter) {
|
ServerAuthenticationConverter authenticationConverter, TokenProvider tokenProvider) {
|
||||||
AuthenticationWebFilter authenticationFilter = new AuthenticationWebFilter(authenticationManager);
|
AuthenticationWebFilter authenticationFilter = new AuthenticationWebFilter(authenticationManager);
|
||||||
authenticationFilter.setRequiresAuthenticationMatcher(pathMatchers(HttpMethod.POST, "/login", "/api/login"));
|
authenticationFilter.setRequiresAuthenticationMatcher(pathMatchers(HttpMethod.POST, "/login", "/api/login"));
|
||||||
authenticationFilter.setAuthenticationFailureHandler(new JsonAuthenticationFailureHandler(authenticationAuditor));
|
authenticationFilter.setAuthenticationFailureHandler(new JsonAuthenticationFailureHandler(authenticationAuditor));
|
||||||
authenticationFilter.setServerAuthenticationConverter(authenticationConverter);
|
authenticationFilter.setServerAuthenticationConverter(authenticationConverter);
|
||||||
authenticationFilter.setAuthenticationSuccessHandler(new JsonAuthenticationSuccessHandler(authenticationAuditor));
|
authenticationFilter.setAuthenticationSuccessHandler(new JsonAuthenticationSuccessHandler(authenticationAuditor, tokenProvider));
|
||||||
authenticationFilter.setSecurityContextRepository(contextRepository());
|
authenticationFilter.setSecurityContextRepository(contextRepository());
|
||||||
return authenticationFilter;
|
return authenticationFilter;
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package dev.flyfish.framework.config;
|
|||||||
|
|
||||||
import dev.flyfish.framework.beans.resolver.DynamicRestBeanResolver;
|
import dev.flyfish.framework.beans.resolver.DynamicRestBeanResolver;
|
||||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||||
|
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
import org.springframework.web.server.WebFilter;
|
import org.springframework.web.server.WebFilter;
|
||||||
@ -13,28 +14,6 @@ import org.springframework.web.server.WebFilter;
|
|||||||
*/
|
*/
|
||||||
public class BeanConfig {
|
public class BeanConfig {
|
||||||
|
|
||||||
/**
|
|
||||||
* 配置支持context-path
|
|
||||||
*
|
|
||||||
* @param serverProperties 服务器参数
|
|
||||||
* @return 结果
|
|
||||||
*/
|
|
||||||
@Bean
|
|
||||||
public WebFilter contextPathWebFilter(ServerProperties serverProperties) {
|
|
||||||
String contextPath = serverProperties.getServlet().getContextPath();
|
|
||||||
return (exchange, chain) -> {
|
|
||||||
ServerHttpRequest request = exchange.getRequest();
|
|
||||||
if (request.getURI().getPath().startsWith(contextPath)) {
|
|
||||||
return chain.filter(
|
|
||||||
exchange.mutate()
|
|
||||||
.request(request.mutate().contextPath(contextPath).build())
|
|
||||||
.build());
|
|
||||||
}
|
|
||||||
return chain.filter(exchange);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public DynamicRestBeanResolver dynamicRestBeanResolver() {
|
public DynamicRestBeanResolver dynamicRestBeanResolver() {
|
||||||
return new DynamicRestBeanResolver();
|
return new DynamicRestBeanResolver();
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package dev.flyfish.framework.handler;
|
package dev.flyfish.framework.handler;
|
||||||
|
|
||||||
import dev.flyfish.framework.bean.Result;
|
import dev.flyfish.framework.bean.Result;
|
||||||
|
import dev.flyfish.framework.configuration.jwt.TokenProvider;
|
||||||
import dev.flyfish.framework.service.AuthenticationAuditor;
|
import dev.flyfish.framework.service.AuthenticationAuditor;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
@ -22,6 +23,8 @@ public class JsonAuthenticationSuccessHandler implements ServerAuthenticationSuc
|
|||||||
|
|
||||||
// 数据块工厂
|
// 数据块工厂
|
||||||
private final AuthenticationAuditor authenticationAuditor;
|
private final AuthenticationAuditor authenticationAuditor;
|
||||||
|
// token提供者
|
||||||
|
private final TokenProvider tokenProvider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 登录成功后要返回用户的基本信息,节省带宽
|
* 登录成功后要返回用户的基本信息,节省带宽
|
||||||
@ -32,6 +35,9 @@ public class JsonAuthenticationSuccessHandler implements ServerAuthenticationSuc
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Mono<Void> onAuthenticationSuccess(WebFilterExchange webFilterExchange, Authentication authentication) {
|
public Mono<Void> onAuthenticationSuccess(WebFilterExchange webFilterExchange, Authentication authentication) {
|
||||||
|
// 添加成功响应头
|
||||||
|
tokenProvider.addToken(webFilterExchange.getExchange(), authentication);
|
||||||
|
// 组装正确的响应信息
|
||||||
ServerHttpResponse response = webFilterExchange.getExchange().getResponse();
|
ServerHttpResponse response = webFilterExchange.getExchange().getResponse();
|
||||||
HttpHeaders headers = response.getHeaders();
|
HttpHeaders headers = response.getHeaders();
|
||||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user