修改oauth2 redis登录信息
private final AuthenticationKeyGenerator authenticationKeyGenerator = new
DefaultAuthenticationKeyGenerator();
RedisTokenStore tokenStore = new RedisTokenStore(redisConnectionFactory);
private final JdkSerializationStrategy serializationStrategy = new JdkSerializationStrategy();
public void setRedis(Authentication authentication, Collection extends GrantedAuthority> authorities, HopeUser hopeUser, RedisTokenStore tokenStore) {
if (authentication instanceof OAuth2Authentication) {
OAuth2Authentication originalOAuth2Authentication = (OAuth2Authentication) authentication;
if (!originalOAuth2Authentication.isClientOnly()) {
Authentication userAuthentication = originalOAuth2Authentication.getUserAuthentication();
if (userAuthentication instanceof WeChatAuthenticationToken || userAuthentication instanceof SmsCodeAuthenticationToken || userAuthentication instanceof UsernamePasswordAuthenticationToken) {
//替换用户信息
UsernamePasswordAuthenticationToken usernamePasswordAuthentication = new UsernamePasswordAuthenticationToken(hopeUser, "N_A", authorities);
usernamePasswordAuthentication.setDetails(hopeUser);
OAuth2Authentication oauth2Authentication = new OAuth2Authentication(originalOAuth2Authentication.getOAuth2Request(), usernamePasswordAuthentication);
oauth2Authentication.setDetails(hopeUser);
//提取秘钥
String key = authenticationKeyGenerator.extractKey(originalOAuth2Authentication);
//获取auth_to_access auth_to_access:key
byte[] serializedKey = serializationStrategy.serialize("auth_to_access" + ":" + key);
byte[] bytes = null;
RedisConnection conn = redisConnectionFactory.getConnection();
try {
bytes = conn.get(serializedKey);
} finally {
conn.close();
}
//获取到token
OAuth2AccessToken accessToken = serializationStrategy.deserialize(bytes, OAuth2AccessToken.class);
//更新这个token权限
tokenStore.storeAccessToken(accessToken, oauth2Authentication);
tokenStore.storeRefreshToken(accessToken.getRefreshToken(), oauth2Authentication);
}
}
}
}



