Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Security返回403而不是401,并创建无效的Redis会话cookie_Java_Spring_Spring Boot_Spring Security_Spring Data Redis - Fatal编程技术网

Java Spring Security返回403而不是401,并创建无效的Redis会话cookie

Java Spring Security返回403而不是401,并创建无效的Redis会话cookie,java,spring,spring-boot,spring-security,spring-data-redis,Java,Spring,Spring Boot,Spring Security,Spring Data Redis,我使用SpringSecurity和SpringDataRedis跟踪具有自定义角色和权限的用户会话。当我尝试在浏览器中没有会话cookie的情况下访问预授权的端点时,它应该返回401。而是创建一个新的(无效)会话cookie,端点返回403 这是我的安全配置: @配置 @EnableGlobalMethodSecurity(preprestenabled=true,order=Ordered。最高优先级) 公共类SecurityConfig扩展了WebSecurity配置适配器{ @凌驾 受保

我使用SpringSecurity和SpringDataRedis跟踪具有自定义角色和权限的用户会话。当我尝试在浏览器中没有会话cookie的情况下访问预授权的端点时,它应该返回401。而是创建一个新的(无效)会话cookie,端点返回403

这是我的安全配置:

@配置
@EnableGlobalMethodSecurity(preprestenabled=true,order=Ordered。最高优先级)
公共类SecurityConfig扩展了WebSecurity配置适配器{
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
http
.authorizeRequests((授权)->授权
.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
.anyRequest().authenticated()
)
//SameSite=严格;
.csrf().disable().cors();
}
@豆子
公共公司过滤器{
UrlBasedCorsConfigurationSource=新的UrlBasedCorsConfigurationSource();
CorsConfiguration配置=新的CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin(“*”);
config.addAllowedMethod(HttpMethod.GET);
config.addAllowedMethod(HttpMethod.POST);
config.addAllowedMethod(HttpMethod.DELETE);
config.addAllowedMethod(HttpMethod.OPTIONS);
config.addAllowedHeader(“授权”);
config.addAllowedHeader(“内容类型”);
config.addAllowedHeader(“*”);
source.registerCorsConfiguration(“/**”,config);
返回新的过滤器(来源);
}
}

我还使用
MethodSecurityConfig
UserDetails
的一个实现来解析来自用户身份验证的自定义字段。

以下是修复方法,适用于遇到类似问题的任何人:

@覆盖
受保护的无效配置(HttpSecurity http)引发异常{
http.sessionManagement().sessionCreationPolicy(sessionCreationPolicy.NEVER)和()//让redis处理会话创建
.csrf().disable().cors()和()
.requestCache().disable().exceptionHandling()和()//防止异常创建重复会话
.authorizeRequests().anyRequest().authorized()和()//所有端点都需要进行身份验证
.exceptionHandling().authenticationEntryPoint(
新建HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED));//在没有会话时返回401
}

添加AuthenticationFailureHandler并将响应状态编辑为401@RoieBeck谢谢,但这不起作用。在调试模式下运行后端时,我在故障处理程序中放置了一个断点,但它没有到达。请确保在自动验证后添加了这一行:
.failureHandler(authenticationFailureHandler())@RoieBeck yessir