【发布时间】:2019-01-14 13:59:27
【问题描述】:
请阅读问题。我在 Stack Overflow 上尝试了所有答案。
错误:
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'.
Origin 'http://localhost:4200' is therefore not allowed access. The
credentials mode of requests initiated by the XMLHttpRequest is controlled by
the withCredentials attribute.
在 Angular 的 4200 端口上运行的前端:
let headers = new Headers({
"Content-Type": "application/x-www-form-urlencoded"});
let options = new RequestOptions({ headers: headers ,withCredentials:true});
//body contains username and password.
this.http.post('http://localhost:8080/login',{body}, options).
subscribe(response => {
console.log(response)
});
后端使用 Spring Boot 在端口 8080 上运行:
@CrossOrigin(origins = "http://localhost:4200",allowCredentials="true")
@RestController
public class SecurityApplication {
... //Rest Api's
}
//Security Config
protected void configure(HttpSecurity http) throws Exception {
http.
cors().and().httpBasic().and().formLogin()
.and()
.authorizeRequests()
.antMatchers( "/", "/home", "/login").permitAll()
.anyRequest().authenticated();
}
我正在点击 Spring Security 提供的 /login api。
我提到了https://www.concretepage.com/spring-4/spring-4-rest-cors-integration-using-crossorigin-annotation-xml-filter-example,据此 origins = "http://localhost:4200" 应在响应标头中设置 Access-Control-Allow-Origin 以提供给它的值,但我仍然得到通配符'*'作为响应。
我尝试了 corsFilters 但没有奏效。我在 Chrome 浏览器中启用了 CORS 扩展。我想知道为什么它没有将 Access-Control-Allow-Origin 设置为我提供的值以及如何解决它。
【问题讨论】:
-
你有没有
CorsConfigurationSource暴露? -
是的,我通过引用 link 尝试过,但没有成功。
标签: angular spring-boot spring-security