Commit a134d96e96436e3c6b9f5e8191b3a2f672a740e8
1 parent
d8c9c10b
同一用户只能一处登录 注销改为通过key进行校验
Showing
2 changed files
with
63 additions
and
17 deletions
jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/controller/LoginController.java
... | ... | @@ -24,9 +24,11 @@ import org.jeecg.modules.system.entity.SysUser; |
24 | 24 | import org.jeecg.modules.system.model.SysLoginModel; |
25 | 25 | import org.jeecg.modules.system.service.*; |
26 | 26 | import org.jeecg.modules.system.util.RandImageUtil; |
27 | +import org.jeecg.modules.system.vo.SysUserOnlineVO; | |
27 | 28 | import org.jeecg.utils.StringUtils; |
28 | 29 | import org.springframework.beans.BeanUtils; |
29 | 30 | import org.springframework.beans.factory.annotation.Autowired; |
31 | +import org.springframework.data.redis.core.RedisTemplate; | |
30 | 32 | import org.springframework.web.bind.annotation.*; |
31 | 33 | |
32 | 34 | import javax.annotation.Resource; |
... | ... | @@ -56,6 +58,8 @@ public class LoginController { |
56 | 58 | @Autowired |
57 | 59 | private ISysTenantService sysTenantService; |
58 | 60 | @Autowired |
61 | + public RedisTemplate redisTemplate; | |
62 | + @Autowired | |
59 | 63 | private ISysDictService sysDictService; |
60 | 64 | @Resource |
61 | 65 | private BaseCommonService baseCommonService; |
... | ... | @@ -430,6 +434,32 @@ public class LoginController { |
430 | 434 | obj.put("tenantList", tenantList); |
431 | 435 | } |
432 | 436 | } |
437 | + | |
438 | + //删除相同用户名称对应的key | |
439 | + Collection<String> keys = redisTemplate.keys(CommonConstant.PREFIX_USER_TOKEN + "*"); | |
440 | + List<SysUserOnlineVO> onlineList = new ArrayList<SysUserOnlineVO>(); | |
441 | + for (String key : keys) { | |
442 | + String token = (String) redisUtil.get(key); | |
443 | + LoginUser loginUser = sysBaseAPI.getUserByName(JwtUtil.getUsername(token)); | |
444 | + if (loginUser != null) { | |
445 | + if(oConvertUtils.isNotEmpty(username) && loginUser.getUsername().contains(username)){ | |
446 | + log.info(" 强制 "+sysUser.getRealname()+"退出成功! "); | |
447 | + //清空用户登录Token缓存 | |
448 | + redisUtil.del(token); | |
449 | + redisUtil.del(CommonConstant.PREFIX_USER_TOKEN + token); | |
450 | + //清空用户登录Shiro权限缓存 | |
451 | + redisUtil.del(CommonConstant.PREFIX_USER_SHIRO_CACHE + sysUser.getId()); | |
452 | + //清空用户的缓存信息(包括部门信息),例如sys:cache:user::<username> | |
453 | + redisUtil.del(String.format("%s::%s", CacheConstant.SYS_USERS_CACHE, sysUser.getUsername())); | |
454 | + //调用shiro的logout | |
455 | + SecurityUtils.getSubject().logout(); | |
456 | + // | |
457 | + redisUtil.del(key); | |
458 | + } | |
459 | + } | |
460 | + } | |
461 | + | |
462 | + | |
433 | 463 | // update-end--Author:sunjianlei Date:20210802 for:获取用户租户信息 |
434 | 464 | // 生成token |
435 | 465 | String token = JwtUtil.sign(username, syspassword, warehouseCode); |
... | ... |
jeecg-boot-master/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/controller/SysUserOnlineController.java
... | ... | @@ -61,7 +61,7 @@ public class SysUserOnlineController { |
61 | 61 | String token = (String)redisUtil.get(key); |
62 | 62 | if (StringUtils.isNotEmpty(token)) { |
63 | 63 | SysUserOnlineVO online = new SysUserOnlineVO(); |
64 | - online.setToken(token); | |
64 | + online.setToken(key); | |
65 | 65 | //TODO 改成一次性查询 |
66 | 66 | LoginUser loginUser = sysBaseAPI.getUserByName(JwtUtil.getUsername(token)); |
67 | 67 | if (loginUser != null) { |
... | ... | @@ -112,22 +112,38 @@ public class SysUserOnlineController { |
112 | 112 | if(oConvertUtils.isEmpty(online.getToken())) { |
113 | 113 | return Result.error("退出登录失败!"); |
114 | 114 | } |
115 | - String username = JwtUtil.getUsername(online.getToken()); | |
116 | - LoginUser sysUser = sysBaseAPI.getUserByName(username); | |
117 | - if(sysUser!=null) { | |
118 | - baseCommonService.addLog("强制: "+sysUser.getRealname()+"退出成功!", CommonConstant.LOG_TYPE_1, null,sysUser); | |
119 | - log.info(" 强制 "+sysUser.getRealname()+"退出成功! "); | |
120 | - //清空用户登录Token缓存 | |
121 | - redisUtil.del(CommonConstant.PREFIX_USER_TOKEN + online.getToken()); | |
122 | - //清空用户登录Shiro权限缓存 | |
123 | - redisUtil.del(CommonConstant.PREFIX_USER_SHIRO_CACHE + sysUser.getId()); | |
124 | - //清空用户的缓存信息(包括部门信息),例如sys:cache:user::<username> | |
125 | - redisUtil.del(String.format("%s::%s", CacheConstant.SYS_USERS_CACHE, sysUser.getUsername())); | |
126 | - //调用shiro的logout | |
127 | - SecurityUtils.getSubject().logout(); | |
128 | - return Result.ok("退出登录成功!"); | |
129 | - }else { | |
130 | - return Result.error("Token无效!"); | |
115 | + Collection<String> keys = redisTemplate.keys(CommonConstant.PREFIX_USER_TOKEN + "*"); | |
116 | + List<SysUserOnlineVO> onlineList = new ArrayList<SysUserOnlineVO>(); | |
117 | + for (String key : keys) { | |
118 | + if (key.equals(online.getToken())) | |
119 | + { | |
120 | + String tokenValue = (String)redisUtil.get(online.getToken()); | |
121 | + String username = JwtUtil.getUsername(tokenValue); | |
122 | + LoginUser sysUser = sysBaseAPI.getUserByName(username); | |
123 | + if(sysUser!=null) { | |
124 | + //update-begin--Author:wangshuai Date:20200714 for:登出日志没有记录人员 | |
125 | + baseCommonService.addLog("用户名: "+sysUser.getRealname()+",退出成功!", CommonConstant.LOG_TYPE_1, null,sysUser); | |
126 | + //update-end--Author:wangshuai Date:20200714 for:登出日志没有记录人员 | |
127 | + log.info(" 用户名: "+sysUser.getRealname()+",退出成功! "); | |
128 | + | |
129 | + //以下两个清空测试无效 保留在这吧 | |
130 | + //清空用户登录Token缓存/用户登录Shiro权限缓存 | |
131 | + redisUtil.del(CommonConstant.PREFIX_USER_TOKEN + tokenValue); | |
132 | + redisUtil.del(CommonConstant.PREFIX_USER_SHIRO_CACHE + sysUser.getId()); | |
133 | + | |
134 | + //真*清空Token | |
135 | + redisUtil.del(key); | |
136 | + | |
137 | + //清空用户的缓存信息 | |
138 | + redisUtil.del(String.format("%s::%s", CacheConstant.SYS_USERS_CACHE, sysUser.getUsername())); | |
139 | + //调用shiro的logout | |
140 | + SecurityUtils.getSubject().logout(); | |
141 | + return Result.ok("退出登录成功!"); | |
142 | + }else { | |
143 | + return Result.error("Token无效!"); | |
144 | + } | |
145 | + } | |
131 | 146 | } |
147 | + return Result.error("Token无效!"); | |
132 | 148 | } |
133 | 149 | } |
... | ... |