我曾经使用angular-auth-oidc-client
来实现 Web 应用程序的身份验证Angular 8
,它在将用户数据存储在localStorage
或sessionStorage
但使用 cookie 存储实现时client:172 [WDS] Disconnected!
工作正常,控制台中显示诸如 start 之类的错误,并且应用程序停止工作告诉我手动删除存储的 cookie。
作为有关如何在此处自定义存储中添加自定义存储的文档,
我做的cookie存储实现是:
import { Injectable, Inject } from '@angular/core';
import { OidcSecurityStorage } from 'angular-auth-oidc-client';
import { CookieService } from 'ngx-cookie-service';
import { OidcConfiguration } from '../config/oidc.config';
@Injectable({
providedIn: 'root'
})
export class CookiesStorageService implements OidcSecurityStorage {
constructor(private cookieService: CookieService, @Inject('OidcConfig') private oidcConfig: OidcConfiguration) {
}
public read(key: string): any {
let val = this.cookieService.get(key + '_' + this.oidcConfig.openIdConfiguration.client_id);
if (val && val != '') {
return JSON.parse(val);
}
return;
}
public write(key: string, value: any): void {
if (value && value != '') {
value = value === undefined ? null : value;
this.cookieService.set(
key + '_' + this.oidcConfig.openIdConfiguration.client_id,
JSON.stringify(value));
}
}
}
值
this.oidcConfig.openIdConfiguration.client_id
是“OPApiClient”,它将 clientId 作为键名后缀附加。
在AppModule
i 中注册此自定义存储,如下所示:
import { NgModule, Inject } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AuthModule, OidcSecurityService } from 'angular-auth-oidc-client';
...
@NgModule({
declarations: [...],
imports: [
CommonModule,
AuthModule.forRoot({ storage: CookiesStorageService }),
...
],
providers: [...]
})
我的 cookie 存储是否有任何错误或我错过了什么?任何帮助将不胜感激。