Skip to content

Commit 60f39ce

Browse files
charsyamrwinch
authored andcommitted
move list to set
fix test code
1 parent 8251dfc commit 60f39ce

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

spring-security-oauth2/src/main/java/org/springframework/security/oauth2/provider/token/store/redis/RedisTokenStore.java

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

33
import org.springframework.data.redis.connection.RedisConnection;
44
import org.springframework.data.redis.connection.RedisConnectionFactory;
5+
import org.springframework.data.redis.core.Cursor;
6+
import org.springframework.data.redis.core.ScanOptions;
57
import org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken;
68
import org.springframework.security.oauth2.common.OAuth2AccessToken;
79
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
@@ -188,9 +190,9 @@ public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authe
188190
conn.set(authToAccessKey, serializedAccessToken);
189191
}
190192
if (!authentication.isClientOnly()) {
191-
conn.rPush(approvalKey, serializedAccessToken);
193+
conn.sAdd(approvalKey, serializedAccessToken);
192194
}
193-
conn.rPush(clientId, serializedAccessToken);
195+
conn.sAdd(clientId, serializedAccessToken);
194196
if (token.getExpiration() != null) {
195197
int seconds = token.getExpiresIn();
196198
conn.expire(accessKey, seconds);
@@ -287,8 +289,8 @@ public void removeAccessToken(String tokenValue) {
287289
byte[] clientId = serializeKey(CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId());
288290
conn.openPipeline();
289291
conn.del(authToAccessKey);
290-
conn.lRem(unameKey, 1, access);
291-
conn.lRem(clientId, 1, access);
292+
conn.sRem(unameKey, access);
293+
conn.sRem(clientId, access);
292294
conn.del(serialize(ACCESS + key));
293295
conn.closePipeline();
294296
}
@@ -396,13 +398,24 @@ private void removeAccessTokenUsingRefreshToken(String refreshToken) {
396398
}
397399
}
398400

401+
private List<byte[]> getByteLists(byte[] approvalKey, RedisConnection conn) {
402+
List<byte[]> byteList;
403+
Long size = conn.sCard(approvalKey);
404+
byteList = new ArrayList<byte[]>(size.intValue());
405+
Cursor<byte[]> cursor = conn.sScan(approvalKey, ScanOptions.NONE);
406+
while(cursor.hasNext()) {
407+
byteList.add(cursor.next());
408+
}
409+
return byteList;
410+
}
411+
399412
@Override
400413
public Collection<OAuth2AccessToken> findTokensByClientIdAndUserName(String clientId, String userName) {
401414
byte[] approvalKey = serializeKey(UNAME_TO_ACCESS + getApprovalKey(clientId, userName));
402415
List<byte[]> byteList = null;
403416
RedisConnection conn = getConnection();
404417
try {
405-
byteList = conn.lRange(approvalKey, 0, -1);
418+
byteList = getByteLists(approvalKey, conn);
406419
} finally {
407420
conn.close();
408421
}
@@ -423,7 +436,7 @@ public Collection<OAuth2AccessToken> findTokensByClientId(String clientId) {
423436
List<byte[]> byteList = null;
424437
RedisConnection conn = getConnection();
425438
try {
426-
byteList = conn.lRange(key, 0, -1);
439+
byteList = getByteLists(key, conn);
427440
} finally {
428441
conn.close();
429442
}

spring-security-oauth2/src/test/java/org/springframework/security/oauth2/provider/token/store/redis/RedisTokenStoreMockTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,16 @@ public void storeAccessTokenWithoutRefreshTokenRemoveAccessTokenVerifyKeysRemove
9393
ArgumentCaptor<byte[]> setKeyArgs = ArgumentCaptor.forClass(byte[].class);
9494
verify(connection, times(3)).set(setKeyArgs.capture(), any(byte[].class));
9595

96-
ArgumentCaptor<byte[]> rPushKeyArgs = ArgumentCaptor.forClass(byte[].class);
97-
verify(connection, times(2)).rPush(rPushKeyArgs.capture(), any(byte[].class));
96+
ArgumentCaptor<byte[]> sAddKeyArgs = ArgumentCaptor.forClass(byte[].class);
97+
verify(connection, times(2)).sAdd(sAddKeyArgs.capture(), any(byte[].class));
9898

9999
tokenStore.removeAccessToken(oauth2AccessToken);
100100

101101
for (byte[] key : setKeyArgs.getAllValues()) {
102102
verify(connection).del(key);
103103
}
104-
for (byte[] key : rPushKeyArgs.getAllValues()) {
105-
verify(connection).lRem(eq(key), eq(1L), any(byte[].class));
104+
for (byte[] key : sAddKeyArgs.getAllValues()) {
105+
verify(connection).sRem(eq(key), any(byte[].class));
106106
}
107107
}
108108

0 commit comments

Comments
 (0)