jwt只能用ajax么,使用JWT进行登录认证的一些问题

我的做法是这样的:

在拦截器中每次校验token的过期时间,如果token快要过期(设置一个合理的时间,防止页面多个ajax请求导致现有token过期,比如三分钟。此处我个人猜测跟微信的那个token类似,就是每次你获取新的token后,旧的的token还有一定的缓冲期),则颁发新的token。

前端我这边用的angularJs,前端http拦截器中获取每次检测是否颁发了新的token,如果颁发了的话,则替换旧的。

检测token时间过期比较简单,后端代码就不放了

前端代码如下:

.factory('authInterceptor', function($q, $rootScope, userService) {

return {

request : function(config) {

config.headers = config.headers || {};

//设置认证token

config.headers.Authorization = userService.getCode();

return config;

},

response : function(response) {

if(response.status == 401 || (response.data && response.data.code == 401)) {

$rootScope.$emit('userIntercepted', 'notLogin', response);

return;

}

//检测到新的token,则替换新的token

if(response.headers('token')) {

userService.putCode(response.headers('token'));

}

return response;

},

responseError : function(rejection) {

if(rejection.status == 401 || (rejection.data && rejection.data.code == 401)) {

$rootScope.$emit('userIntercepted', 'notLogin', rejection);

return;

}

return $q.reject(rejection);

}

}

})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值