0

我正在尝试遵循文档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);

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

4

2 回答 2

0

原来我没有正确地做到这一点。

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

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

于 2020-05-17T12:28:16.740 回答
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,
};

文件 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();
      }
    });
}
于 2021-05-19T16:54:14.577 回答