【发布时间】:2018-08-24 10:59:06
【问题描述】:
我一直在做一个在我的后端使用 spring + spring security 的项目,最近遇到了一个与 cors 相关的问题:
前两行您可以看到成功的 api 调用输出,随后 chrome 尝试将带有选项标头的 Http 请求发送到我的休息服务。
我的问题是这个问题的潜在根源是什么?
我在 dev 中的前端使用 angular CLI 客户端,在我为 Spring Security 创建自定义过滤器并添加我的 WebConfigurerAdapter 配置之前,我没有遇到任何 CORS 问题。如果需要,很乐意提供额外的代码。但是我认为它与此无关,因为我删除了过滤器并且发生了同样的问题。
在 chrome、firefox 和 ubuntu 浏览器中测试出现同样的问题。
Angular CLI 正在成功代理对“/api/[blah]”的调用
这是我的代理配置,我试过只匹配所有内容,但这没有区别。
{
"/api/*": {
"target": "http://localhost:8080",
"secure": false,
"logLevel": "debug",
"changeOrigin": true
}
}
WebSecurityAdapter配置
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/index.html", "/api/registration", "/api/authenticate",
"/api/isAuthenticated", "/api/validateCaptcha", "/console", "/api/loginRecaptchaRequired", "/api/login", "/")
.permitAll()
.anyRequest()
.authenticated()
.and()
.addFilterBefore(
authenticationFilter(),
UsernamePasswordAuthenticationFilter.class)
.logout()
.logoutUrl("/api/logout")
.and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.and()
.addFilterAfter(csrfHeaderFilter(), SessionManagementFilter.class);
}
【问题讨论】:
-
您似乎缺少 allow-origin 标头,因此没有来源可以使用 CORS。 Info
-
后端,我会确定哪些域应该能够发出 CORS 请求,并在此处将它们列入白名单,除非您想将其公开,然后使用
*
标签: java spring spring-security cors angular-cli