Java Spring Security返回200而不是HttpWebHandlerAdapter所述的401

Java Spring Security返回200而不是HttpWebHandlerAdapter所述的401,java,spring,spring-boot,spring-security,spring-webflux,Java,Spring,Spring Boot,Spring Security,Spring Webflux,试图弄清楚我是否在SpringSecurity中发现了一个bug,这是针对最新的2.4.5版本的。HttpWebHandlerAdapter声明它在日志中返回401,而在Postman中的响应是200。下面是相关的spring安全配置/处理程序等 Spring安全配置 @配置 @启用WebFluxSecurity @EnableReactiveMethodSecurity(proxyTargetClass=true) @所需参数构造函数 公共类SecurityConfig{ 私有最终JwtAut

试图弄清楚我是否在SpringSecurity中发现了一个bug,这是针对最新的2.4.5版本的。HttpWebHandlerAdapter声明它在日志中返回401,而在Postman中的响应是200。下面是相关的spring安全配置/处理程序等

Spring安全配置

@配置
@启用WebFluxSecurity
@EnableReactiveMethodSecurity(proxyTargetClass=true)
@所需参数构造函数
公共类SecurityConfig{
私有最终JwtAuthenticationConverter JwtAuthenticationConverter;
私有最终用户Repository用户Repository;
私有最终用户角色存储用户角色存储;
私人最终角色扮演者角色扮演者角色扮演者角色扮演者角色扮演者角色扮演者;
私有最终JwtUtil JwtUtil;
私有静态映射验证白名单=
地图(
//公共身份验证端点
HttpMethod.PUT,新字符串[]{“/v1/auth/login”},
HttpMethod.POST,新字符串[]{“/v1/auth/register”},
HttpMethod.GET,
新字符串[]{
//促动器
“/执行机构”,
“/执行机构/健康*”,
“/exactor/info”,
//SpringFox/OpenAPI
“/v3/api docs/**”,
“/swagger ui/**”,
“/swagger resources/**”,
“/webjars/swagger ui/**”,
//公共API端点
“/v1/posts/*/comments/*”,
“/v1/posts/*/comments”,
“/v1/posts/*”,
“/v1/posts”
});
@豆子
公共安全WebFilterChain安全WebFilterChain(ServerHttpSecurity http){
//生成路径/动词匹配器
Set matchers=new HashSet();
AUTH_WHITELIST.forEach(
(方法,路径)->matchers.add(ServerWebExchangeMatchers.pathMatchers(方法,路径));
ServerWebExchangeMatcher[]matchersArray=matchers.toArray(新的ServerWebExchangeMatcher[0]);
返回http.addFilterAt(
getAuthenticationWebFilter(matchersArray),SecurityWebFiltersOrder.AUTHENTICATION)
.授权交易所()
.matchers(matchersArray)
.permitAll()
.anyExchange()
.authenticated()
.及()
.formLogin()
.及()
.csrf()
.disable()
.cors()
.configurationSource(createCorsConfigSource())
.及()
.formLogin()
.disable()
.httpBasic()
.disable()
.logout()
.disable()
.build();
}
公共公司配置源createCorsConfigSource(){
org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource=
新的UrlBasedCorsConfigurationSource();
CorsConfiguration配置=新的CorsConfiguration();
config.addAllowedOrigin(“http://localhost:3000");
config.setAllowedMethods(List.of(“OPTIONS”、“GET”、“PUT”、“POST”、“DELETE”));
source.registerCorsConfiguration(“/**”,config);
返回源;
}
私有身份验证WebFilter getAuthenticationWebFilter(
ServerWebExchangeMatcher[]匹配器阵列){
//使用自定义用户详细信息服务/身份验证管理器创建web筛选器
AuthenticationWebFilter AuthenticationWebFilter=
新建AuthenticationWebFilter(新建AuthenticationManager(customUserDetailsService());
//设置自定义JWT身份验证转换器
authenticationWebFilter.setServerAuthenticationConverter(jwtAuthenticationConverter);
//否定白名单以设置具有所需身份验证的路径
否定服务器WebExchangeMatcher否定白名单=
新的否定ServerWebExchangeMatcher(ServerWebExchangeMatchers.matchers(MatcherArray));
authenticationWebFilter.setRequiresAuthenticationMatcher(否定白名单);
//添加失败处理程序
authenticationWebFilter.setAuthenticationFailureHandler(新的AuthenticationFailureHandler());
返回authenticationWebFilter;
}
@豆子
公共密码编码器PasswordEncoder(){
返回新的BCryptPasswordEncoder();
}
@豆子
@初级的
public userdetails服务customuserdetails服务(){
返回新的UserDetailsService(
新用户服务(
用户假定,
用户角色存储库,
角色扮演者,
passwordEncoder(),
jwtUtil);
}
}
故障处理程序

@Slf4j
公共类AuthenticationFailureHandler实现ServerAuthenticationFailureHandler{
@凌驾
公共身份验证失败(
WebFilterExchange WebFilterExchange,身份验证异常){
log.warn(exception.getMessage());
ServerHttpResponse response=webFilterExchange.getExchange().getResponse();
response.setStatusCode(HttpStatus.UNAUTHORIZED);
response.getHeaders().addIfAbsent(HttpHeaders.LOCATION,“/”;
response.setComplete();
返回Mono.error(异常);
}
}
JWT认证转换器

@Slf4j
@所需参数构造函数
@组成部分
公共类JwtAuthenticationConverter实现ServerAuthenticationConverter{
私有最终JwtUtil JwtUtil;
专用Mono extractJwtFromAuthorizationHeader(服务器WebExchange){
返回Mono.justOrEmpty(exchange.getRequest().getHeaders().get(HttpHeaders.AUTHORIZATION))
//删除空标题/以空字符串作为值的标题
.过滤器(
标题->
!header.isEmpty()
&&StringUtils.hasText(header.get(0))
&&header.get(0).包含(“承载者”))
.map(header->header.get(0).replaceAll(AuthConstants.BEARER\u PREFIX\u REGEX,“”)
.switchIfEmpty(Mono.error(新的InvalidJwtException(“无效承载令牌”));
}
@凌驾
公共Mono转换(服务器WebExchange){
返回Mono.justOrEmpty(交换)
.flatMap(此::extractJwtFromAuthorizationHeader)
.map(jwtUtil::getAuthenticationFromJwt);
}
}
身份验证管理器

公共类身份验证管理
2021-05-05 15:41:18.981 DEBUG 1984531 --- [or-http-epoll-3] o.s.w.s.h.ResponseStatusExceptionHandler : [82168f6e-13] Resolved [InvalidJwtException: Unsupported JWT token: The parsed JWT indicates it was signed with the HS512 signature algorithm, but the specified signing key of type sun.security.rsa.RSAPublicKeyImpl may not be used to validate HS512 signatures.  Because the specified signing key reflects a specific and expected algorithm, and the JWT does not reflect this algorithm, it is likely that the JWT was not expected and therefore should not be trusted.  Another possibility is that the parser was configured with the incorrect signing key, but this cannot be assumed for security reasons.] for HTTP POST /v1/posts
2021-05-05 15:41:18.981 DEBUG 1984531 --- [or-http-epoll-3] o.s.w.s.adapter.HttpWebHandlerAdapter    : [82168f6e-13] Completed 401 UNAUTHORIZED