token过期提示
This commit is contained in:
@@ -21,6 +21,12 @@
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- redis -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--WQL-->
|
||||
<dependency>
|
||||
<groupId>com.hynnet</groupId>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,12 +6,12 @@ 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}
|
||||
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}
|
||||
# password: ${DB_PWD:P@ssw0rd}
|
||||
password: ${DB_PWD:12356}
|
||||
# username: ${DB_USER:root}
|
||||
password: ${DB_PWD:P@ssw0rd}
|
||||
# password: ${DB_PWD:12356}
|
||||
# 初始连接数
|
||||
initial-size: 5
|
||||
# 最小连接数
|
||||
|
||||
Reference in New Issue
Block a user