Commit 3855f12ceea90855a20486e08be8d0363a2433f5
1 parent
59562d9c
---author:scott---date:20220417-----for:【重要】shiro获取用户的逻辑抽共通 并 解决其他服务节点在system服务挂…
…了,调用其他服务不同问题(缓存调用换写法)---
Showing
2 changed files
with
25 additions
and
34 deletions
jeecg-boot/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/TokenUtils.java
... | ... | @@ -3,6 +3,7 @@ package org.jeecg.common.util; |
3 | 3 | import lombok.extern.slf4j.Slf4j; |
4 | 4 | import org.apache.commons.lang3.StringUtils; |
5 | 5 | import org.jeecg.common.api.CommonAPI; |
6 | +import org.jeecg.common.constant.CacheConstant; | |
6 | 7 | import org.jeecg.common.constant.CommonConstant; |
7 | 8 | import org.jeecg.common.exception.JeecgBoot401Exception; |
8 | 9 | import org.jeecg.common.system.util.JwtUtil; |
... | ... | @@ -38,31 +39,7 @@ public class TokenUtils { |
38 | 39 | public static boolean verifyToken(HttpServletRequest request, CommonAPI commonApi, RedisUtil redisUtil) { |
39 | 40 | log.debug(" -- url --" + request.getRequestURL()); |
40 | 41 | String token = getTokenByRequest(request); |
41 | - | |
42 | - if (StringUtils.isBlank(token)) { | |
43 | - throw new JeecgBoot401Exception("Token不能为空!"); | |
44 | - } | |
45 | - | |
46 | - // 解密获得username,用于和数据库进行对比 | |
47 | - String username = JwtUtil.getUsername(token); | |
48 | - if (username == null) { | |
49 | - throw new JeecgBoot401Exception("Token非法无效!"); | |
50 | - } | |
51 | - | |
52 | - // 查询用户信息 | |
53 | - LoginUser user = commonApi.getUserByName(username); | |
54 | - if (user == null) { | |
55 | - throw new JeecgBoot401Exception("用户不存在!"); | |
56 | - } | |
57 | - // 判断用户状态 | |
58 | - if (user.getStatus() != 1) { | |
59 | - throw new JeecgBoot401Exception("账号已锁定,请联系管理员!"); | |
60 | - } | |
61 | - // 校验token是否超时失效 & 或者账号密码是否错误 | |
62 | - if (!jwtTokenRefresh(token, username, user.getPassword(), redisUtil)) { | |
63 | - throw new JeecgBoot401Exception("Token失效,请重新登录"); | |
64 | - } | |
65 | - return true; | |
42 | + return TokenUtils.verifyToken(token, commonApi, redisUtil); | |
66 | 43 | } |
67 | 44 | |
68 | 45 | /** |
... | ... | @@ -80,7 +57,8 @@ public class TokenUtils { |
80 | 57 | } |
81 | 58 | |
82 | 59 | // 查询用户信息 |
83 | - LoginUser user = commonApi.getUserByName(username); | |
60 | + LoginUser user = TokenUtils.getLoginUser(username, commonApi, redisUtil); | |
61 | + //LoginUser user = commonApi.getUserByName(username); | |
84 | 62 | if (user == null) { |
85 | 63 | throw new JeecgBoot401Exception("用户不存在!"); |
86 | 64 | } |
... | ... | @@ -113,16 +91,27 @@ public class TokenUtils { |
113 | 91 | redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, newAuthorization); |
114 | 92 | redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME * 2 / 1000); |
115 | 93 | } |
116 | - //update-begin--Author:scott Date:20191005 for:解决每次请求,都重写redis中 token缓存问题 | |
117 | -// else { | |
118 | -// redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, cacheToken); | |
119 | -// // 设置超时时间 | |
120 | -// redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME / 1000); | |
121 | -// } | |
122 | - //update-end--Author:scott Date:20191005 for:解决每次请求,都重写redis中 token缓存问题 | |
123 | 94 | return true; |
124 | 95 | } |
125 | 96 | return false; |
126 | 97 | } |
127 | 98 | |
99 | + /** | |
100 | + * 获取登录用户 | |
101 | + * | |
102 | + * @param commonApi | |
103 | + * @param username | |
104 | + * @return | |
105 | + */ | |
106 | + public static LoginUser getLoginUser(String username, CommonAPI commonApi, RedisUtil redisUtil) { | |
107 | + LoginUser loginUser = null; | |
108 | + String loginUserKey = CacheConstant.SYS_USERS_CACHE + "::" + username; | |
109 | + if(redisUtil.hasKey(loginUserKey)){ | |
110 | + loginUser = (LoginUser) redisUtil.get(loginUserKey); | |
111 | + }else{ | |
112 | + // 查询用户信息 | |
113 | + loginUser = commonApi.getUserByName(username); | |
114 | + } | |
115 | + return loginUser; | |
116 | + } | |
128 | 117 | } |
... | ... |
jeecg-boot/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ShiroRealm.java
... | ... | @@ -15,6 +15,7 @@ import org.jeecg.common.system.util.JwtUtil; |
15 | 15 | import org.jeecg.common.system.vo.LoginUser; |
16 | 16 | import org.jeecg.common.util.RedisUtil; |
17 | 17 | import org.jeecg.common.util.SpringContextUtils; |
18 | +import org.jeecg.common.util.TokenUtils; | |
18 | 19 | import org.jeecg.common.util.oConvertUtils; |
19 | 20 | import org.jeecg.config.mybatis.TenantContext; |
20 | 21 | import org.springframework.context.annotation.Lazy; |
... | ... | @@ -122,7 +123,8 @@ public class ShiroRealm extends AuthorizingRealm { |
122 | 123 | |
123 | 124 | // 查询用户信息 |
124 | 125 | log.debug("———校验token是否有效————checkUserTokenIsEffect——————— "+ token); |
125 | - LoginUser loginUser = commonApi.getUserByName(username); | |
126 | + LoginUser loginUser = TokenUtils.getLoginUser(username,commonApi,redisUtil); | |
127 | + //LoginUser loginUser = commonApi.getUserByName(username); | |
126 | 128 | if (loginUser == null) { |
127 | 129 | throw new AuthenticationException("用户不存在!"); |
128 | 130 | } |
... | ... |