简体   繁体   English

在AngularJS SPA中使用刷新令牌

[英]Using refresh tokens within AngularJS SPA

What is the correct way to request a new JWT authentication token via refresh tokens within AngularJS? 通过AngularJS中的刷新令牌请求新的JWT身份验证令牌的正确方法是什么?

I already have an implementation that, on every API request, checks whether the session needs refreshing and, if so, requests a new token from the webserver. 我已经有一个实现,可以在每个API请求中检查会话是否需要刷新,如果需要,则从Web服务器请求一个新令牌。 But, if a page makes 3 calls at once it requests a new refresh token for each call which seems incorrect - I would think it should only update the token once. 但是,如果一个页面一次发出3次调用,那么它为每个调用请求一个新的刷新令牌,这似乎是不正确的-我认为它应该只将令牌更新一次。

Is there a way to block other calls or should I put the refresh on an interval and not do it via interceptors? 有没有办法阻止其他呼叫,或者我应该将刷新间隔放置一个时间,而不应该通过拦截器来执行?

Interceptor request method 拦截器请求方法

request = (config: angular.IRequestConfig) => {

    this.setBearerToken(config);

    var responsePromise = this.$q.when(config);

    if (config.url !== this.tokenUrl) {

        const factSession = this.$injector.get("factSession") as Session;

        if (factSession.shouldRefreshToken()) {
            responsePromise = factSession
                .refreshSession()
                .then(() => {
                    this.setBearerToken(config);

                    return config;
                });
        }
    }

    return responsePromise;
}

Just enhance your refreshSession method, so it wont refresh several times. 只要增强您的refreshSession方法,它就不会刷新几次。

var refreshPromise;

refreshSession: () => {
  if (!refreshPromise) {
    refreshPromise = $http.get(...);
    refreshPromise.finally(() => refreshPromise = null);
  }

  return refreshPromise;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM