Angular 5 + OAuth2:令牌没有设置libary [angular-oauth2-oidc]

时间:2018-01-21 16:55:03

标签: javascript oauth-2.0 angular5 oidc angular-oauth2-oidc

我正在尝试将我的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方法,我总是假的。此外,hasValidAccessToken()还有一个错误的返回。

有人能说明如何正确配置角度5和oauth2吗? 我还需要存储我给定的访问令牌,以便在我的rest方法中使用它们来获取数据。

1 个答案:

答案 0 :(得分:0)

需要在配置中添加JWK令牌验证程序。然后根据您的响应类型设置Jwks

this.oauthService.tokenValidationHandler = new JwksValidationHandler();
相关问题