我用JSESSIONID通过Spring安全认证编写了一些代码。使用邮递员,效果很好:
当我试图通过React与axios来获取一些数据时,问题就开始了:
在两种情况下,我都在请求中发送了正确的JSESSIONID,但是在浏览器中,我得到403,而在邮递员中我得到了200。
您认为此错误与Spring Security的错误配置有关吗?还是请求中的标题错误?
Spring安全配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration configuration = new CorsConfiguration();
configuration.addAllowedOrigin("http://localhost:3000");
configuration.addAllowedHeader("*");
configuration.setAllowCredentials(true);
configuration.addAllowedMethod("GET");
configuration.addAllowedMethod("PUT");
configuration.addAllowedMethod("POST");
source.registerCorsConfiguration("/**", configuration);
CorsFilter corsFilter = new CorsFilter(source);
http
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.ALWAYS).and()
.addFilterBefore(corsFilter, LogoutFilter.class)
.headers()
.cacheControl().and()
.frameOptions().deny()
.xssProtection().xssProtectionEnabled(true).and()
.contentTypeOptions().and()
.and()
.csrf().disable()
;
}