token过期提示
This commit is contained in:
@@ -21,6 +21,12 @@
|
|||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- redis -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!--WQL-->
|
<!--WQL-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.hynnet</groupId>
|
<groupId>com.hynnet</groupId>
|
||||||
|
|||||||
@@ -118,8 +118,10 @@ public class AuthorizationController {
|
|||||||
put("user", jsonObject);
|
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);
|
return ResponseEntity.ok(authInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +159,7 @@ public class AuthorizationController {
|
|||||||
@ApiOperation("退出登录")
|
@ApiOperation("退出登录")
|
||||||
@DeleteMapping(value = "/logout")
|
@DeleteMapping(value = "/logout")
|
||||||
public ResponseEntity<Object> logout(HttpServletRequest request) {
|
public ResponseEntity<Object> logout(HttpServletRequest request) {
|
||||||
onlineUserService.logout(StpUtil.getTokenValue());
|
// onlineUserService.logout(StpUtil.getTokenValue());
|
||||||
StpUtil.logout();
|
StpUtil.logout();
|
||||||
return new ResponseEntity<>(HttpStatus.OK);
|
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:
|
druid:
|
||||||
db-type: com.alibaba.druid.pool.DruidDataSource
|
db-type: com.alibaba.druid.pool.DruidDataSource
|
||||||
driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
|
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: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
|
# 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}
|
username: ${DB_USER:root}
|
||||||
# password: ${DB_PWD:P@ssw0rd}
|
# username: ${DB_USER:root}
|
||||||
password: ${DB_PWD:12356}
|
password: ${DB_PWD:P@ssw0rd}
|
||||||
|
# password: ${DB_PWD:12356}
|
||||||
# 初始连接数
|
# 初始连接数
|
||||||
initial-size: 5
|
initial-size: 5
|
||||||
# 最小连接数
|
# 最小连接数
|
||||||
|
|||||||
@@ -6,6 +6,65 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
export default {
|
export default {
|
||||||
name: 'App'
|
name: 'App',
|
||||||
|
created() {
|
||||||
|
this.webSocket() // token过期提示
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
webSocket() {
|
||||||
|
const that = this
|
||||||
|
if (typeof (WebSocket) === 'undefined') {
|
||||||
|
this.$notify({
|
||||||
|
title: '提示',
|
||||||
|
message: '当前浏览器无法接收实时报警信息,请使用谷歌浏览器!',
|
||||||
|
type: 'warning',
|
||||||
|
duration: 0
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
const id = 'exp-token'
|
||||||
|
// 获取token保存到vuex中的用户信息,此处仅适用于本项目,注意删除或修改
|
||||||
|
// 实例化socket,这里我把用户名传给了后台,使后台能判断要把消息发给哪个用户,其实也可以后台直接获取用户IP来判断并推送
|
||||||
|
// const socketUrl = process.env.VUE_APP_WS_API + id
|
||||||
|
const wsUri = window.g.prod.VUE_APP_BASE_API.replace('http', 'ws') + '/webSocket/' + id
|
||||||
|
this.socket = new WebSocket(wsUri)
|
||||||
|
// 监听socket打开
|
||||||
|
this.socket.onopen = function() {
|
||||||
|
that.socket.send('测试客户端发送消息')
|
||||||
|
}
|
||||||
|
const _this = this
|
||||||
|
// 监听socket消息接收
|
||||||
|
this.socket.onmessage = function(msg) {
|
||||||
|
const data = JSON.parse(msg.data)
|
||||||
|
_this.$confirm(data.msg, '提示', {
|
||||||
|
confirmButtonText: '确定',
|
||||||
|
closeOnClickModal: false,
|
||||||
|
showCancelButton: false,
|
||||||
|
type: 'warning'
|
||||||
|
}).then(() => {
|
||||||
|
_this.$router.push('/login')
|
||||||
|
}).catch(() => {
|
||||||
|
this.$message({
|
||||||
|
type: 'info',
|
||||||
|
message: '已取消'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 监听socket错误
|
||||||
|
this.socket.onerror = function() {
|
||||||
|
that.$notify({
|
||||||
|
title: '错误',
|
||||||
|
message: '服务器错误,无法接收实时报警信息',
|
||||||
|
type: 'error',
|
||||||
|
duration: 0
|
||||||
|
})
|
||||||
|
}
|
||||||
|
// 监听socket关闭
|
||||||
|
this.socket.onclose = function() {
|
||||||
|
console.log('WebSocket已关闭')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user