首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >登录后,使用angular-oauth2-oidc时,hasValidAccessToken始终为true

登录后,使用angular-oauth2-oidc时,hasValidAccessToken始终为true
EN

Stack Overflow用户
提问于 2021-09-04 18:34:57
回答 1查看 398关注 0票数 1

我已经用Angular 12.2实现了angular-oauth-oidc (版本12.1.0)。我使用Keycloak作为SSO登录没有问题,但我在库中有一个奇怪的行为。

每次登录后,即使令牌过期,方法hasValidIdTokenhasValidAccessToken也会返回true。似乎库没有验证令牌的过期时间。

我的配置文件如下:

代码语言:javascript
复制
export const authCodeFlowConfig: AuthConfig = {
    // Url of the Identity Provider
    issuer: 'http://localhost:8080/auth/realms/realmname',

    // URL of the SPA to redirect the user to after login
    redirectUri: window.location.origin + '/landing',

    // The SPA's id. The SPA is registerd with this id at the auth-server
    clientId: 'client-name',

    responseType: 'code',

    // set the scope for the permissions the client should request
    // The first four are defined by OIDC.
    // Important: Request offline_access to get a refresh token
    // The api scope is a usecase specific one
    scope: 'openid profile email',

    showDebugInformation: true,
  };

我用以下代码初始化库:

代码语言:javascript
复制
ngOnInit(): void {
    this.oauthService.configure(authCodeFlowConfig);
    this.oauthService.loadDiscoveryDocumentAndTryLogin().then(() => {
      if (this.oauthService.hasValidAccessToken()) {
        this.router.navigate(['home']);
      }
      else {
        this.oauthService.initCodeFlow();
      }
    });
  }

最后,我在guard中使用以下条件检查令牌是否有效:

代码语言:javascript
复制
canActivate(
    route: ActivatedRouteSnapshot, 
    state: RouterStateSnapshot) {
        var hasIdToken = this.oauthService.hasValidIdToken();
        var hasAccessToken = this.oauthService.hasValidAccessToken();
        var hasAccess = (hasIdToken && hasAccessToken)
            
        if (!hasAccess) {
            console.log('AuthGuard: Token not valid');
            this.router.navigate(['landing']);
        }
        else {
            console.log('AuthGuard: Valid token');
        }
            
        return hasAccess;
}

如果我以前登录过,这个方法总是返回true。在没有事先登录的情况下,它会按预期返回false

如果我使用其他库进行令牌验证,则在验证过期令牌时会得到预期的结果。

EN

回答 1

Stack Overflow用户

发布于 2021-11-20 17:56:44

您发布的ngOnInit将是异步的,可能需要150ms才能完成。最有可能的是,你的浏览器在初始化运行之前同步地触发了守卫,因此将返回访问令牌上的存储中的旧状态。

您需要执行以下任一操作:

之前,应用程序中没有运行任何内容;或者使用

你可以通过快速更改你自己的设置延迟2.5秒来检查我的怀疑是否正确,这样我们就可以合理地确定初始化已经完全完成:

代码语言:javascript
复制
canActivate(
    route: ActivatedRouteSnapshot, 
    state: RouterStateSnapshot) {
        var hasIdToken = this.oauthService.hasValidIdToken();
        var hasAccessToken = this.oauthService.hasValidAccessToken();
        var hasAccess = (hasIdToken && hasAccessToken)
                   
        return new Promise((resolve) => {
          setTimeout(() => {
            if (!hasAccess) {
              console.log('AuthGuard: Token not valid');
              this.router.navigate(['landing']);
            } else {
              console.log('AuthGuard: Valid token');
            }
            resolve(hasAccess);
          }, 2500);
        );
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69058026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档