angular-oauth2-oidc token 登录后总是无效

angular-oauth2-oidc token always invalid after log in

我正在尝试按照文档中的指南进行操作 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);
    });

然而下面仍然returns false, false, null:

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

我的 url 中有一个代码= 没有对服务进行其他更改,一切似乎都已登录。 我希望做一些愚蠢的事情或误解正在发生的事情,但任何建议都会有所帮助。

原来我没有做对。

我没有在我的构造函数中调用加载发现文档,这是我应该做的。

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

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

文件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,
};

文件login.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();
      }
    });
}