我正在尝试将我的 Angular 应用程序配置为使用 OAuth2 库( angular-oauth2-oidc )。
在文件 auth.service.ts 中,我配置了 OAuthService:
this.oauthService.loginUrl = 'https://serverdomain.com/authorization/';
this.oauthService.redirectUri = 'http://localhost:4200/auth';
this.oauthService.clientId = '1111-2222-3333-4444-5555-6666-7777';
this.oauthService.setStorage(localStorage);
this.oauthService.requireHttps = false;
this.oauthService.responseType = 'token';
this.oauthService.tokenEndpoint = 'https://serverdomain.com/token/';
this.oauthService.oidc = false;
this.oauthService.issuer = 'https://serverdomain.com/authorization/';
this.oauthService.tokenValidationHandler = new JwksValidationHandler();
this.oauthService.requestAccessToken = true;
this.oauthService.showDebugInformation = true;
this.oauthService.scope = 'openid profile email';
this.oauthService.tryLogin({
onTokenReceived: context => {
console.log(context);
}
});
obtainAccessToken() {
this.oauthService.initImplicitFlow();
}
isLoggedIn() {
if (this.oauthService.getAccessToken() === null) {
return false;
}
return true;
}
logout() {
this.oauthService.logOut();
location.reload();
}
logAuthData() {
console.log(this.oauthService.hasValidAccessToken());
}
在我的主页组件中,我添加了一个按钮来触发隐式流并获取访问 token 。
隐式流初始化后,应用程序重定向到提供程序的正确登录页面,我在其中登录并重定向到我的 OAuth 配置的 redirectUri。
但
如果我尝试获取一个状态,例如我调用 isLoggedIn 方法,我总是得到 false。此外,在 hasValidAccessToken() 处有一个错误的返回。
有人可以展示如何正确配置 angular 5 和 oauth2 吗?
我还需要可以存储给定的访问 token ,以便在我的其余方法中使用它们来获取数据。
最佳答案
需要在您的配置中添加 JWKs token 验证器。并根据您的响应类型设置 Jwt
this.oauthService.tokenValidationHandler = new JwksValidationHandler();
关于javascript - Angular 5 + OAuth2 : Token not getting set with libary [angular-oauth2-oidc],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48369306/