我正在尝试在Spring Boot API上构建一个Angular 2页面。我已经配置了CORS(正确的我相信),但是我被Spring Security的CSRF保护阻止了。Angular 2/Spring Security CSRF实现问题
这是我的理解,Angular2自动处理RC2的RCF,我在RC4上。服务器响应来自客户端的POST发送XSRF令牌在这里看到:
我认为角2不捡起来?我知道CORS正在工作,因为当我在.csrf()的末尾放.ignoringAntMatchers(“/ urlhere /”)进行测试时,请求已经完成,因此CORS不会阻止它。这里是我的HttpSecurity配置方法:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/auth/**", "/signup/**").permitAll()
.and()
.headers()
.and()
.exceptionHandling()
.and()
.cors()
.and()
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
}
我的登录信息在客户端过程包括,首先发送证书的方法,然后使用JWT令牌来获取证书。两种方法如下:
sendCredentials(model) {
let tokenUrl = 'http://localhost:8080/user/login';
let headers1 = new Headers({'Content-Type': 'application/json'});
return this.http.post(tokenUrl, JSON.stringify(model), {headers: headers1});
}
sendToken(token){
let userUrl = 'http://localhost:8080/rest/user/users';
let headers2 = new Headers({'Authorization': 'Bearer '+token});
return this.http.get(userUrl, {headers:headers2});
}
我缺少什么来满足CSRF保护的要求?它是客户端吗?或者我需要将/login/添加到我的antMatchers列表中?
感谢您的回答!它增加了知识库,所以我将其标记为已回答。 –