AngularJS - 处理刷新令牌? [英] AngularJS - Handling refresh token?

查看:952
本文介绍了AngularJS - 处理刷新令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立与通信AngularJS的水疗中心,以服务(JAVA)。

I'm building a SPA with AngularJS with communication to a service (JAVA).

当用户发送自己的用户名/密码,服务发回两个:ACCES令牌并刷新令牌。我试图处理:如果我得到响应状态为401,发回刷新令牌,然后重新发送您的最后一个请求。我试图做到这一点与包括$ HTTP,但角度并不让我包括在这个拦截。有什么办法来重新与我recieving这个响应参数的原始请求?

When user sends his username/pass, service sends back both: Acces token and Refresh token. I'm trying to handle: if I get response with status 401, send back refresh token and then send your last request again. I tried to do that with including $http, but angular doesn't let me include it in this interceptor. Is there any way to recreate the original request with this response parameter I'm recieving?

是这样的:


  1. 我得到401

  2. 保存我的要求

  3. 如果我有一个刷新令牌发送刷新标记

  4. 在成功重新发送我的要求

  5. 这是错误重定向到/登录页

  1. I get 401
  2. save my request
  3. if I have a refresh token send that refresh token
  4. on success resend my request
  5. on error redirect to /login page

'use strict';

angular.module('testApp')
    .factory('authentificationFactory', function($rootScope, $q, $window, $location, CONF) {

return {
    request: function(config) {
        config.headers = config.headers || {};
        if ($window.sessionStorage.token) {
            config.headers.Authorization = 'Bearer ' + $window.sessionStorage.token;
        }
        console.log(config);
        $rootScope.lastRequest = config;
        return config;
    },

    response: function(response) {
        console.log($rootScope.lastRequest);
        if (response.status === 401) {
            if ($window.sessionStorage.refreshToken) {

                //Save, request new token, send old response
                //if it fails, go to login

                $location.url('/login');
            } else {
                $location.url('/login');
            }
        }
        return response || $q.when(response);
    }
};
});


奖金问题(主要问题是更重要):有2个移动应用程序,也将连接到我的服务,当我从我的web应用程序登录,过了一会儿,从我的移动应用,移动应用程序需要一新的刷新令牌和我的web应用程序的更新凭证是有效的没了。什么是处理,最好的选择?

Bonus Question (the main question is more important): There are 2 mobile apps that will also connect to my service, and when I log in from my web app, and few moments later from my mobile app, mobile app takes a new refresh token and my web app's refresh token is valid no more. What would be the best option for dealing with that?

感谢您的时间,
最好的问候

Thank you for your time, Best regards

推荐答案

有一个看看这个:<一href=\"https://github.com/witoldsz/angular-http-auth\">https://github.com/witoldsz/angular-http-auth.

他使用一个缓冲区来重播验证后的请求。

He uses a buffer to replay the requests after authentication.

这篇关于AngularJS - 处理刷新令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆