角度CSRF保护

时间:2018-03-13 11:27:07

标签: angular spring-boot angular5 interceptor csrf-protection

我做过spring boot csrf保护。我将令牌设置为cookie,并希望从请求中获取它。

在angular 5中,我编写了自定义拦截器,它从cookie中获取csrf令牌并将其设置为每个帖子请求的标题,一切正常。

我的问题是我必须发送第一个帖子请求,而令牌还没有在cookie中设置bean。然后任何其他请求都可以正常工作。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

我的角度拦截器:

intercept(req: HttpRequest<any> , next: HttpHandler): Observable<HttpEvent<any>> {
    if (req.method === 'POST') {
      this.cookieValue = this.cookieService.get('CT');
      this.authReq  = req.clone({ headers: req.headers.set('X-XSRF-TOKEN', this.cookieValue )}
    );
    } else {
      this.cookieValue = this.cookieService.get('CT');
      this.authReq = req.clone({ headers: req.headers.set('Accept', '')});
    }
    return next.handle(this.authReq)