Angular OIDC库随机登出问题求助
Angular应用随机登出问题:无需凭证即可重新登录,疑似OIDC状态异常
我在开发Angular应用时碰到了一个非常棘手的随机登出问题——这个问题很难稳定复现,但用户在正常使用过程中会突然被强制登出并跳转至登录页面。更奇怪的是,此时点击登录按钮不需要输入任何账号凭证,就能直接重新登录回应用。
我初步推测这个问题可能和OIDC库的状态管理、应用配置或者服务器端延迟有关。我使用的是angular-auth-oidc-client这个OIDC认证库。
我的OIDC配置如下:
@NgModule({ imports: [ AuthModule.forRoot({ config: { authority: environment.authConfig.authority, redirectUrl: window.location.origin, postLogoutRedirectUri: window.location.origin + '/sign-in', clientId: environment.authConfig.clientId, scope: environment.authConfig.scope, responseType: environment.authConfig.responseType, unauthorizedRoute: '/sign-in', silentRenew: true, useRefreshToken: true, startCheckSession: environment.platform === 'pwa', renewTimeBeforeTokenExpiresInSeconds: 20, configId: environment.configID, ignoreNonceAfterRefresh: true, }, }), ], providers: [{ provide: AbstractSecurityStorage, useClass: AuthStorage }], exports: [AuthModule], })
路由守卫中的认证检查代码:
我在路由守卫里每次页面切换时都会调用认证检查逻辑,代码如下:
return await this.zone.run(async () => { try { let result: LoginResponse; result = await firstValueFrom(this.oidcSecurityService.checkAuthIncludingServer()); if (result?.isAuthenticated) { await this.handleLoginResponse(result); return true; } if (result?.errorMessage) { this.notificationService.showError(result.errorMessage); this.authenticated = false; } } catch (error) { this.notificationService.showError('Login Error: ' + error); this.authenticated = false; return false; } });
排查情况:
我尝试在代码中打印result对象,发现某次result为null时触发了登出行为,但此时没有任何错误信息输出,导致我完全无法定位问题的根源。
内容的提问来源于stack exchange,提问作者Farrukh Faizy
相关产品推荐
相关产品推荐

