登录后,angular-oauth2-oidc令牌始终无效

时间:2020-05-08 10:19:06

标签: angular oauth-2.0 angular-oauth2-oidc

我正在尝试遵循文档https://github.com/manfredsteyer/angular-oauth2-oidc上的指南。

我在服务的构造函数中具有以下配置:

authCodeFlowConfig: AuthConfig = {
   issuer: 'https://demo.identityserver.io',
   redirectUri: window.location.origin + '/home',
   clientId: 'spa',
   responseType: 'code',
   scope: 'openid profile email offline_access api',
   showDebugInformation: true
 };

this.oauthService.configure(this.authCodeFlowConfig);

(我也尝试过使用index.html的重定向URL,但这似乎没有什么作用)

登录似乎可以正常工作,我将重定向到页面登录并重定向到首页:

    this.oauthService.loadDiscoveryDocumentAndTryLogin().then((response) => {
      if (!this.oauthService.hasValidIdToken() || !this.oauthService.hasValidAccessToken()) {
         this.oauthService.initCodeFlow();
      }
    }).catch((err) => {
      console.log(err);
    });

但是以下内容仍返回false,false,null:

    const claims = this.oauthService.getIdentityClaims();
    console.log(this.oauthService.hasValidIdToken());
    console.log(this.oauthService.hasValidAccessToken());
    console.log(claims);

我的网址中有一个code =,对该服务没有其他更改,一切似乎都已使我登录。 我期望做的事情很愚蠢或被误解了,但是任何建议都会有所帮助。

2 个答案:

答案 0 :(得分:0)

结果是我没有正确执行。

我没有像我应该做的那样在构造函数中调用负载发现文档。

如果页面重新加载,我将没有发现文档来检查我的令牌,因此它是无效的。

答案 1 :(得分:0)

我有同样的问题,我解决了:

文件 config.ts:

import { AuthConfig } from 'angular-oauth2-oidc';

export const authCodeFlowConfig: AuthConfig = {
    issuer: 'https://accounts.----/auth/realms/---',
    redirectUri: window.location.origin + '/login',
    clientId: 'my-project',
    responseType: 'code',
    scope: 'openid profile email offline_access',
    showDebugInformation: true,
};

文件登录.ts:

ngOnInit(): void {
    this.oauthService.configure(authCodeFlowConfig);
    this.oauthService.tokenValidationHandler = new JwksValidationHandler();
    this.oauthService.setupAutomaticSilentRefresh();
    this.oauthService.loadDiscoveryDocumentAndTryLogin().then(() => {
      if (this.oauthService.hasValidAccessToken()) {
        // Load UserProfile to get the additional claims
        this.oauthService.loadUserProfile();
        this.router.navigateByUrl('/');
      } else {
        this.oauthService.initCodeFlow();
      }
    });
}