From 2970ebf9ec5fb8bcb2715c364051369e9520b27d Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: Tue, 11 Mar 2025 15:51:07 +0800
Subject: [PATCH] 增加用户已登录,不允许重复登陆
---
ard-framework/src/main/java/com/ard/framework/security/handle/CustomAuthenticationSuccessHandler.java | 20 ++++++++++++++++++++
ard-framework/src/main/java/com/ard/framework/web/service/SysLoginService.java | 19 +++++++++++++++++++
ard-admin/src/main/resources/application.yml | 2 +-
3 files changed, 40 insertions(+), 1 deletions(-)
diff --git a/ard-admin/src/main/resources/application.yml b/ard-admin/src/main/resources/application.yml
index fee1534..334660b 100644
--- a/ard-admin/src/main/resources/application.yml
+++ b/ard-admin/src/main/resources/application.yml
@@ -50,7 +50,7 @@
# 令牌密钥
secret: abcdefghijklmnopqrstuvwxyz
# 令牌有效期(默认30分钟)
- expireTime: 720
+ expireTime: 10
# 是否允许账户多终端同时登录(true允许 false不允许)
soloLogin: false
# MyBatis配置
diff --git a/ard-framework/src/main/java/com/ard/framework/security/handle/CustomAuthenticationSuccessHandler.java b/ard-framework/src/main/java/com/ard/framework/security/handle/CustomAuthenticationSuccessHandler.java
new file mode 100644
index 0000000..4ae53bf
--- /dev/null
+++ b/ard-framework/src/main/java/com/ard/framework/security/handle/CustomAuthenticationSuccessHandler.java
@@ -0,0 +1,20 @@
+package com.ard.framework.security.handle;
+
+import com.ard.common.core.redis.RedisCache;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+public class CustomAuthenticationSuccessHandler {
+
+ @Autowired
+ private RedisCache redisCache;
+
+ public void onAuthenticationSuccess(Long userId) {
+ // 检查 Redis 中是否已有该用户登录
+ String key = "login_userid:" + userId;
+ if (redisCache.hasKey(key)) {
+ throw new RuntimeException("用户已登录,请勿重复登录");
+ }
+ }
+}
diff --git a/ard-framework/src/main/java/com/ard/framework/web/service/SysLoginService.java b/ard-framework/src/main/java/com/ard/framework/web/service/SysLoginService.java
index fd63a77..d7fec9a 100644
--- a/ard-framework/src/main/java/com/ard/framework/web/service/SysLoginService.java
+++ b/ard-framework/src/main/java/com/ard/framework/web/service/SysLoginService.java
@@ -1,12 +1,15 @@
package com.ard.framework.web.service;
import javax.annotation.Resource;
+
+import com.ard.framework.security.handle.CustomAuthenticationSuccessHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
+import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;
import com.ard.common.constant.CacheConstants;
import com.ard.common.constant.Constants;
@@ -57,6 +60,12 @@
@Autowired
private ISysConfigService configService;
+ @Resource
+ private CustomAuthenticationSuccessHandler successHandler;
+
+ @Autowired
+ private UserDetailsService userDetailsService;
+
/**
* 登录验证
*
@@ -80,6 +89,15 @@
AuthenticationContextHolder.setContext(authenticationToken);
// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
authentication = authenticationManager.authenticate(authenticationToken);
+
+ // 获取用户ID
+ LoginUser loginUser = (LoginUser) userDetailsService.loadUserByUsername(username);
+ Long userId = loginUser.getUser().getUserId();
+ // 登录成功逻辑
+ if (authentication.isAuthenticated()) {
+ // 检查是否已登录
+ successHandler.onAuthenticationSuccess(userId);
+ }
}
catch (Exception e)
{
@@ -158,6 +176,7 @@
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("not.null")));
throw new UserNotExistsException();
}
+
// 密码如果不在指定范围内 错误
if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
|| password.length() > UserConstants.PASSWORD_MAX_LENGTH)
--
Gitblit v1.9.3