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