lizijie
2021-03-22 ebdacd7123e42c76cd7cb43938574cfeb0706250
screen-common/src/main/java/com/moral/util/TokenUtils.java
@@ -1,17 +1,16 @@
package com.moral.util;
import com.moral.constant.Constants;
import com.moral.constant.ResponseCodeEnum;
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;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
@@ -24,7 +23,6 @@
@Component
@Slf4j
public class TokenUtils {
    private static RedisTemplate redisTemplate;
    @Autowired
@@ -37,13 +35,13 @@
    private static final int validity_time = 60*30;
    /**
     * @Description: 生成token
     * @Param: [type, uid] type: 后台取值:manage  前台取值:api
     * @Description: 生成token,并且将用户信息存入缓存
     * @Param: [uid] type: 后台取值:manage  前台取值:api
     * @return: java.lang.String
     * @Author: 陈凯裕
     * @Date: 2021/3/10
     */
    public static String getToken(String uid, Map<String, Object> userInfo) {
    public static String getToken(String uid, Object userInfo) {
        //生成加密token
        try {
            //生成token
@@ -60,10 +58,11 @@
            return token;
        } catch (Exception e) {
            log.error("token生成异常:"+e.getMessage());
            throw new TokenException(Constants.CODE_TOKEN_CREATE_ERROR,Constants.MSG_TOKEN_CREATE_ERROR);
            throw new TokenException(ResponseCodeEnum.TOKEN_CREATE_ERROR.getCode(),
                    ResponseCodeEnum.TOKEN_CREATE_ERROR.getMsg());
        }
    }
    /**
     * @Description: 校验token
@@ -77,29 +76,33 @@
            String[] tokenArray = TokenEncryptUtils.decoded(token).split("/");
            //校验token是否合法
            if (tokenArray.length != 2) {
                throw new TokenException(Constants.CODE_TOKEN_ERROR,Constants.MSG_TOKEN_ERROR);
                throw new TokenException(ResponseCodeEnum.TOKEN_INVALID.getCode(),
                        ResponseCodeEnum.TOKEN_INVALID.getMsg());
            }
            //校验token是否过期
            if (!redisTemplate.hasKey(token)) {
                throw new TokenException(Constants.CODE_TOKEN_ERROR,Constants.MSG_TOKEN_ERROR);
                throw new TokenException(ResponseCodeEnum.TOKEN_INVALID.getCode(),
                        ResponseCodeEnum.TOKEN_INVALID.getMsg());
            }
        } catch (Exception e) {
            log.error("token工具类校验token异常" + e.getMessage());
            throw new TokenException(Constants.CODE_TOKEN_ERROR,Constants.MSG_TOKEN_ERROR);
            throw new TokenException(ResponseCodeEnum.TOKEN_INVALID.getCode(),
                    ResponseCodeEnum.TOKEN_INVALID.getMsg());
        }
    }
    /**
     * @Description: 通过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);
    public static Object getUserInfoByToken(String token) {
        Object userInfo = redisTemplate.opsForValue().get(token);
        if(userInfo==null)
            throw new TokenException(Constants.CODE_TOKEN_ERROR,Constants.MSG_TOKEN_ERROR);
            throw new TokenException(ResponseCodeEnum.TOKEN_INVALID.getCode(),
                    ResponseCodeEnum.TOKEN_INVALID.getMsg());
        return userInfo;
    }
@@ -111,17 +114,17 @@
     * @Date: 2021/3/11
     */
    public static void destoryToken(String uid, String token) {
        redisTemplate.delete("token");
        redisTemplate.delete(token);
        redisTemplate.opsForHash().delete("user_token", uid);
    }
    /**
    * @Description: token延长
            * @Param: [token]
            * @return: void
            * @Author: 陈凯裕
            * @Date: 2021/3/11
            */
     * @Description: token延长
     * @Param: [token]
     * @return: void
     * @Author: 陈凯裕
     * @Date: 2021/3/11
     */
    public static void extendTokenTime(String token)  {
        redisTemplate.expire(token, validity_time, TimeUnit.SECONDS);
    }