| | |
| | | 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()); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | |
| | | * @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是否合法 |
| | |
| | | ResponseCodeEnum.TOKEN_INVALID.getMsg()); |
| | | } |
| | | } catch (Exception e) { |
| | | throw new TokenException(ResponseCodeEnum.TOKEN_INVALID.getCode(), |
| | | ResponseCodeEnum.TOKEN_INVALID.getMsg()); |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | |
| | |
| | | throw new TokenException(ResponseCodeEnum.TOKEN_INVALID.getCode(), |
| | | ResponseCodeEnum.TOKEN_INVALID.getMsg()); |
| | | return userInfo; |
| | | } |
| | | |
| | | public static Object getUserInfo() { |
| | | String token = getCurrentToken(); |
| | | return getUserInfoByToken(token); |
| | | } |
| | | |
| | | /** |
| | |
| | | destoryToken(getUidByToken(token), token); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @Description: 销毁当前连接的token |
| | | * @Param: [] |
| | |
| | | * @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)); |
| | | } |
| | | |
| | | |