在我的Angular应用中,我有一个这样的interceptor类,我使用的是Angular Universal服务器端渲染,所以我从服务器上获取token,然后在每次被服务器调用的API调用中手动设置。
import { Injectable, Inject, Optional, PLATFORM_ID } from '@angular/core';
import {
HttpInterceptor,
HttpHandler,
HttpRequest,
} from '@angular/common/http';
import { REQUEST } from '@nguniversal/express-engine/tokens';
import { isPlatformServer } from '@angular/common';
@Injectable()
export class UniversalInterceptor implements HttpInterceptor {
constructor(
@Inject(PLATFORM_ID) private platformId,
@Optional() @Inject(REQUEST) private request
) {}
intercept(req: HttpRequest<any>, next: HttpHandler) {
if (isPlatformServer(this.platformId)) {
req = req.clone({ headers: req.headers.set('Cookie', this.request.headers.cookie) });
}
return next.handle(req);
}
}
我使用的是Angular Universal服务器端渲染,所以我从服务器上获取token,然后在每次服务器调用的API调用中手动设置。一切都很好,但这安全吗?我在每个API请求的头中手动设置了token cookie,也许这样做有一定的风险?
我可能错了,但我认为只有当你向你不拥有的API发送cookie时才会有问题,在这种情况下,你可能会向第三方发送敏感信息。
如果你不拥有API,你可以尝试解析cookie字符串(this.request.headers.cookie
),并且只传递API需要的。