修复redis序列化认证信息问题

pull/7327/head
EightMonth 2 months ago
parent fdb05443c2
commit 4aa88189ed

@ -43,9 +43,11 @@ public class JeecgRedisOAuth2AuthorizationService implements OAuth2Authorization
if (isState(authorization)) { if (isState(authorization)) {
String token = authorization.getAttribute("state"); String token = authorization.getAttribute("state");
RedisSerializer<?> redisSerializer = redisTemplate.getValueSerializer();
redisTemplate.setValueSerializer(RedisSerializer.java()); redisTemplate.setValueSerializer(RedisSerializer.java());
redisTemplate.opsForValue().set(buildKey(OAuth2ParameterNames.STATE, token), authorization, TIMEOUT, redisTemplate.opsForValue().set(buildKey(OAuth2ParameterNames.STATE, token), authorization, TIMEOUT,
TimeUnit.MINUTES); TimeUnit.MINUTES);
redisTemplate.setValueSerializer(redisSerializer);
} }
if (isCode(authorization)) { if (isCode(authorization)) {
@ -54,22 +56,27 @@ public class JeecgRedisOAuth2AuthorizationService implements OAuth2Authorization
OAuth2AuthorizationCode authorizationCodeToken = authorizationCode.getToken(); OAuth2AuthorizationCode authorizationCodeToken = authorizationCode.getToken();
long between = ChronoUnit.MINUTES.between(authorizationCodeToken.getIssuedAt(), long between = ChronoUnit.MINUTES.between(authorizationCodeToken.getIssuedAt(),
authorizationCodeToken.getExpiresAt()); authorizationCodeToken.getExpiresAt());
RedisSerializer<?> redisSerializer = redisTemplate.getValueSerializer();
redisTemplate.setValueSerializer(RedisSerializer.java()); redisTemplate.setValueSerializer(RedisSerializer.java());
redisTemplate.opsForValue().set(buildKey(OAuth2ParameterNames.CODE, authorizationCodeToken.getTokenValue()), redisTemplate.opsForValue().set(buildKey(OAuth2ParameterNames.CODE, authorizationCodeToken.getTokenValue()),
authorization, between, TimeUnit.MINUTES); authorization, between, TimeUnit.MINUTES);
redisTemplate.setValueSerializer(redisSerializer);
} }
if (isRefreshToken(authorization)) { if (isRefreshToken(authorization)) {
OAuth2RefreshToken refreshToken = authorization.getRefreshToken().getToken(); OAuth2RefreshToken refreshToken = authorization.getRefreshToken().getToken();
long between = ChronoUnit.SECONDS.between(refreshToken.getIssuedAt(), refreshToken.getExpiresAt()); long between = ChronoUnit.SECONDS.between(refreshToken.getIssuedAt(), refreshToken.getExpiresAt());
RedisSerializer<?> redisSerializer = redisTemplate.getValueSerializer();
redisTemplate.setValueSerializer(RedisSerializer.java()); redisTemplate.setValueSerializer(RedisSerializer.java());
redisTemplate.opsForValue().set(buildKey(OAuth2ParameterNames.REFRESH_TOKEN, refreshToken.getTokenValue()), redisTemplate.opsForValue().set(buildKey(OAuth2ParameterNames.REFRESH_TOKEN, refreshToken.getTokenValue()),
authorization, between, TimeUnit.SECONDS); authorization, between, TimeUnit.SECONDS);
redisTemplate.setValueSerializer(redisSerializer);
} }
if (isAccessToken(authorization)) { if (isAccessToken(authorization)) {
OAuth2AccessToken accessToken = authorization.getAccessToken().getToken(); OAuth2AccessToken accessToken = authorization.getAccessToken().getToken();
long between = ChronoUnit.SECONDS.between(accessToken.getIssuedAt(), accessToken.getExpiresAt()); long between = ChronoUnit.SECONDS.between(accessToken.getIssuedAt(), accessToken.getExpiresAt());
RedisSerializer<?> redisSerializer = redisTemplate.getValueSerializer();
redisTemplate.setValueSerializer(RedisSerializer.java()); redisTemplate.setValueSerializer(RedisSerializer.java());
redisTemplate.opsForValue().set(buildKey(OAuth2ParameterNames.ACCESS_TOKEN, accessToken.getTokenValue()), redisTemplate.opsForValue().set(buildKey(OAuth2ParameterNames.ACCESS_TOKEN, accessToken.getTokenValue()),
authorization, between, TimeUnit.SECONDS); authorization, between, TimeUnit.SECONDS);
@ -77,6 +84,7 @@ public class JeecgRedisOAuth2AuthorizationService implements OAuth2Authorization
// 扩展记录 access-token 、username 的关系 1::token::username::admin::xxx // 扩展记录 access-token 、username 的关系 1::token::username::admin::xxx
String tokenUsername = String.format("%s::%s::%s", AUTHORIZATION, authorization.getPrincipalName(), accessToken.getTokenValue()); String tokenUsername = String.format("%s::%s::%s", AUTHORIZATION, authorization.getPrincipalName(), accessToken.getTokenValue());
redisTemplate.opsForValue().set(tokenUsername, accessToken.getTokenValue(), between, TimeUnit.SECONDS); redisTemplate.opsForValue().set(tokenUsername, accessToken.getTokenValue(), between, TimeUnit.SECONDS);
redisTemplate.setValueSerializer(redisSerializer);
} }
} }
@ -125,8 +133,11 @@ public class JeecgRedisOAuth2AuthorizationService implements OAuth2Authorization
public OAuth2Authorization findByToken(String token, @Nullable OAuth2TokenType tokenType) { public OAuth2Authorization findByToken(String token, @Nullable OAuth2TokenType tokenType) {
Assert.hasText(token, "token cannot be empty"); Assert.hasText(token, "token cannot be empty");
Assert.notNull(tokenType, "tokenType cannot be empty"); Assert.notNull(tokenType, "tokenType cannot be empty");
RedisSerializer<?> redisSerializer = redisTemplate.getValueSerializer();
redisTemplate.setValueSerializer(RedisSerializer.java()); redisTemplate.setValueSerializer(RedisSerializer.java());
return (OAuth2Authorization) redisTemplate.opsForValue().get(buildKey(tokenType.getValue(), token)); OAuth2Authorization oAuth2Authorization = (OAuth2Authorization) redisTemplate.opsForValue().get(buildKey(tokenType.getValue(), token));
redisTemplate.setValueSerializer(redisSerializer);
return oAuth2Authorization;
} }
private String buildKey(String type, String id) { private String buildKey(String type, String id) {

Loading…
Cancel
Save