This commit is contained in:
2022-10-08 17:22:57 +08:00
7 changed files with 148 additions and 4 deletions

View File

@@ -118,8 +118,10 @@ public class AuthorizationController {
put("user", jsonObject);
}};
redisUtils.set("my-satoken", StpUtil.getTokenValue(), StpUtil.getTokenTimeout());
// 保存在线信息
onlineUserService.save(userDto, StpUtil.getTokenValue(), request);
// onlineUserService.save(userDto, StpUtil.getTokenValue(), request);
return ResponseEntity.ok(authInfo);
}
@@ -157,7 +159,7 @@ public class AuthorizationController {
@ApiOperation("退出登录")
@DeleteMapping(value = "/logout")
public ResponseEntity<Object> logout(HttpServletRequest request) {
onlineUserService.logout(StpUtil.getTokenValue());
// onlineUserService.logout(StpUtil.getTokenValue());
StpUtil.logout();
return new ResponseEntity<>(HttpStatus.OK);
}

View File

@@ -0,0 +1,21 @@
package org.nl.modules.security.satoken;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
/**
* @author: lyd
* @description:
* @Date: 2022/10/8
*/
@Configuration
public class RedisListenerConfig {
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}
}

View File

@@ -0,0 +1,51 @@
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);
}
}

View File

@@ -15,7 +15,6 @@ import org.nl.modules.common.utils.SecurityUtils;
import org.nl.modules.common.utils.dto.CurrentUser;
import org.nl.modules.system.service.ParamService;
import org.nl.modules.system.service.dto.ParamDto;
import org.nl.modules.system.service.dto.UserDto;
import org.nl.modules.wql.core.bean.ResultBean;
import org.nl.modules.wql.core.bean.WQLObject;
import org.nl.modules.wql.util.WqlUtil;

View File

@@ -6,6 +6,10 @@ spring:
druid:
db-type: com.alibaba.druid.pool.DruidDataSource
driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
url: jdbc:log4jdbc:mysql://${DB_HOST:47.111.78.178}:${DB_PORT:3306}/${DB_NAME:nladmin}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&useOldAliasMetadataBehavior=true
# url: jdbc:log4jdbc:mysql://${DB_HOST:127.0.0.1}:${DB_PORT:3306}/${DB_NAME:nladmin}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&useOldAliasMetadataBehavior=true
username: ${DB_USER:root}
# username: ${DB_USER:root}
url: jdbc:log4jdbc:mysql://${DB_HOST:47.111.78.178}:${DB_PORT:3306}/${DB_NAME:lzhl_one_lms}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&useOldAliasMetadataBehavior=true
# url: jdbc:log4jdbc:mysql://${DB_HOST:127.0.0.1}:${DB_PORT:3306}/${DB_NAME:nladmin}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&useOldAliasMetadataBehavior=true
username: ${DB_USER:root}