【发布时间】:2021-02-10 17:32:09
【问题描述】:
我有一个带有 Spring Security 的 Spring Boot 应用程序。当来自不同主机的任何客户端从我的应用程序调用 API 方法时,他会看到异常:
Access to XMLHttpRequest at 'http://localhost:8080/test' from origin 'http://localhost:8088' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
但是我的控制器仍然接受请求。我想禁用它。我希望 Spring Security 拒绝这些请求。
我试过了:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.cors().and()
还有这个:
@Bean
CorsConfigurationSource corsConfigurationSource() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
return source;
}
我想我不了解全貌,我想了解:如何在应用层阻止 CORS 请求?因为默认情况下 - 应用程序处理请求,而只是浏览器阻止响应。
【问题讨论】:
标签: spring-boot spring-security cors