Skip to content

Commit 84c780d

Browse files
committed
修复无法退出问题
1 parent f44a84c commit 84c780d

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

hsweb-web-core/src/main/java/org/hsweb/web/core/session/redis/RedisHttpSessionManager.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.hsweb.web.bean.po.user.User;
44
import org.hsweb.web.core.session.AbstractHttpSessionManager;
5+
import org.springframework.dao.DataAccessException;
6+
import org.springframework.data.redis.connection.RedisConnection;
57
import org.springframework.data.redis.core.RedisCallback;
68
import org.springframework.data.redis.core.RedisTemplate;
79
import org.springframework.session.ExpiringSession;
@@ -12,6 +14,7 @@
1214
import javax.servlet.http.HttpSessionContext;
1315
import java.util.Collections;
1416
import java.util.Enumeration;
17+
import java.util.HashSet;
1518
import java.util.Set;
1619
import java.util.stream.Collectors;
1720

@@ -46,12 +49,7 @@ public User getUserBySessionId(String sessionId) {
4649

4750
@Override
4851
public String getSessionIdByUserId(String userId) {
49-
return (String) sessionRedisTemplate.execute((RedisCallback<String>) connection -> {
50-
String key = "http.session.user:" + userId;
51-
byte[] sessionId = connection.get(key.getBytes());
52-
if (sessionId == null) return null;
53-
return new String(sessionId);
54-
});
52+
return (String) sessionRedisTemplate.opsForValue().get("http.session.user:" + userId);
5553
}
5654

5755
@Override
@@ -74,6 +72,7 @@ public void removeUser(String userId) {
7472

7573
@Override
7674
public void removeSession(String sessionId) {
75+
if (null == sessionId) return;
7776
sessionRedisTemplate.delete("spring:session:sessions:".concat(sessionId));
7877
}
7978

@@ -82,24 +81,34 @@ public void addUser(User user, HttpSession session) {
8281
removeUser(user.getId());
8382
String key = "http.session.user:" + user.getId();
8483
String value = session.getId();
84+
session.setAttribute("user", user);
8585
sessionRedisTemplate.opsForValue().set(key, value);
8686
onUserLogin(user, session);
8787
}
8888

89+
8990
@Override
9091
public Set<String> getUserIdList() {
91-
return (Set<String>) sessionRedisTemplate.execute((RedisCallback<Set<String>>) connection -> {
92-
Set<byte[]> keys = connection.keys("http.session.user:*".getBytes());
93-
return keys.stream().map(key -> {
94-
String sessionId = "spring:session:sessions:" + new String(connection.get(key));
95-
String userId = new String(key).split("[:]")[1];
96-
if (!connection.exists(sessionId.getBytes())) {
97-
removeUser(userId);
98-
return null;
92+
Set<String> keys = sessionRedisTemplate.keys("http.session.user:*");
93+
if (keys == null || keys.isEmpty()) {
94+
return new HashSet<>();
95+
}
96+
return keys.stream().map(key -> {
97+
String sessionId = (String) sessionRedisTemplate.opsForValue().get(key);
98+
String sessionIdKey = "spring:session:sessions:".concat(sessionId);
99+
String userId = new String(key).split("[:]")[1];
100+
boolean sessionExists = (Boolean) sessionRedisTemplate.execute(new RedisCallback<Boolean>() {
101+
@Override
102+
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
103+
return connection.exists(sessionIdKey.getBytes());
99104
}
100-
return userId;
101-
}).filter(key -> key != null).collect(Collectors.toSet());
102-
});
105+
});
106+
if (!sessionExists) {
107+
sessionRedisTemplate.delete(key);
108+
return null;
109+
}
110+
return userId;
111+
}).filter(key -> key != null).collect(Collectors.toSet());
103112
}
104113

105114
@Override
@@ -126,6 +135,7 @@ public boolean isLogin(String userId) {
126135
);
127136
}
128137

138+
129139
public void setRedisOperationsSessionRepository(RedisOperationsSessionRepository redisOperationsSessionRepository) {
130140
this.redisOperationsSessionRepository = redisOperationsSessionRepository;
131141
}

0 commit comments

Comments
 (0)