Angular HTTPInterceptor是最合适的解决方案 。
您可以按照以下步骤使用它:
1: 构建您的HTTPInterceptor (@Injectable服务):
@Injectable()export class SpringbootInterceptor implements HttpInterceptor { constructor(public auth: AuthService) {} intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { // Clone the request to add the new header const clonedRequest = req.clone({ headers: req.headers.set('Set-cookie', 'jsessionid=' + this.auth.getJSessionId()) }); // Pass control to the next request return next.handle(clonedRequest); }}请注意, .clone() 方法添加了作为参数提供的信息。
2: 将Interceptor设置为您的NgModule提供者 :
@NgModule({ bootstrap: [AppComponent], imports: [...], providers: [ { provide: HTTP_INTERCEPTORS, useClass: SpringbootInterceptor, multi: true } ]})现在,来自NgModule的任何请求都可以在 SpringbootInterceptor中 设置标头。
您可以在以下位置查看更多信息:
- https://angular.io/api/common/http/HttpInterceptor
- https://angular.io/api/http/Headers



