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);

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

原来我做得不对。

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

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

我遇到了同样的问题,我解决了如下所示:

文件配置:

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();
}
});
}

我们可以在Auth配置设置中编写:StopIssuer

最新更新