管理员
2 years ago
7 changed files with 32 additions and 103 deletions
@ -1,51 +0,0 @@ |
|||||
package org.nl.modules.security.satoken; |
|
||||
|
|
||||
|
|
||||
import lombok.extern.slf4j.Slf4j; |
|
||||
import org.nl.modules.mnt.websocket.MsgType; |
|
||||
import org.nl.modules.mnt.websocket.SocketMsg; |
|
||||
import org.nl.modules.mnt.websocket.WebSocketServer; |
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||
import org.springframework.data.redis.connection.Message; |
|
||||
import org.springframework.data.redis.core.StringRedisTemplate; |
|
||||
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener; |
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer; |
|
||||
import org.springframework.stereotype.Component; |
|
||||
|
|
||||
import java.io.IOException; |
|
||||
import java.nio.charset.StandardCharsets; |
|
||||
|
|
||||
/** |
|
||||
* @author: lyd |
|
||||
* @description: |
|
||||
* @Date: 2022/10/8 |
|
||||
*/ |
|
||||
@Slf4j |
|
||||
@Component |
|
||||
public class TokenKeyExpirationListener extends KeyExpirationEventMessageListener { |
|
||||
@Autowired |
|
||||
private StringRedisTemplate redisTemplate; |
|
||||
|
|
||||
public TokenKeyExpirationListener(RedisMessageListenerContainer listenerContainer) { |
|
||||
super(listenerContainer); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public void onMessage(Message message, byte[] pattern) { |
|
||||
// 监听过期的key
|
|
||||
String expireKey = new String(message.getBody(), StandardCharsets.UTF_8); |
|
||||
//获取key原本的value 获取不到 是null
|
|
||||
String expireKeyValue = redisTemplate.opsForValue().get("my-satoken"); |
|
||||
//我是根据tokenvalues作为主键ID的
|
|
||||
String[] split = expireKey.split(":"); |
|
||||
String s = split[split.length - 1]; |
|
||||
try { |
|
||||
WebSocketServer.sendInfo(new SocketMsg("token会话过期!", MsgType.INFO), "exp-token"); |
|
||||
} catch (IOException e) { |
|
||||
log.error(e.getMessage(), e); |
|
||||
} |
|
||||
log.info("expireKey---"+expireKey); |
|
||||
log.info("expireKeyValue---"+expireKeyValue); |
|
||||
} |
|
||||
|
|
||||
} |
|
@ -1,45 +0,0 @@ |
|||||
package org.nl.modules.security.satoken; |
|
||||
|
|
||||
|
|
||||
import cn.hutool.core.util.ObjectUtil; |
|
||||
import lombok.extern.slf4j.Slf4j; |
|
||||
import org.nl.modules.common.utils.SecurityUtils; |
|
||||
import org.nl.modules.mnt.websocket.MsgType; |
|
||||
import org.nl.modules.mnt.websocket.SocketMsg; |
|
||||
import org.nl.modules.mnt.websocket.WebSocketServer; |
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||
import org.springframework.data.redis.connection.Message; |
|
||||
import org.springframework.data.redis.core.StringRedisTemplate; |
|
||||
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener; |
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer; |
|
||||
import org.springframework.stereotype.Component; |
|
||||
|
|
||||
import java.io.IOException; |
|
||||
|
|
||||
/** |
|
||||
* @author: lyd |
|
||||
* @description: redis过期key监听器 |
|
||||
* @Date: 2022/10/8 |
|
||||
*/ |
|
||||
@Slf4j |
|
||||
@Component |
|
||||
public class TokenKeyExpirationListener extends KeyExpirationEventMessageListener { |
|
||||
@Autowired |
|
||||
private StringRedisTemplate redisTemplate; |
|
||||
|
|
||||
public TokenKeyExpirationListener(RedisMessageListenerContainer listenerContainer) { |
|
||||
super(listenerContainer); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public void onMessage(Message message, byte[] pattern) { |
|
||||
String expireKeyValue = redisTemplate.opsForValue().get("my-satoken"); |
|
||||
try { |
|
||||
if (ObjectUtil.isEmpty(expireKeyValue)) |
|
||||
WebSocketServer.sendInfo(new SocketMsg("token会话过期!", MsgType.INFO), "exp-token"); |
|
||||
} catch (IOException e) { |
|
||||
log.error(e.getMessage(), e); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
} |
|
Loading…
Reference in new issue