<分区>
我一直在开发一个在我的后端使用 spring + spring security 的项目,最近遇到了一个与 cors 相关的问题:
前两行您可以看到成功的 api 调用输出,然后是 chrome 尝试向我的 rest 服务发送带有选项 header 的 Http 请求。
我的问题是这个问题的潜在根源是什么?
我在开发中的前端使用角度 CLI 客户端,在我为 spring 安全创建自定义过滤器并添加我的 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);
}