Jer*_*oen 4 typescript identityserver4 angular angular-oauth2-oidc
我将angular-oauth2-oidc库与隐式流(带有 IdentityServer4 服务器)结合使用。我已经成功地从 docs设置了Silent Refresh 建议。
以下是我在包装服务中引导事物的方式:
private userSubject = new Subject<User>();
constructor(private config: ConfigService, private oAuthService: OAuthService)
{ }
// Called on app load:
configure(): void {
const config: AuthConfig = {
issuer: this.config.getIdentityUrl(),
logoutUrl: this.config.getIdentityUrl() + '/connect/endsession',
redirectUri: window.location.origin + '/',
silentRefreshRedirectUri: window.location.origin + '/silent-refresh.html',
clientId: 'my_client_id',
scope: 'openid profile my_api',
sessionChecksEnabled: true,
};
this.oAuthService.configure(config);
this.oAuthService.tokenValidationHandler = new JwksValidationHandler();
this.oAuthService
.loadDiscoveryDocumentAndLogin()
.then((_) => this.loadUserProfile());
this.oAuthService.setupAutomaticSilentRefresh();
}
private loadUserProfile() {
this.oAuthService.loadUserProfile()
.then((userProfile) => {
const user = new User(userProfile['name']);
this.userSubject.next(user);
});
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我在新选项卡中打开应用程序,用户也会被重定向到 IdentityServer(并立即返回到我的应用程序)。
我的问题:我可以让库从同一来源的其他选项卡中检索现有的访问令牌(以及可选的用户信息),以防止重定向?(首选,因为它不需要 Ajax 调用。)
或者,在我们将某人发送到 IdentityServer 之前,是否有一种简单的方法可以尝试使用静默刷新机制?
首先:我以某种方式得到了sessionStorage适合代币的想法,localStorage应该避免这种想法。但那是来自另一个涉及更强大(刷新)令牌的项目,并且通过隐式流,我只有短暂的访问令牌,因此localStorage并不比sessionStorage.
我没有提到我有这个想法,另一个答案让我重新思考并考虑使用 localStorage。这实际上是一个很好的解决方案。
事实证明,该库已内置支持localStorage用作令牌和其他数据的后备存储。起初虽然我在尝试:
// This doesn't work properly though, read on...
this.oAuthService.setStorage(localStorage);
Run Code Online (Sandbox Code Playgroud)
但是这种引导方式对我的情况不起作用,请参阅库 GitHub 问题列表上的问题 321 以获取我的日志。从该线程重复解决方案(或解决方法?),我通过在应用程序模块中执行此操作来解决问题providers:
{ provide: OAuthStorage, useValue: localStorage },
Run Code Online (Sandbox Code Playgroud)
现在库将正确使用 localStorage 并且新选项卡(甚至新窗口)将自动选择它。
作为脚注,如果localStorage出于安全原因您不想使用,您也可以提供自己的存储,只要它实现了OAuthStorage接口。然后,您自己的实现可以使用任何可用的选项卡间通信技术来“询问”来自其他选项卡的数据,并在需要时回退到 sessionStorage。
| 归档时间: |
|
| 查看次数: |
7904 次 |
| 最近记录: |