feat: token的生成的解析需要以sign-verify,encrypt-decrypt成对出现

This commit is contained in:
wangyu 2024-10-01 00:26:32 +08:00
parent 0cf030a57a
commit 59ab17aa0c
3 changed files with 11 additions and 13 deletions

View File

@ -113,7 +113,7 @@ public class WebSecurityConfig {
} }
@ConditionalOnProperty(value = "jwt.enable", havingValue = "false") @ConditionalOnProperty(value = "jwt.enable", havingValue = "false")
@Bean @Bean("contextRepository")
public ServerSecurityContextRepository contextRepository() { public ServerSecurityContextRepository contextRepository() {
return new WebSessionServerSecurityContextRepository(); return new WebSessionServerSecurityContextRepository();
} }
@ -137,10 +137,11 @@ public class WebSecurityConfig {
SecurityProperties properties, SecurityProperties properties,
ReactiveUserDetailsService userDetailsService, ReactiveUserDetailsService userDetailsService,
ServerAuthenticationConverter authenticationConverter, ServerAuthenticationConverter authenticationConverter,
AuthenticationAuditor authenticationAuditor) { AuthenticationAuditor authenticationAuditor,
ServerSecurityContextRepository contextRepository) {
ReactiveAuthenticationManager authenticationManager = new UserDetailsRepositoryReactiveAuthenticationManager(userDetailsService); ReactiveAuthenticationManager authenticationManager = new UserDetailsRepositoryReactiveAuthenticationManager(userDetailsService);
return http return http
.securityContextRepository(contextRepository()) .securityContextRepository(contextRepository)
.authorizeExchange(spec -> spec.pathMatchers(ArrayUtils.addAll(properties.getAllowUris(), "/api/logout", "/api/login")).permitAll() .authorizeExchange(spec -> spec.pathMatchers(ArrayUtils.addAll(properties.getAllowUris(), "/api/logout", "/api/login")).permitAll()
.pathMatchers("/api/users/**").authenticated() .pathMatchers("/api/users/**").authenticated()
.anyExchange().authenticated()) .anyExchange().authenticated())
@ -154,7 +155,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, tokenProvider), configure(authenticationManager, authenticationAuditor, authenticationConverter, contextRepository),
SecurityWebFiltersOrder.FORM_LOGIN) SecurityWebFiltersOrder.FORM_LOGIN)
.build(); .build();
} }
@ -207,13 +208,14 @@ public class WebSecurityConfig {
*/ */
private AuthenticationWebFilter configure(ReactiveAuthenticationManager authenticationManager, private AuthenticationWebFilter configure(ReactiveAuthenticationManager authenticationManager,
AuthenticationAuditor authenticationAuditor, AuthenticationAuditor authenticationAuditor,
ServerAuthenticationConverter authenticationConverter, TokenProvider tokenProvider) { ServerAuthenticationConverter authenticationConverter,
ServerSecurityContextRepository contextRepository) {
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, tokenProvider)); authenticationFilter.setAuthenticationSuccessHandler(new JsonAuthenticationSuccessHandler(authenticationAuditor));
authenticationFilter.setSecurityContextRepository(contextRepository()); authenticationFilter.setSecurityContextRepository(contextRepository);
return authenticationFilter; return authenticationFilter;
} }

View File

@ -168,9 +168,9 @@ public class TokenProvider implements InitializingBean {
*/ */
public Claims parseToken(String token) { public Claims parseToken(String token) {
return Jwts.parser() return Jwts.parser()
.decryptWith(key) .verifyWith(key)
.build() .build()
.parseEncryptedClaims(token) .parseSignedClaims(token)
.getPayload(); .getPayload();
} }

View File

@ -23,8 +23,6 @@ public class JsonAuthenticationSuccessHandler implements ServerAuthenticationSuc
// 数据块工厂 // 数据块工厂
private final AuthenticationAuditor authenticationAuditor; private final AuthenticationAuditor authenticationAuditor;
// token提供者
private final TokenProvider tokenProvider;
/** /**
* 登录成功后要返回用户的基本信息节省带宽 * 登录成功后要返回用户的基本信息节省带宽
@ -35,8 +33,6 @@ 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();