| | |
| | | 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; |
| | |
| | | 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()); |
| | | } |
| | | } |
| | | |
| | |
| | | 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()); |
| | | } |
| | | } |
| | | |
| | |
| | | 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; |
| | | } |
| | | |