栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Angular 6.x /设置jsessionid cookie

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Angular 6.x /设置jsessionid cookie

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/412131.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号