我正在尝试将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());
}
在我的家庭组件中,我添加了一个按钮以触发隐式流程并获取访问令牌。
在隐式流初始化之后,应用程序重定向到我登录的提供商的正确登录页面,并重定向到我的OAuth配置的redirectUri。
BUT
[例如,如果我尝试获取状态,则调用isLoggedIn方法,则始终为false。此外,hasValidAccessToken()处返回错误。
有人可以显示如何正确配置angular 5和oauth2吗?我还需要存储给定的访问令牌,以便在我的其余方法中使用它们来获取数据。
需要在您的配置中添加JWK令牌验证器。并根据您的响应类型设置Jwks
this.oauthService.tokenValidationHandler = new JwksValidationHandler();