| | |
| | | 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; |
| | |
| | | } |
| | | |
| | | //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 |
| | |
| | | * @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 |
| | | String token = TokenEncryptUtils.encoded(uid + "/" + System.currentTimeMillis() / 1000); |
| | | //查询旧的token |
| | | String oldToken = (String) redisTemplate.opsForHash().get("user_token", uid); |
| | | if(oldToken!=null) |
| | | 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); |
| | | |
| | | result.put("code", valid); |
| | | result.put("token",token); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | log.error(e.getMessage()); |
| | | result.put("code",error); |
| | | redisTemplate.opsForHash().put("user_token", uid, token); |
| | | return token; |
| | | } catch (Exception e) { |
| | | log.error("token生成异常:"+e.getMessage()); |
| | | throw new TokenException(Constants.CODE_TOKEN_CREATE_ERROR,Constants.MSG_TOKEN_CREATE_ERROR); |
| | | } |
| | | return result; |
| | | |
| | | } |
| | | |
| | | /** |
| | | * @Description: 校验token |
| | | * @Param: [type, token] type: 后台取值:manage 前台取值:api |
| | | * @return: java.util.Map<java.lang.String , java.lang.Object> |
| | | * @return: java.util.Map<java.lang.String , java.lang.Object> |
| | | * @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); |
| | | } |
| | | } |