Java POST方法上缺少集合cookie标头
当我从Angular使用HTTP POST方法执行请求时,响应没有设置cookie头,相反,当我使用HTTP GET方法执行请求时,响应带有设置cookie头 我知道当使用POST方法时,浏览器会自动发出飞行前选项请求。我在寻找建议,在POST方法请求客户端get set cookie头之后,我可以做些什么,因为它已经与get方法一起工作了 我的请求:Java POST方法上缺少集合cookie标头,java,jakarta-ee,spring-security,http-post,csrf,Java,Jakarta Ee,Spring Security,Http Post,Csrf,当我从Angular使用HTTP POST方法执行请求时,响应没有设置cookie头,相反,当我使用HTTP GET方法执行请求时,响应带有设置cookie头 我知道当使用POST方法时,浏览器会自动发出飞行前选项请求。我在寻找建议,在POST方法请求客户端get set cookie头之后,我可以做些什么,因为它已经与get方法一起工作了 我的请求: this.http.post<UserDto>("//localhost:8080/users", this.user, {with
this.http.post<UserDto>("//localhost:8080/users", this.user, {withCredentials: true}).subscribe( user => {
alert("User created successfully.");
});;
和我的请求标题:
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: pl-PL,pl;q=0.9,en-US;q=0.8,en;q=0.7
Access-Control-Request-Headers: content-type
Access-Control-Request-Method: POST
Connection: keep-alive
Host: localhost:8080
Origin: http://localhost:4200
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
我的Spring安全配置:
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.
cors().and().
csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.and().addFilterBefore(
new StatelessCSRFFilter(), CsrfFilter.class);
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(ImmutableList.of("http://localhost:4200"));
configuration.setAllowedMethods(Arrays.asList("POST","PUT","DELETE","GET","OPTIONS"));
configuration.setAllowCredentials(true);
configuration.setAllowedHeaders(ImmutableList.of("Authorization", "Cache-Control", "Content-Type"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}
我发现了这样一个建议:
“我为飞行前选项请求添加了一个响应过滤器,该过滤器可以在没有凭证的情况下工作
<security-constraint>
<display-name>No check for options</display-name>
<web-resource-collection>
<web-resource-name>All Access</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>OPTIONS</http-method>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
不检查选项
所有访问
/*
选择权
没有一个
我不知道如何实现spring的解决方案
CORS配置已在SpringFramework后端正确设置,因为方法按预期成功,并且Angular确实为GET请求设置了Cookie。只有POST操作在没有HTTP.GET请求的情况下才不会成功。是否可以在spring security飞行前选项请求中进行筛选?您的问题是unclear.请添加您的
GET
请求的响应。您设置了什么类型的cookie?您是为OPTIONS
请求还是为POST
请求获取已添加的响应?是否可以在春季安全飞行前选项请求中过滤?您的问题不清楚。请添加您的GET
请求的响应也请求。您设置了什么类型的cookie?您是为OPTIONS
请求还是为POST
请求获得已添加的响应?
<security-constraint>
<display-name>No check for options</display-name>
<web-resource-collection>
<web-resource-name>All Access</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>OPTIONS</http-method>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>