【发布时间】:2019-07-20 13:41:21
【问题描述】:
我希望使用 Spring Security(版本 5.1.2)为我的 Angular 7 应用程序生成 CSRF 令牌。我的 SecurityConfig 文件中有以下内容:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and()
.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
}
在我的控制器中使用以下 RequestMapping:
@RestController
@RequestMapping("/authentication")
public class AuthenticationController {
@GetMapping("/csrf")
public void getCsrfToken(){...}
@PostMapping("/register")
public RegisterOutputDTO register(@RequestBody UserDTO input){...}
}
我从各种来源收集到csrfTokenRepository 会在我的第一次GET 调用(这是/authentication/csrf 的用途)中自动生成一个带有标题XSRF-token 的cookie,但我没有从那里得到cookie服务器。因此,在我的下一个POST 电话中,我收到了 403 响应。我可能会错过什么?
【问题讨论】:
-
只有在 cookie
XSRF-TOKEN丢失时才会创建它。查看请求标头以查看它是否已经存在。 -
@Andreas 我也想过这个,但我没有在标题中发送任何这样的标记。我可以在 Chrome 的应用程序选项卡中看到该 cookie 不存在。
-
我相信我已经找到了答案。似乎无法跨域发送 cookie。我的 Angular 应用程序托管在 localhost:3000 上,而我的 Java 后端托管在 localhost:9080 上。看来我的选择是以某种方式将它们部署在同一个域上或使用代理。来源:stackoverflow.com/questions/48002670/…
标签: java angular spring-security csrf