在 Angular 文档中,提到 Angular会自动在post 请求的标头中httpclient
发送 cookie 的值。文档链接XSRF-TOKEN
X-XSRF-TOKEN
但它不会为我发送标题。这是我的代码
设置 cookie 的 Nodejs 代码
router.get('/set-csrf',function(req,res,next){
res.setHeader('Set-Cookie', "XSRF-TOKEN=abc;Path=/; HttpOnly; SameSite=Strict");
res.send();
})
我在 app.module.ts 中使用了 httpclient
imports: [
HttpClientModule
]
** 以上代码仅用于调试目的。我没有 set-csrf 端点。
但是当我发送发布请求时它不会发送任何标题。我无法调试。
我也在 Angular 的 github 存储库中添加了这个问题。HttpXsrfInterceptor 检查请求是 GET 还是 HEAD,或者它是否以 http 开头。如果为真,它会跳过添加标题。
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const lcUrl = req.url.toLowerCase();
// Skip both non-mutating requests and absolute URLs.
// Non-mutating requests don't require a token, and absolute URLs require special handling
// anyway as the cookie set
// on our origin is not the same as the token expected by another origin.
if (req.method === 'GET' || req.method === 'HEAD' || lcUrl.startsWith('http://') ||
lcUrl.startsWith('https://')) {
return next.handle(req);
}
const token = this.tokenService.getToken();
// Be careful not to overwrite an existing header of the same name.
if (token !== null && !req.headers.has(this.headerName)) {
req = req.clone({headers: req.headers.set(this.headerName, token)});
}
return next.handle(req);
}
我不确定他们为什么跳过 http/s 部分。这是我在 github 上的问题