cjl
2023-11-10 c3883cee792651c5331635b367ea3daea43bf170
screen-common/src/main/java/com/moral/util/TokenUtils.java
@@ -76,6 +76,27 @@
                    ResponseCodeEnum.TOKEN_CREATE_ERROR.getMsg());
        }
    }
    public static String getTokenApp(String uid, Object userInfo) {
        //生成加密token
        try {
            //生成token
            String token = TokenEncryptUtils.encoded(uid + "/" + System.currentTimeMillis() / 1000);
            //查询旧的token
            String oldToken = (String) redisTemplate.opsForHash().get("user_token", uid);
            if (oldToken != null)
                redisTemplate.delete(oldToken);
            //新token写入到value中
            redisTemplate.opsForValue().set(token, userInfo);
            //redisTemplate.expire(token, validity_time, TimeUnit.SECONDS);
            //新token写入到Hash中
            redisTemplate.opsForHash().put("user_token", uid, token);
            return token;
        } catch (Exception e) {
            log.error("token生成异常:" + e.getMessage());
            throw new TokenException(ResponseCodeEnum.TOKEN_CREATE_ERROR.getCode(),
                    ResponseCodeEnum.TOKEN_CREATE_ERROR.getMsg());
        }
    }
    /**
@@ -85,7 +106,7 @@
     * @Author: 陈凯裕
     * @Date: 2021/3/10
     */
    public static void checkToken(String token) {
    public static boolean checkToken(String token) {
        try {
            String[] tokenArray = TokenEncryptUtils.decoded(token).split("/");
            //校验token是否合法
@@ -99,9 +120,9 @@
                        ResponseCodeEnum.TOKEN_INVALID.getMsg());
            }
        } catch (Exception e) {
            throw new TokenException(ResponseCodeEnum.TOKEN_INVALID.getCode(),
                    ResponseCodeEnum.TOKEN_INVALID.getMsg());
            return false;
        }
        return true;
    }
@@ -120,6 +141,11 @@
        return userInfo;
    }
    public static Object getUserInfo() {
        String token = getCurrentToken();
        return getUserInfoByToken(token);
    }
    /**
     * @Description: 销毁token
     * @Param: [uid, token]
@@ -128,6 +154,8 @@
     * @Date: 2021/3/11
     */
    public static void destoryToken(String uid, String token) {
        if(ObjectUtils.isEmpty(uid)||ObjectUtils.isEmpty(token))
            return;
        redisTemplate.delete(token);
        redisTemplate.opsForHash().delete("user_token", uid);
    }
@@ -144,6 +172,7 @@
        destoryToken(getUidByToken(token), token);
    }
    /**
     * @Description: 销毁当前连接的token
     * @Param: []
@@ -157,7 +186,7 @@
    public static void destoryToken(Integer id) {
        String token = getTokenById(id);
        destoryToken(token,String.valueOf(id));
        destoryToken(String.valueOf(id),token);
    }
    /**
@@ -168,7 +197,11 @@
     * @Date: 2021/5/21
     */
    public static String getTokenById(Integer id) {
         return (String)redisTemplate.opsForHash().get("user_token", String.valueOf(id));
         return (String) getTokenById(id.toString());
    }
    public static String getTokenById(String id) {
        return (String)redisTemplate.opsForHash().get("user_token", String.valueOf(id));
    }