javascript - Angular 5 + OAuth2 : Token not getting set with libary [angular-oauth2-oidc]

标签 javascript oauth-2.0 angular5 openid-connect 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());
  }

在我的主页组件中,我添加了一个按钮来触发隐式流并获取访问 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/

相关文章:

angular - 节点 + Angular 通用 SSR : how to set the device-width when rendering a page

javascript - Ionic 3 未在图像 src 中显示相机 FILE_URI 路径

javascript - 第一个选项卡面板不显示网格中的数据(IE 9)

javascript - 如何使用微数据提取/解析 HTML

javascript - jQuery 没有得到应用。 Javascript 工作正常

c# - 无法将类型为 'RSACng' 的对象转换为类型 'System.Security.Cryptography.RSACryptoServiceProvider'

javascript - 制作 Chrome 扩展时如何导入文件

javascript - 如何获取一个访问 token /刷新 token 以无限期使用(oauth2 - Spotify API)

asp.net-web-api - 看不懂MVC5 + WebApi2授权

angular - 类型 'element' .ts 上不存在属性 'Directive'