2
2
3
3
import org .hsweb .web .bean .po .user .User ;
4
4
import org .hsweb .web .core .session .AbstractHttpSessionManager ;
5
+ import org .springframework .dao .DataAccessException ;
6
+ import org .springframework .data .redis .connection .RedisConnection ;
5
7
import org .springframework .data .redis .core .RedisCallback ;
6
8
import org .springframework .data .redis .core .RedisTemplate ;
7
9
import org .springframework .session .ExpiringSession ;
12
14
import javax .servlet .http .HttpSessionContext ;
13
15
import java .util .Collections ;
14
16
import java .util .Enumeration ;
17
+ import java .util .HashSet ;
15
18
import java .util .Set ;
16
19
import java .util .stream .Collectors ;
17
20
@@ -46,12 +49,7 @@ public User getUserBySessionId(String sessionId) {
46
49
47
50
@ Override
48
51
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 );
55
53
}
56
54
57
55
@ Override
@@ -74,6 +72,7 @@ public void removeUser(String userId) {
74
72
75
73
@ Override
76
74
public void removeSession (String sessionId ) {
75
+ if (null == sessionId ) return ;
77
76
sessionRedisTemplate .delete ("spring:session:sessions:" .concat (sessionId ));
78
77
}
79
78
@@ -82,24 +81,34 @@ public void addUser(User user, HttpSession session) {
82
81
removeUser (user .getId ());
83
82
String key = "http.session.user:" + user .getId ();
84
83
String value = session .getId ();
84
+ session .setAttribute ("user" , user );
85
85
sessionRedisTemplate .opsForValue ().set (key , value );
86
86
onUserLogin (user , session );
87
87
}
88
88
89
+
89
90
@ Override
90
91
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 ());
99
104
}
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 ());
103
112
}
104
113
105
114
@ Override
@@ -126,6 +135,7 @@ public boolean isLogin(String userId) {
126
135
);
127
136
}
128
137
138
+
129
139
public void setRedisOperationsSessionRepository (RedisOperationsSessionRepository redisOperationsSessionRepository ) {
130
140
this .redisOperationsSessionRepository = redisOperationsSessionRepository ;
131
141
}
0 commit comments