Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a5aef69

Browse files
2 parents 284476b + b73345c commit a5aef69

File tree

4 files changed

+155
-33
lines changed

4 files changed

+155
-33
lines changed

‎src/main/java/com/geekcattle/controller/console/AdminController.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.geekcattle.controller.console;
22

3+
import com.geekcattle.core.shiro.AdminShiroRealm;
34
import com.geekcattle.model.console.Admin;
45
import com.geekcattle.model.console.AdminRole;
56
import com.geekcattle.model.console.Role;
@@ -46,6 +47,9 @@ public class AdminController {
4647
@Autowired
4748
private RoleService roleService;
4849

50+
@Autowired
51+
private AdminShiroRealm adminShiroRealm;
52+
4953
@RequiresPermissions("admin:index")
5054
@RequestMapping(value = "/index", method = {RequestMethod.GET})
5155
public String index(Model model) {
@@ -69,7 +73,7 @@ public String from(Admin admin, Model model) {
6973
}
7074
checkRoleId = String.join(",", checkRoleIds);
7175
}
72-
}else {
76+
}else {
7377
admin.setIsSystem(0);
7478
}
7579
model.addAttribute("checkRoleId", checkRoleId);
@@ -107,7 +111,7 @@ public ModelMap list(Admin admin) {
107111
public ModelMap save(@Valid Admin admin, BindingResult result) {
108112
try {
109113
if (result.hasErrors()) {
110-
for (ObjectError er : result.getAllErrors()){
114+
for (ObjectError er : result.getAllErrors()){
111115
return ReturnUtil.error(er.getDefaultMessage(), null, null);
112116
}
113117
}
@@ -155,10 +159,13 @@ public ModelMap save(@Valid Admin admin, BindingResult result) {
155159
adminRole.setRoleId(roleid);
156160
adminRoleService.insert(adminRole);
157161
}
158-
}else{
162+
}else{
159163
adminRoleService.deleteAdminId(admin.getUid());
160164
}
161165

166+
// 更新用户权限
167+
adminShiroRealm.kickOutUser(admin.getUid(), false);
168+
162169
return ReturnUtil.success("操作成功", null, "/console/admin/index");
163170
} catch (Exception e) {
164171
e.printStackTrace();
@@ -204,6 +211,7 @@ public ModelMap delete(String[] ids) {
204211
for (String id : ids) {
205212
adminRoleService.deleteAdminId(id);
206213
adminService.deleteById(id);
214+
adminShiroRealm.kickOutUser(id, true);
207215
}
208216
}
209217
return ReturnUtil.success("删除成功", null, null);

‎src/main/java/com/geekcattle/controller/console/RoleController.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.geekcattle.controller.console;
22

3+
import com.geekcattle.core.shiro.AdminShiroRealm;
34
import com.geekcattle.model.console.*;
45
import com.geekcattle.service.console.AdminRoleService;
56
import com.geekcattle.service.console.MenuService;
@@ -48,6 +49,9 @@ public class RoleController {
4849
@Autowired
4950
private RoleMenuService roleMenuService;
5051

52+
@Autowired
53+
private AdminShiroRealm adminShiroRealm;
54+
5155
@RequiresPermissions("role:index")
5256
@RequestMapping(value = "/index", method = {RequestMethod.GET})
5357
public String index(Model model) {
@@ -99,6 +103,8 @@ public ModelMap save(@Valid Role role, BindingResult result) {
99103
role.setUpdatedAt(DateUtil.getCurrentTime());
100104
roleService.save(role);
101105
}
106+
// 更新所有用户权限,更严谨的做法是更新与当前角色有关的用户权限
107+
adminShiroRealm.kickOutAllUser(false);
102108
return ReturnUtil.success("操作成功", null, "/console/role/index");
103109
} catch (Exception e) {
104110
e.printStackTrace();
@@ -118,6 +124,9 @@ public ModelMap delete(String[] ids) {
118124
adminRoleService.deleteRoleId(id);
119125
roleService.deleteById(id);
120126
}
127+
// 更新所有用户权限,更严谨的做法是更新与当前角色有关的用户权限
128+
adminShiroRealm.kickOutAllUser(false);
129+
121130
return ReturnUtil.success("操作成功", null, null);
122131
}
123132
} catch (Exception e) {
@@ -165,6 +174,10 @@ public ModelMap grant(String roleId, String[] menuIds) {
165174
} else if (menuIds == null && StringUtils.isNotEmpty(roleId)) {
166175
roleMenuService.deleteRoleId(roleId);
167176
}
177+
178+
// 更新所有用户权限,更严谨的做法是更新与当前角色有关的用户权限
179+
adminShiroRealm.kickOutAllUser(false);
180+
168181
return ReturnUtil.success("操作成功", null, null);
169182
} catch (Exception e) {
170183
e.printStackTrace();

‎src/main/java/com/geekcattle/core/redis/RedisSessionDAO.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
import org.springframework.data.redis.core.RedisTemplate;
99
import org.springframework.stereotype.Repository;
1010

11-
1211
import javax.annotation.Resource;
1312
import java.io.Serializable;
1413
import java.util.Collection;
15-
import java.util.Collections;
1614
import java.util.concurrent.TimeUnit;
15+
import java.util.stream.Collectors;
1716

1817
/**
1918
* redis实现共享session
19+
*
2020
* @author geekcattle
2121
*/
2222
@Repository("redisSessionDAO")
@@ -31,73 +31,83 @@ public class RedisSessionDAO extends AbstractSessionDAO {
3131
private RedisTemplate<String, Object> redisTemplate;
3232

3333
private String getKey(String originalKey) {
34-
return redisConfiguration.getSessionPrefix() +":"+ originalKey;
34+
return redisConfiguration.getSessionPrefix() +":"+ originalKey;
3535
}
3636

3737
/**
3838
* 创建session,保存到数据库
39+
*
3940
* @param session
4041
* @return
4142
*/
4243
@Override
4344
protected Serializable doCreate(Session session) {
4445
Serializable sessionId = this.generateSessionId(session);
4546
this.assignSessionId(session, sessionId);
46-
if(logger.isDebugEnabled()){
47+
if(logger.isDebugEnabled()){
4748
logger.debug("createSession:{}", sessionId.toString());
4849
}
49-
redisTemplate.opsForValue().set(getKey(sessionId.toString()), session,redisConfiguration.getSessionTime(),TimeUnit.MINUTES);
50+
redisTemplate.opsForValue().set(getKey(sessionId.toString()), session,redisConfiguration.getSessionTime(),TimeUnit.MINUTES);
5051
return sessionId;
5152
}
5253

5354
/**
5455
* 获取session
56+
*
5557
* @param sessionId
5658
* @return
5759
*/
5860
@Override
5961
protected Session doReadSession(Serializable sessionId) {
60-
if(logger.isDebugEnabled()){
62+
if(logger.isDebugEnabled()){
6163
logger.debug("readSession:{}", sessionId.toString());
6264
}
6365
// 先从缓存中获取session,如果没有再去数据库中获取
6466
Session session = null;
65-
if(session == null){
67+
if(session == null){
6668
session = (Session) redisTemplate.opsForValue().get(getKey(sessionId.toString()));
6769
}
6870
return session;
6971
}
7072

7173
/**
7274
* 更新session的最后一次访问时间
75+
*
7376
* @param session
7477
*/
7578
@Override
7679
public void update(Session session) {
77-
if(logger.isDebugEnabled()){
80+
if(logger.isDebugEnabled()){
7881
logger.debug("updateSession:{}", session.getId().toString());
7982
}
8083
String key = getKey(session.getId().toString());
81-
redisTemplate.opsForValue().set(key, session,redisConfiguration.getSessionTime(), TimeUnit.MINUTES);
84+
redisTemplate.opsForValue().set(key, session,redisConfiguration.getSessionTime(), TimeUnit.MINUTES);
8285
}
8386

8487
/**
8588
* 删除session
89+
*
8690
* @param session
8791
*/
8892
@Override
8993
public void delete(Session session) {
90-
if(logger.isDebugEnabled()){
94+
if(logger.isDebugEnabled()){
9195
logger.debug("delSession:{}", session.getId());
9296
}
9397
redisTemplate.delete(getKey(session.getId().toString()));
9498
}
9599

96100
@Override
97101
public Collection<Session> getActiveSessions() {
98-
if(logger.isDebugEnabled()){
102+
if(logger.isDebugEnabled()){
99103
logger.debug("activeSession");
100104
}
101-
return Collections.emptySet();
105+
redisTemplate.keys(redisConfiguration.getSessionPrefix() + "*");
106+
Collection<Session> sessions = redisTemplate.opsForValue()
107+
.multiGet(redisTemplate.keys(redisConfiguration.getSessionPrefix() + "*"))
108+
.stream()
109+
.map(s -> (Session) s)
110+
.collect(Collectors.toSet());
111+
return sessions;
102112
}
103113
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /