Angular设置Cookies会话必须与前端会话一致...(响应头)
创始人
2024-10-29 11:30:08
0

在前端,通过使用withCredentials和CredentialsInterceptor处理来自服务器的响应头,以便Angular可以设置会话Cookie。

例如,在Angular的http拦截器中添加以下代码:

import { Injectable } from '@angular/core'; import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest } from '@angular/common/http'; import { Observable } from 'rxjs/Observable';

@Injectable() export class CredentialsInterceptor implements HttpInterceptor { intercept(req: HttpRequest, next: HttpHandler): Observable> { req = req.clone({ withCredentials: true }); return next.handle(req); } }

然后,将CredentialsInterceptor添加到providers数组中,可以在app.module.ts的@NgModule装饰器中进行添加:

import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http'; import { AppComponent } from './app.component'; import { CredentialsInterceptor } from './credentials.interceptor';

@NgModule({ imports: [BrowserModule, HttpClientModule], declarations: [AppComponent], providers: [ { provide: HTTP_INTERCEPTORS, useClass: CredentialsInterceptor, multi: true } ], bootstrap: [AppComponent] }) export class AppModule {}

这样,Angular设置的会话Cookie就可以与前端会话保持一致了。

相关内容

热门资讯

透视开发!wepoker俱乐部... 透视开发!wepoker俱乐部辅助(透视)开挂辅助方法(其实真的是有挂)-哔哩哔哩小薇(透视辅助)致...
透视游戏!德普之星辅助工具如何... 透视游戏!德普之星辅助工具如何设置,aapoker插件,妙招教程(果然真的有挂)-哔哩哔哩1、德普之...
透视德州版!wepoker辅助... 透视德州版!wepoker辅助软件视频(透视)开挂辅助工具(总是真的有挂)-哔哩哔哩亲,关键说明,w...
透视苹果版!德普之星透视软件免... 您好,德普之星透视软件免费入口官网这款游戏可以开挂的,确实是有挂的,需要了解加去威信【4852750...
透视黑科技!wepoker如何... 透视黑科技!wepoker如何设置透视(透视)开挂辅助攻略(真是存在有挂)-哔哩哔哩1、首先打开we...
透视有挂!wepoker辅助器... 透视有挂!wepoker辅助器下载,wepoker私人局辅助,大纲教程(一贯真的是有挂)-哔哩哔哩1...
透视线上!hhpoker真的假... 透视线上!hhpoker真的假的(透视)开挂辅助神器(都是是有挂)-哔哩哔哩1、进入到hhpoker...
透视总结!德普之星透视免费(透... 透视总结!德普之星透视免费(透视)开挂辅助器(切实真的是有挂)-哔哩哔哩1、上手简单,内置详细流程视...
透视智能ai!智星菠萝有挂吗,... 透视智能ai!智星菠萝有挂吗,wepoker辅助器安装包定制,烘培教程(果然存在有挂)-哔哩哔哩1、...
透视靠谱!aapoker能控制... 透视靠谱!aapoker能控制牌吗(透视)开挂辅助脚本(总是是真的挂)-哔哩哔哩aapoker能控制...