kaiyu
2021-03-11 d21e551b42746e5c689c96e584042e418083ff9b
screen-common/src/main/java/com/moral/util/TokenUtils.java
@@ -1,5 +1,8 @@
package com.moral.util;
import com.moral.constant.Constants;
import com.moral.exception.TokenException;
import com.sun.org.apache.bcel.internal.classfile.ConstantString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -31,13 +34,7 @@
    }
    //token有效期 单位:秒
    private static final int validity_time = 60*60*24*7;
    //token非法,生成错误
    public static final int error = -1;
    //token过期
    public static final int timeout = -2;
    //token有效,生成成功
    public static final int valid = 1;
    private static final int validity_time = 60*30;
    /**
     * @Description: 生成token
@@ -46,8 +43,7 @@
     * @Author: 陈凯裕
     * @Date: 2021/3/10
     */
    public static Map<String, Object> getToken(String uid, Map<String, Object> userInfo) {
        Map<String, Object> result = new HashMap<>();
    public static String getToken(String uid, Map<String, Object> userInfo) {
        //生成加密token
        try {
            //生成token
@@ -61,15 +57,12 @@
            redisTemplate.expire(token, validity_time, TimeUnit.SECONDS);
            //新token写入到Hash中
            redisTemplate.opsForHash().put("user_token",uid,token);
            result.put("code", valid);
            result.put("token",token);
            return token;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            result.put("code",error);
            log.error("token生成异常:"+e.getMessage());
            throw new TokenException(Constants.CODE_TOKEN_CREATE_ERROR,Constants.MSG_TOKEN_CREATE_ERROR);
        }
        return result;
    }
    /**
@@ -79,36 +72,57 @@
     * @Author: 陈凯裕
     * @Date: 2021/3/10
     */
    public static Map<String, Object> checkToken( String token) {
        Map<String, Object> result = new HashMap<>();
    public static void checkToken(String token) {
        try {
            String[] tokenArray = TokenEncryptUtils.decoded(token).split("/");
            //校验token是否合法
            if (tokenArray.length != 2) {
                result.put("code", error);
                result.put("msg", "无效的token");
                return result;
                throw new TokenException(Constants.CODE_TOKEN_ERROR,Constants.MSG_TOKEN_ERROR);
            }
            //校验token是否过期
            int tokenTime = Integer.parseInt(tokenArray[1]);
            if ((System.currentTimeMillis() / 1000) - tokenTime > validity_time) {
                result.put("code", timeout);
                result.put("msg", "登陆身份已过期,请重新登陆");
                return result;
            if (!redisTemplate.hasKey(token)) {
                throw new TokenException(Constants.CODE_TOKEN_ERROR,Constants.MSG_TOKEN_ERROR);
            }
            result.put("code", valid);
            return result;
        } catch (Exception e) {
            log.error("token工具类校验token异常" + e.getMessage());
            result.put("code", error);
            result.put("msg", "无效的token");
            return result;
            throw new TokenException(Constants.CODE_TOKEN_ERROR,Constants.MSG_TOKEN_ERROR);
        }
    }
    //通过token获取用户信息
    /**
     * @Description: 通过token获取用户信息
     * @Param: [token]
     * @return: java.util.Map<java.lang.String   ,   java.lang.Object>
     * @Author: 陈凯裕
     * @Date: 2021/3/11
     */
    public static Map<String, Object> getUserInfoByToken(String token) {
        Map<String, Object> userInfo = (Map<String, Object>) redisTemplate.opsForValue().get(token);
        if(userInfo==null)
            throw new TokenException(Constants.CODE_TOKEN_ERROR,Constants.MSG_TOKEN_ERROR);
        return userInfo;
    }
    /**
     * @Description: 销毁token
     * @Param: [uid, token]
     * @return: void
     * @Author: 陈凯裕
     * @Date: 2021/3/11
     */
    public static void destoryToken(String uid, String token) {
        redisTemplate.delete("token");
        redisTemplate.opsForHash().delete("user_token", uid);
    }
    /**
    * @Description: token延长
            * @Param: [token]
            * @return: void
            * @Author: 陈凯裕
            * @Date: 2021/3/11
            */
    public static void extendTokenTime(String token)  {
        redisTemplate.expire(token, validity_time, TimeUnit.SECONDS);
    }
}