java 监听redis事件
第一步:利用RDM等redis连接工具查看相应事件,然后去网上搜索 一下,会有redis各种事件的说明,选择契合业务的事件;
第二步:创建监听处理类;
1 package com.lechuang.auth.advice; 2 3 import com.lechuang.common.core.entity.LeChuangAuthUser; 4 import com.lechuang.common.core.entity.system.SystemUser; 5 import com.lechuang.common.core.utils.LeChuangUtil; 6 import com.lechuang.common.redis.service.RedisDBService; 7 import com.lechuang.common.redis.service.RedisService; 8 import lombok.RequiredArgsConstructor; 9 import lombok.extern.slf4j.Slf4j; 10 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.data.redis.connection.Message; 12 import org.springframework.data.redis.connection.MessageListener; 13 import org.springframework.scheduling.annotation.Async; 14 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 15 import org.springframework.security.core.userdetails.User; 16 import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; 17 import org.springframework.security.oauth2.provider.OAuth2Authentication; 18 import org.springframework.security.oauth2.provider.token.TokenStore; 19 import org.springframework.stereotype.Service; 20 import org.springframework.util.ObjectUtils; 21 22 import javax.annotation.Resource; 23 import java.lang.reflect.Field; 24 import java.util.LinkedHashMap; 25 26 /** 27 * 功能描述:Redis监听 28 * 作者:唐泽齐 29 */ 30 @Slf4j 31 @Service 32 @RequiredArgsConstructor 33 public class LiveRedisKeysExpireListener implements MessageListener { 34 35 @Autowired 36 private RedisDBService redisDBService; 37 @Resource 38 private TokenStore tokenStore; 39 40 @Override 41 public void onMessage(Message message, byte[] pattern) { 42 Object o = redisDBService.getAndSet(9, "renewUser:" + message.toString(), null); 43 if(!ObjectUtils.isEmpty(o)) { 44 log.info("处理用户缓存信息更新:"+message.toString()); 45 renewToken(o,message.toString()); 46 } else { 47 redisDBService.del(9,"renewUser:" + message.toString()); 48 } 49 } 50 51 @Async 52 void renewToken(Object o,String tokenKey) { 53 try { 54 SystemUser user = (SystemUser) o; 55 OAuth2Authentication authentication = tokenStore.readAuthentication(tokenKey); 56 UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) authentication.getUserAuthentication(); 57 LinkedHashMap details = (LinkedHashMap) token.getDetails(); 58 LinkedHashMap principal = (LinkedHashMap) details.get("principal"); 59 60 LeChuangAuthUser authUser = (LeChuangAuthUser) token.getPrincipal(); 61 62 //更新username 63 Field username = User.class.getDeclaredField("username"); 64 username.setAccessible(true); 65 username.set(authUser,user.getUsername()); 66 //更新 avatar 67 authUser.setAvatar(user.getAvatar()); 68 //将新凭证 写入 token 69 DefaultOAuth2AccessToken oauthtoken = new DefaultOAuth2AccessToken(tokenKey); 70 oauthtoken.setTokenType("Bearer"); 71 tokenStore.storeAccessToken(oauthtoken, authentication); 72 redisDBService.del(9,"renewUser:"+tokenKey); 73 } catch (Exception e) { 74 e.printStackTrace(); 75 } 76 } 77 78 }
第三步:将监听处理类加入项目环境配置中,伴随项目启动;
1 package com.lechuang.auth.advice; 2 3 import org.springframework.context.annotation.Bean; 4 import org.springframework.context.annotation.Configuration; 5 import org.springframework.data.redis.connection.RedisConnectionFactory; 6 import org.springframework.data.redis.listener.PatternTopic; 7 import org.springframework.data.redis.listener.RedisMessageListenerContainer; 8 9 import javax.annotation.Resource; 10 11 /** 12 * 功能描述:Redis监听配置 13 * 作者:唐泽齐 14 */ 15 @Configuration 16 public class RedisListenerConfig { 17 18 @Resource 19 private LiveRedisKeysExpireListener liveRedisKeysExpireListener; 20 21 @Bean 22 RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { 23 RedisMessageListenerContainer container = new RedisMessageListenerContainer(); 24 container.setConnectionFactory(connectionFactory); 25 container.addMessageListener(liveRedisKeysExpireListener, new PatternTopic("__keyevent@9__:expire")); 26 return container; 27 } 28 29 }