JWT入门1 (4)

3.验证token

/**
 * @param token
 * @return
 * @Description token验证
 * @date 2017年12月18日下午5:21:20
 */
public boolean verifyTokenRedis(String token, HttpServletRequest request) {
    boolean flag = false;

    UserToken user = JWT.unsign(token, UserToken.class);

    if (null == user || null == user.getId()) {
        return flag;
    }
    String userAgent = request.getHeader("deviceFlag");
    logger.info("RedisUtils.verifyTokenRedis获取的user-agent:" + userAgent);
    Boolean isApp = VerifyUtils.isLoginByApp(userAgent);
    logger.info("RedisUtils.verifyTokenRedis的isApp:" + isApp);
    UserToken ut = (UserToken) redisTemplate.opsForValue().get(user.getType() + Long.toString(user.getId()));
    if (null != ut && null != ut.getToken() && ut.getToken().equals(token)) {
        String url = request.getServletPath();
        request.setAttribute(BaseConstant.LOGINPERSONID, user.getId());
        logger.info(BaseConstant.LOGINPERSONID + user.getId());
        logger.info(url);
        if (isApp) {
            this.saveRedisForApp(user.getType() + Long.toString(user.getId()), ut);
        } else {
            saveRedis(user.getType() + Long.toString(user.getId()), ut);
        }
        flag = true;
    }

    return flag;
}
 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwxydz.html