美文网首页
JWT的TOKEN续期功能

JWT的TOKEN续期功能

作者: 爱余星痕 | 来源:发表于2019-02-11 13:15 被阅读0次

JWT里有一个关键的东东,就是续期TOKEN,即TOKEN快过期时,刷新一个新的TOKEN给客户端.
办法如下:
1.后端生成TOKEN


import com.starmark.core.shiro.model.SecurityUser;
import com.starmark.core.shiro.model.UserLoginToken;
import com.starmark.core.shiro.util.JWTUtil;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.AuthenticatingFilter;
import org.apache.shiro.web.util.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Objects;

public class JwtAuthFilter extends AuthenticatingFilter {
    private final Logger log = LoggerFactory.getLogger(JwtAuthFilter.class);
    //10分钟后刷新token
    private static final int tokenRefreshInterval = 60 * 10;

    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest httpServletRequest = WebUtils.toHttp(request);
        if (httpServletRequest.getMethod().equals(RequestMethod.OPTIONS.name())) //对于OPTION请求做拦截,不做token校验
            return false;

        return super.preHandle(request, response);
    }

    @Override
    protected void postHandle(ServletRequest request, ServletResponse response) {
        request.setAttribute("jwtShiroFilter.FILTERED", true);
    }

    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        if (this.isLoginRequest(request, response)) {
            return true;
        }
        Boolean afterFiltered = (Boolean) (request.getAttribute("jwtShiroFilter.FILTERED"));
        if (BooleanUtils.isTrue(afterFiltered))
            return true;

        boolean allowed = false;
        try {
            allowed = executeLogin(request, response);
        } catch (IllegalStateException e) { //not found any token
            log.error("Not found any token");
        } catch (Exception e) {
            log.error("Error occurs when login", e);
        }
        return allowed || super.isPermissive(mappedValue);
    }

    @Override
    protected AuthenticationToken createToken(ServletRequest servletRequest, ServletResponse servletResponse) {
        String jwtToken = getAuthzHeader(servletRequest);
        if (StringUtils.isNotBlank(jwtToken) && !JWTUtil.isTokenExpired(jwtToken))
            return UserLoginToken.buildPassword(jwtToken, null, "jwt");

        return null;
    }

    @Override
    protected boolean onAccessDenied(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
        HttpServletResponse httpResponse = WebUtils.toHttp(servletResponse);
        httpResponse.sendRedirect("/unauth");


        return false;
    }

    @Override
    protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) {
        HttpServletResponse httpResponse = WebUtils.toHttp(response);
    
        if (token instanceof UserLoginToken && "jwt".equalsIgnoreCase(((UserLoginToken) token).getLoginType())) {
            UserLoginToken jwtToken = (UserLoginToken) token;
            boolean shouldRefresh = shouldTokenRefresh(Objects.requireNonNull(JWTUtil.getIssuedAt(jwtToken.getUsername())));
            if (shouldRefresh) {
                //生成新的TOKEN
                SecurityUser user = (SecurityUser) subject.getPrincipal();
                String newToken = JWTUtil.sign(user.getUserInfo().getId());
                httpResponse.setHeader("x-auth-token", newToken);
            }
        }


        return true;
    }

    @Override
    protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) {
        log.error("Validate token fail, token:{}, error:{}", token.toString(), e.getMessage());
        return false;
    }

    /**
     * 获取TOKEN
     * @param request 请求
     * @return token
     */
    private String getAuthzHeader(ServletRequest request) {
        HttpServletRequest httpRequest = WebUtils.toHttp(request);
        String header = httpRequest.getHeader("x-auth-token");
        return StringUtils.removeStart(header, "Bearer ");
    }

    /**
     * 判断是否需要刷新TOKEN
     * @param issueAt token签发日期
     * @return 是否需要刷新TOKEN
     */
    private boolean shouldTokenRefresh(Date issueAt) {
        LocalDateTime issueTime = LocalDateTime.ofInstant(issueAt.toInstant(), ZoneId.systemDefault());
        return LocalDateTime.now().minusSeconds(tokenRefreshInterval).isAfter(issueTime);
    }


}

原签发TOKEN后10分钟后刷新新的TOKEN

2.前端获取TOKEN

// 拦截响应response,并做一些错误处理
axios.interceptors.response.use((response) => {
    if(response.status ===200 && response.data && response.data.code === 401) {
        //console.log(window.location.origin);

        window.location.href=window.location.origin+window.location.pathname+'#/login';
    }

    //获取返回的TOKEN
    const token=response.headers['x-auth-token'];

    if(token) {
        //将续期的TOKEN存起来
        localStorage.setItem("token",token) ;
    }
    // 这里是填写处理信息
    return response;
}, (err) => { // 这里是返回状态码不为200时候的错误处理
    console.log(err);
    if(err && err.response) {
        switch(err.response.data.code) {
            case 400:
                err.message = '请求错误';
                break;

            case 401:
                err.message = '未授权,请登录';
                break;

            case 403:
                err.message = '无权限';
                break;

            case 404:
                err.message = `请求地址出错: ${err.response.config.url}`;
                break;

            case 408:
                err.message = '请求超时';
                break;

            case 500:
                err.message = '服务器内部错误';
                break;

            case 501:
                err.message = '服务未实现';
                break;

            case 502:
                err.message = '网关错误';
                break;

            case 503:
                err.message = '服务不可用';
                break;

            case 504:
                err.message = '网关超时';
                break;

            case 505:
                err.message = 'HTTP版本不受支持';
                break;

            default:
        }
    }
    Vue.prototype.$message.error(err.response.data.msg!=null?err.response.data.msg:err.message);
    return Promise.reject(err)
});

注意一点,需要通过过滤器调整FITLER,增加Access-Control-Expose-Headers的输出,否则无法获取response中的header.

至此,JWT的TOKEN续期功能完成.

相关文章

  • JWT的TOKEN续期功能

    JWT里有一个关键的东东,就是续期TOKEN,即TOKEN快过期时,刷新一个新的TOKEN给客户端.办法如下:1....

  • Django 前后端分离 JWT 登录

    Django REST framework JWT 和登录功能实现前后分离token的方式做登录身份校验,jwt校...

  • JWT 认证过程

    JWT Token Structure A JWT token contains a Header, a Payl...

  • jwt 在微服务中应用

    jwt token jwt 在api 方式中表中的token生成,验证以及获取jwt解密后携带的用户信息 jwt ...

  • django websocket使用JWT登陆验证

    使用channels实现websocket,利用restframework-jwt功能进行token验证。流程如下...

  • 利用JWT生成Token

    开篇 实现Token的方式有很多,本篇介绍的是利用Json Web Token(JWT)生成的Token.JWT生...

  • 从session、cookie到token以及JWT

    从session、cookie到token以及JWT 主要讲token和jwt技术,关于session和cooki...

  • JWT token 介绍

    JWT token 介绍 JSON Web Token (JWT) 是一个开放标准 (RFC 7519),它定义了...

  • Token和Jwt存在什么区别

    token和jwt存在什么区别 token需要查库验证token 是否有效,而JWT不用查库或者少查库,直接在服务...

  • JWT实现登陆认证及Token续期

    过去这段时间主要负责了项目中的用户管理模块,用户管理模块会涉及到加密及认证流程,加密已经在前面的文章中介绍了,可以...

网友评论

      本文标题:JWT的TOKEN续期功能

      本文链接:https://www.haomeiwen.com/subject/ukkqeqtx.html