From 1bca1b9c61676b48cc3137d3003e520c75ecdd92 Mon Sep 17 00:00:00 2001
From: aijinhui <aijinhui>
Date: 星期六, 13 一月 2024 10:57:36 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'
---
ard-work/pom.xml | 1
ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java | 20 +++++-
ruoyi-framework/src/main/java/com/ruoyi/framework/security/handle/LogoutSuccessHandlerImpl.java | 4 +
ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java | 5 +
ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java | 132 +++++++++++++++++++++++++++----------------
5 files changed, 109 insertions(+), 53 deletions(-)
diff --git a/ard-work/pom.xml b/ard-work/pom.xml
index 24e4683..8a51372 100644
--- a/ard-work/pom.xml
+++ b/ard-work/pom.xml
@@ -76,6 +76,7 @@
<artifactId>geodesy</artifactId>
<version>1.1.3</version>
</dependency>
+ <!-- 瑙嗛澶勭悊 -->
<!-- <dependency>-->
<!-- <groupId>org.bytedeco</groupId>-->
<!-- <artifactId>javacv-platform</artifactId>-->
diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java b/ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java
index 8c1d99a..f10e7dc 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java
@@ -139,4 +139,9 @@
*/
public static final String[] JOB_ERROR_STR = { "java.net.URL", "javax.naming.InitialContext", "org.yaml.snakeyaml",
"org.springframework", "org.apache", "com.ruoyi.common.utils.file", "com.ruoyi.common.config" };
+
+ /**
+ * 鐧诲綍鐢ㄦ埛缂栧彿 redis key
+ */
+ public static final String LOGIN_USERID_KEY = "login_userid:";
}
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/security/handle/LogoutSuccessHandlerImpl.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/security/handle/LogoutSuccessHandlerImpl.java
index 14874ba..df7539f 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/security/handle/LogoutSuccessHandlerImpl.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/security/handle/LogoutSuccessHandlerImpl.java
@@ -49,7 +49,9 @@
{
String userName = loginUser.getUsername();
// 鍒犻櫎鐢ㄦ埛缂撳瓨璁板綍
- tokenService.delLoginUser(loginUser.getToken());
+ // tokenService.delLoginUser(loginUser.getToken());
+ // 鍒犻櫎鐢ㄦ埛缂撳瓨璁板綍
+ tokenService.delLoginUser(loginUser.getToken(), loginUser.getUser().getUserId());
// 璁板綍鐢ㄦ埛閫�鍑烘棩蹇�
//鍥介檯鍖�
final String key = "language";
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java
index f3fa8aa..7e23657 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java
@@ -1,9 +1,9 @@
package com.ruoyi.framework.web.service;
import javax.annotation.Resource;
-
import com.ruoyi.rongcloud.service.RongCloudService;
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;
@@ -36,6 +36,11 @@
*/
@Component
public class SysLoginService {
+
+ // 鏄惁鍏佽璐︽埛澶氱粓绔悓鏃剁櫥褰曪紙true鍏佽 false涓嶅厑璁革級
+ @Value("${token.soloLogin}")
+ private boolean soloLogin;
+
@Autowired
private TokenService tokenService;
@@ -65,7 +70,6 @@
*/
public String login(String username, String password, String code, String uuid) {
-
boolean captchaEnabled = configService.selectCaptchaEnabled();
// 楠岃瘉鐮佸紑鍏�
if (captchaEnabled) {
@@ -92,7 +96,17 @@
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
recordLoginInfo(loginUser.getUserId());
-
+ if (!soloLogin)
+ {
+ // 濡傛灉鐢ㄦ埛涓嶅厑璁稿缁堢鍚屾椂鐧诲綍锛屾竻闄ょ紦瀛樹俊鎭�
+ String userIdKey = Constants.LOGIN_USERID_KEY + loginUser.getUser().getUserId();
+ String userKey = redisCache.getCacheObject(userIdKey);
+ if (StringUtils.isNotEmpty(userKey))
+ {
+ redisCache.deleteObject(userIdKey);
+ redisCache.deleteObject(userKey);
+ }
+ }
getRongCloudToken(loginUser);
// 鐢熸垚token
String token = tokenService.createToken(loginUser);
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java
index 710b5d3..07ecf89 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java
@@ -4,6 +4,7 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;
+
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@@ -27,8 +28,7 @@
* @author ruoyi
*/
@Component
-public class TokenService
-{
+public class TokenService {
// 浠ょ墝鑷畾涔夋爣璇�
@Value("${token.header}")
private String header;
@@ -40,6 +40,10 @@
// 浠ょ墝鏈夋晥鏈燂紙榛樿30鍒嗛挓锛�
@Value("${token.expireTime}")
private int expireTime;
+
+ // 鏄惁鍏佽璐︽埛澶氱粓绔悓鏃剁櫥褰曪紙true鍏佽 false涓嶅厑璁革級
+ @Value("${token.soloLogin}")
+ private boolean soloLogin;
protected static final long MILLIS_SECOND = 1000;
@@ -55,23 +59,18 @@
*
* @return 鐢ㄦ埛淇℃伅
*/
- public LoginUser getLoginUser(HttpServletRequest request)
- {
+ public LoginUser getLoginUser(HttpServletRequest request) {
// 鑾峰彇璇锋眰鎼哄甫鐨勪护鐗�
String token = getToken(request);
- if (StringUtils.isNotEmpty(token))
- {
- try
- {
+ if (StringUtils.isNotEmpty(token)) {
+ try {
Claims claims = parseToken(token);
// 瑙f瀽瀵瑰簲鐨勬潈闄愪互鍙婄敤鎴蜂俊鎭�
String uuid = (String) claims.get(Constants.LOGIN_USER_KEY);
String userKey = getTokenKey(uuid);
LoginUser user = redisCache.getCacheObject(userKey);
return user;
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
}
}
return null;
@@ -80,24 +79,40 @@
/**
* 璁剧疆鐢ㄦ埛韬唤淇℃伅
*/
- public void setLoginUser(LoginUser loginUser)
- {
- if (StringUtils.isNotNull(loginUser) && StringUtils.isNotEmpty(loginUser.getToken()))
- {
+ public void setLoginUser(LoginUser loginUser) {
+ if (StringUtils.isNotNull(loginUser) && StringUtils.isNotEmpty(loginUser.getToken())) {
refreshToken(loginUser);
}
}
+ ///**
+ // * 鍒犻櫎鐢ㄦ埛韬唤淇℃伅
+ // */
+ //public void delLoginUser(String token)
+ //{
+ // if (StringUtils.isNotEmpty(token))
+ // {
+ // String userKey = getTokenKey(token);
+ // redisCache.deleteObject(userKey);
+ // }
+ //}
+
/**
* 鍒犻櫎鐢ㄦ埛韬唤淇℃伅
*/
- public void delLoginUser(String token)
- {
- if (StringUtils.isNotEmpty(token))
- {
+ public void delLoginUser(String token, String userId) {
+ if (StringUtils.isNotEmpty(token)) {
String userKey = getTokenKey(token);
redisCache.deleteObject(userKey);
}
+ if (!soloLogin && StringUtils.isNotNull(userId)) {
+ String userIdKey = getUserIdKey(userId);
+ redisCache.deleteObject(userIdKey);
+ }
+ }
+
+ private String getUserIdKey(String userId) {
+ return Constants.LOGIN_USERID_KEY + userId;
}
/**
@@ -106,8 +121,7 @@
* @param loginUser 鐢ㄦ埛淇℃伅
* @return 浠ょ墝
*/
- public String createToken(LoginUser loginUser)
- {
+ public String createToken(LoginUser loginUser) {
String token = IdUtils.fastUUID();
loginUser.setToken(token);
setUserAgent(loginUser);
@@ -124,40 +138,67 @@
* @param loginUser
* @return 浠ょ墝
*/
- public void verifyToken(LoginUser loginUser)
- {
+ public void verifyToken(LoginUser loginUser) {
long expireTime = loginUser.getExpireTime();
long currentTime = System.currentTimeMillis();
- if (expireTime - currentTime <= MILLIS_MINUTE_TEN)
- {
+ if (expireTime - currentTime <= MILLIS_MINUTE_TEN) {
refreshToken(loginUser);
}
}
+
+ ///**
+ // * 鍒锋柊浠ょ墝鏈夋晥鏈�
+ // *
+ // * @param loginUser 鐧诲綍淇℃伅
+ // */
+ //public void refreshToken(LoginUser loginUser)
+ //{
+ // loginUser.setLoginTime(System.currentTimeMillis());
+ // String appUserType = loginUser.getUser().getAppUserType();
+ // if(StringUtils.isNull(appUserType))
+ // {
+ // loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE);
+ // // 鏍规嵁uuid灏唋oginUser缂撳瓨
+ // String userKey = getTokenKey(loginUser.getToken());
+ // redisCache.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES);
+ // }
+ // else
+ // {
+ // loginUser.setExpireTime(loginUser.getLoginTime() + 365*24*60 * MILLIS_MINUTE);
+ // // 鏍规嵁uuid灏唋oginUser缂撳瓨
+ // String userKey = getTokenKey(loginUser.getToken());
+ // redisCache.setCacheObject(userKey, loginUser, 365*24*60, TimeUnit.MINUTES);
+ // }
+ //}
/**
* 鍒锋柊浠ょ墝鏈夋晥鏈�
*
* @param loginUser 鐧诲綍淇℃伅
*/
- public void refreshToken(LoginUser loginUser)
- {
+ public void refreshToken(LoginUser loginUser) {
loginUser.setLoginTime(System.currentTimeMillis());
+ loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE);
+ // 鏍规嵁uuid灏唋oginUser缂撳瓨
+ //String userKey = getTokenKey(loginUser.getToken());
+ //redisCache.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES);
String appUserType = loginUser.getUser().getAppUserType();
- if(StringUtils.isNull(appUserType))
- {
+ if (StringUtils.isNull(appUserType)) {
loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE);
// 鏍规嵁uuid灏唋oginUser缂撳瓨
String userKey = getTokenKey(loginUser.getToken());
redisCache.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES);
- }
- else
- {
- loginUser.setExpireTime(loginUser.getLoginTime() + 365*24*60 * MILLIS_MINUTE);
+ if (!soloLogin) {
+ // 缂撳瓨鐢ㄦ埛鍞竴鏍囪瘑锛岄槻姝㈠悓涓�甯愬彿锛屽悓鏃剁櫥褰�
+ String userIdKey = getUserIdKey(loginUser.getUser().getUserId());
+ redisCache.setCacheObject(userIdKey, userKey, expireTime, TimeUnit.MINUTES);
+ }
+ } else {
+ loginUser.setExpireTime(loginUser.getLoginTime() + 365 * 24 * 60 * MILLIS_MINUTE);
// 鏍规嵁uuid灏唋oginUser缂撳瓨
String userKey = getTokenKey(loginUser.getToken());
- redisCache.setCacheObject(userKey, loginUser, 365*24*60, TimeUnit.MINUTES);
+ redisCache.setCacheObject(userKey, loginUser, 365 * 24 * 60, TimeUnit.MINUTES);
}
-
}
/**
@@ -165,8 +206,7 @@
*
* @param loginUser 鐧诲綍淇℃伅
*/
- public void setUserAgent(LoginUser loginUser)
- {
+ public void setUserAgent(LoginUser loginUser) {
UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
loginUser.setIpaddr(ip);
@@ -181,8 +221,7 @@
* @param claims 鏁版嵁澹版槑
* @return 浠ょ墝
*/
- private String createToken(Map<String, Object> claims)
- {
+ private String createToken(Map<String, Object> claims) {
String token = Jwts.builder()
.setClaims(claims)
.signWith(SignatureAlgorithm.HS512, secret).compact();
@@ -195,8 +234,7 @@
* @param token 浠ょ墝
* @return 鏁版嵁澹版槑
*/
- private Claims parseToken(String token)
- {
+ private Claims parseToken(String token) {
return Jwts.parser()
.setSigningKey(secret)
.parseClaimsJws(token)
@@ -209,8 +247,7 @@
* @param token 浠ょ墝
* @return 鐢ㄦ埛鍚�
*/
- public String getUsernameFromToken(String token)
- {
+ public String getUsernameFromToken(String token) {
Claims claims = parseToken(token);
return claims.getSubject();
}
@@ -221,18 +258,15 @@
* @param request
* @return token
*/
- private String getToken(HttpServletRequest request)
- {
+ private String getToken(HttpServletRequest request) {
String token = request.getHeader(header);
- if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX))
- {
+ if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX)) {
token = token.replace(Constants.TOKEN_PREFIX, "");
}
return token;
}
- private String getTokenKey(String uuid)
- {
+ private String getTokenKey(String uuid) {
return CacheConstants.LOGIN_TOKEN_KEY + uuid;
}
}
--
Gitblit v1.9.3