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

View File

@@ -20,6 +20,14 @@
</properties>
<dependencies>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--WQL-->
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>jxl</artifactId>

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}

View File

@@ -6,6 +6,65 @@
<script>
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>