-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 修复在多租户下,token到期时无法清理在线用户状态 perf: 升级ruoyi-ui依赖
- Loading branch information
Showing
9 changed files
with
249 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...atoken/src/main/java/org/dromara/common/satoken/online/DefaultOnlineUserCacheManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.dromara.common.satoken.online; | ||
|
||
import cn.dev33.satoken.stp.SaLoginModel; | ||
import org.dromara.common.core.constant.CacheConstants; | ||
import org.dromara.common.core.domain.dto.UserOnlineDTO; | ||
import org.dromara.common.core.enums.UserType; | ||
import org.dromara.common.redis.utils.RedisUtils; | ||
import org.dromara.common.satoken.utils.OnlineUserUtil; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.Duration; | ||
|
||
/** | ||
* 默认在线登录缓存管理 | ||
* | ||
* @author hexm | ||
* @date 2023/07/14 17:22 | ||
*/ | ||
@Component | ||
@ConditionalOnMissingBean(OnlineUserCacheManagerInterface.class) | ||
public class DefaultOnlineUserCacheManager implements OnlineUserCacheManagerInterface { | ||
|
||
/** | ||
* 保存在线用户 | ||
* | ||
* @param userType 用户类型 | ||
* @param tokenValue token值 | ||
* @param loginModel 登录对象 | ||
*/ | ||
@Override | ||
public void setCache(UserType userType, String tokenValue, SaLoginModel loginModel) { | ||
UserOnlineDTO dto = OnlineUserUtil.getOnlineDTO(tokenValue); | ||
RedisUtils.setObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, dto); | ||
} | ||
|
||
/** | ||
* 保存在线用户 | ||
* | ||
* @param userType 用户类型 | ||
* @param tokenValue token值 | ||
* @param loginModel 登录对象 | ||
* @param timeout 超时时间 | ||
*/ | ||
@Override | ||
public void setCache(UserType userType, String tokenValue, SaLoginModel loginModel, Long timeout) { | ||
UserOnlineDTO dto = OnlineUserUtil.getOnlineDTO(tokenValue); | ||
RedisUtils.setObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, dto, Duration.ofSeconds(timeout)); | ||
} | ||
|
||
/** | ||
* 删除缓存 | ||
* | ||
* @param loginType 登录类型 | ||
* @param loginId 用户id | ||
* @param tokenValue token值 | ||
*/ | ||
@Override | ||
public void deleteCache(String loginType, Object loginId, String tokenValue) { | ||
RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...oken/src/main/java/org/dromara/common/satoken/online/OnlineUserCacheManagerInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.dromara.common.satoken.online; | ||
|
||
import cn.dev33.satoken.stp.SaLoginModel; | ||
import org.dromara.common.core.enums.UserType; | ||
|
||
/** | ||
* 在线用户管理 | ||
* | ||
* @author hexm | ||
* @date 2023/07/14 17:14 | ||
*/ | ||
public interface OnlineUserCacheManagerInterface { | ||
|
||
/** | ||
* 保存在线用户 | ||
* | ||
* @param userType 用户类型 | ||
* @param tokenValue token值 | ||
* @param loginModel 登录对象 | ||
*/ | ||
void setCache(UserType userType, String tokenValue, SaLoginModel loginModel); | ||
|
||
/** | ||
* 保存在线用户 | ||
* | ||
* @param userType 用户类型 | ||
* @param tokenValue token值 | ||
* @param loginModel 登录对象 | ||
* @param timeout 超时时间 | ||
*/ | ||
void setCache(UserType userType, String tokenValue, SaLoginModel loginModel, Long timeout); | ||
|
||
/** | ||
* 删除缓存 | ||
* | ||
* @param loginType 登录类型 | ||
* @param loginId 用户id | ||
* @param tokenValue token值 | ||
*/ | ||
void deleteCache(String loginType, Object loginId, String tokenValue); | ||
} |
37 changes: 37 additions & 0 deletions
37
...n/ruoyi-common-satoken/src/main/java/org/dromara/common/satoken/utils/OnlineUserUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.dromara.common.satoken.utils; | ||
|
||
import cn.hutool.http.useragent.UserAgent; | ||
import cn.hutool.http.useragent.UserAgentUtil; | ||
import org.dromara.common.core.domain.dto.UserOnlineDTO; | ||
import org.dromara.common.core.domain.model.LoginUser; | ||
import org.dromara.common.core.utils.ServletUtils; | ||
import org.dromara.common.core.utils.ip.AddressUtils; | ||
|
||
/** | ||
* @author hexm | ||
* @date 2023/07/14 17:30 | ||
*/ | ||
public class OnlineUserUtil { | ||
|
||
/** | ||
* 获取在线登录用户对象 | ||
* | ||
* @param tokenValue token值 | ||
* @return | ||
*/ | ||
public static UserOnlineDTO getOnlineDTO(String tokenValue) { | ||
UserAgent userAgent = UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent")); | ||
String ip = ServletUtils.getClientIP(); | ||
LoginUser user = LoginHelper.getUser(); | ||
UserOnlineDTO dto = new UserOnlineDTO(); | ||
dto.setIpaddr(ip); | ||
dto.setLoginLocation(AddressUtils.getRealAddressByIP(ip)); | ||
dto.setBrowser(userAgent.getBrowser().getName()); | ||
dto.setOs(userAgent.getOs().getName()); | ||
dto.setLoginTime(System.currentTimeMillis()); | ||
dto.setTokenId(tokenValue); | ||
dto.setUserName(user.getUsername()); | ||
dto.setDeptName(user.getDeptName()); | ||
return dto; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...n-tenant/src/main/java/org/dromara/common/tenant/online/TenantOnlineUserCacheManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package org.dromara.common.tenant.online; | ||
|
||
import cn.dev33.satoken.stp.SaLoginModel; | ||
import cn.hutool.core.util.StrUtil; | ||
import org.dromara.common.core.constant.CacheConstants; | ||
import org.dromara.common.core.constant.GlobalConstants; | ||
import org.dromara.common.core.domain.dto.UserOnlineDTO; | ||
import org.dromara.common.core.domain.model.LoginUser; | ||
import org.dromara.common.core.enums.UserType; | ||
import org.dromara.common.redis.utils.RedisUtils; | ||
import org.dromara.common.satoken.online.OnlineUserCacheManagerInterface; | ||
import org.dromara.common.satoken.utils.LoginHelper; | ||
import org.dromara.common.satoken.utils.OnlineUserUtil; | ||
import org.dromara.common.tenant.helper.TenantHelper; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.Duration; | ||
|
||
/** | ||
* 租户在线用户缓存管理 | ||
* | ||
* @author hexm | ||
* @date 2023/07/14 17:29 | ||
*/ | ||
@Primary | ||
@Component | ||
public class TenantOnlineUserCacheManager implements OnlineUserCacheManagerInterface { | ||
|
||
/** | ||
* 保存在线用户 | ||
* | ||
* @param userType 用户类型 | ||
* @param tokenValue token值 | ||
* @param loginModel 登录对象 | ||
*/ | ||
@Override | ||
public void setCache(UserType userType, String tokenValue, SaLoginModel loginModel) { | ||
UserOnlineDTO dto = OnlineUserUtil.getOnlineDTO(tokenValue); | ||
LoginUser user = LoginHelper.getUser(); | ||
RedisUtils.setObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, dto); | ||
RedisUtils.setObject(GlobalConstants.ONLINE_TOKEN_TENANT_ID_KEY + tokenValue, user.getTenantId()); | ||
} | ||
|
||
/** | ||
* 保存在线用户 | ||
* | ||
* @param userType 用户类型 | ||
* @param tokenValue token值 | ||
* @param loginModel 登录对象 | ||
* @param timeout 超时时间 | ||
*/ | ||
@Override | ||
public void setCache(UserType userType, String tokenValue, SaLoginModel loginModel, Long timeout) { | ||
LoginUser user = LoginHelper.getUser(); | ||
UserOnlineDTO dto = OnlineUserUtil.getOnlineDTO(tokenValue); | ||
RedisUtils.setObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, dto, Duration.ofSeconds(timeout)); | ||
RedisUtils.setObject(GlobalConstants.ONLINE_TOKEN_TENANT_ID_KEY + tokenValue, user.getTenantId(), Duration.ofSeconds(timeout)); | ||
} | ||
|
||
/** | ||
* 删除缓存 | ||
* | ||
* @param loginType 登录类型 | ||
* @param loginId 用户id | ||
* @param tokenValue token值 | ||
*/ | ||
@Override | ||
public void deleteCache(String loginType, Object loginId, String tokenValue) { | ||
String tenantId = RedisUtils.getObject(GlobalConstants.ONLINE_TOKEN_TENANT_ID_KEY + tokenValue); | ||
if (StrUtil.isNotBlank(tenantId)) { | ||
try { | ||
TenantHelper.setDynamic(tenantId); | ||
RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue); | ||
TenantHelper.clearDynamic(); | ||
} finally { | ||
RedisUtils.deleteObject(GlobalConstants.ONLINE_TOKEN_TENANT_ID_KEY + tokenValue); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.