This commit is contained in:
77
2024-05-18 01:02:13 +08:00
parent 874b33557d
commit 32933c2840
47 changed files with 416 additions and 282 deletions

View File

@@ -1,51 +0,0 @@
package com.ruoyi.web.cache;
import com.ruoyi.component.core.constant.CacheConstants;
import org.redisson.api.RSet;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Set;
@Component
public class OnlineUserTodayCache {
@Autowired
private RedissonClient redissonClient;
private String getKey(LocalDate dateTime){
String today = dateTime.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
return String.format(CacheConstants.ONLINE_TODAY_TOKEN_KEY,today);
}
public boolean addOnlineUserId(Long userId, LocalDate dateTime){
RSet<Long> set = redissonClient.getSet(getKey(dateTime));
boolean res = set.add(userId);
set.expire(Duration.ofDays(3));
return res;
}
public Set<Long> getAllOnlineToday(){
RSet<Long> set = redissonClient.getSet(getKey(LocalDate.now()));
return set.readAll();
}
public Long getOnlineNum(LocalDate time){
RSet<Long> set = redissonClient.getSet(getKey(time));
return (long) set.size();
}
public Long getOnlineTodayNum(){
RSet<Long> set = redissonClient.getSet(getKey(LocalDate.now()));
return (long) set.size();
}
public Long getOnlineLastNum(){
RSet<Long> set = redissonClient.getSet(getKey(LocalDate.now().plusDays(-1)));
return (long) set.size();
}
}

View File

@@ -7,6 +7,7 @@ import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.ruoyi.component.core.constant.CacheConstants;
import com.ruoyi.component.core.constant.Constants;
import com.ruoyi.component.core.constant.GlobalConstants;
import com.ruoyi.component.core.domain.R;
import com.ruoyi.component.core.util.StringUtils;
import com.ruoyi.component.core.util.reflect.ReflectUtils;
@@ -59,7 +60,7 @@ public class CaptchaController {
*/
@GetMapping("/captchaSms")
public R<Void> smsCaptcha(@NotBlank(message = "{user.phonenumber.not.blank}") String phonenumber) {
String key = CacheConstants.CAPTCHA_CODE_KEY + phonenumber;
String key = GlobalConstants.CAPTCHA_CODE_KEY + phonenumber;
String code = RandomUtil.randomNumbers(4);
RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
// 验证码模板id 自行处理 (查数据库或写死均可)
@@ -85,7 +86,7 @@ public class CaptchaController {
if (!mailProperties.getEnabled()) {
return R.fail("当前系统没有开启邮箱功能!");
}
String key = CacheConstants.CAPTCHA_CODE_KEY + email;
String key = GlobalConstants.CAPTCHA_CODE_KEY + email;
String code = RandomUtil.randomNumbers(4);
RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
try {
@@ -110,7 +111,7 @@ public class CaptchaController {
}
// 保存验证码信息
String uuid = IdUtil.simpleUUID();
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
String verifyKey = GlobalConstants.CAPTCHA_CODE_KEY + uuid;
// 生成验证码
CaptchaType captchaType = captchaProperties.getType();
boolean isMath = CaptchaType.MATH == captchaType;

View File

@@ -3,17 +3,18 @@ package com.ruoyi.web.listener;
import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.listener.SaTokenListener;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import com.ruoyi.component.core.constant.CacheConstants;
import com.ruoyi.component.core.domain.dto.UserOnlineDTO;
import com.ruoyi.component.core.domain.model.LoginUser;
import com.ruoyi.component.core.enums.UserType;
import com.ruoyi.component.redis.util.RedisUtils;
import com.ruoyi.component.satoken.utils.LoginHelper;
import com.ruoyi.component.core.util.ServletUtils;
import com.ruoyi.component.core.util.ip.AddressUtils;
import com.ruoyi.web.cache.OnlineUserTodayCache;
import com.ruoyi.component.redis.util.RedisUtils;
import com.ruoyi.component.satoken.utils.LoginHelper;
import com.ruoyi.component.tenant.helper.TenantHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@@ -31,7 +32,6 @@ import java.time.Duration;
public class UserActionListener implements SaTokenListener {
private final SaTokenConfig tokenConfig;
private final OnlineUserTodayCache onlineUserTodayCache;
/**
* 每次登录时触发
@@ -54,9 +54,13 @@ public class UserActionListener implements SaTokenListener {
dto.setDeptName(user.getDeptName());
dto.setUserType(UserType.SYS_USER.getUserType());
if(tokenConfig.getTimeout() == -1) {
RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, dto);
TenantHelper.dynamic(user.getTenantId(),() -> {
RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, dto);
});
} else {
RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, dto, Duration.ofSeconds(tokenConfig.getTimeout()));
TenantHelper.dynamic(user.getTenantId(),() -> {
RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, dto, Duration.ofSeconds(tokenConfig.getTimeout()));
});
}
log.info("user doLogin, userId:{}, token:{}", loginId, tokenValue);
} else if (userType == UserType.APP_USER) {
@@ -74,9 +78,13 @@ public class UserActionListener implements SaTokenListener {
dto.setUserName(user.getUsername());
dto.setDeptName(user.getDeptName());
if(tokenConfig.getTimeout() == -1) {
RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, dto);
TenantHelper.dynamic(user.getTenantId(),() -> {
RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, dto);
});
} else {
RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, dto, Duration.ofSeconds(tokenConfig.getTimeout()));
TenantHelper.dynamic(user.getTenantId(),() -> {
RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, dto, Duration.ofSeconds(tokenConfig.getTimeout()));
});
}
}
}
@@ -86,7 +94,12 @@ public class UserActionListener implements SaTokenListener {
*/
@Override
public void doLogout(String loginType, Object loginId, String tokenValue) {
RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue);
String tenantId = LoginHelper.getTenantIdByToken(tokenValue);
if(tenantId != null){
TenantHelper.dynamic(tenantId,() -> {
RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue);
});
}
log.info("user doLogout, userId:{}, token:{}", loginId, tokenValue);
}
@@ -95,7 +108,12 @@ public class UserActionListener implements SaTokenListener {
*/
@Override
public void doKickout(String loginType, Object loginId, String tokenValue) {
RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue);
String tenantId = LoginHelper.getTenantIdByToken(tokenValue);
if(tenantId != null){
TenantHelper.dynamic(tenantId,() -> {
RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue);
});
}
log.info("user doLogoutByLoginId, userId:{}, token:{}", loginId, tokenValue);
}
@@ -104,7 +122,12 @@ public class UserActionListener implements SaTokenListener {
*/
@Override
public void doReplaced(String loginType, Object loginId, String tokenValue) {
RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue);
String tenantId = LoginHelper.getTenantIdByToken(tokenValue);
if(tenantId != null){
TenantHelper.dynamic(tenantId,() -> {
RedisUtils.deleteObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue);
});
}
log.info("user doReplaced, userId:{}, token:{}", loginId, tokenValue);
}

View File

@@ -21,7 +21,7 @@ spring:
driverClassName: com.mysql.cj.jdbc.Driver
# jdbc 所有参数配置参考 https://lionli.blog.csdn.net/article/details/122018562
# rewriteBatchedStatements=true 批处理优化 大幅提升批量插入更新删除性能(对数据库有性能损耗 使用批量操作应考虑性能问题)
url: jdbc:mysql://localhost:4306/cai-v2?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true
url: jdbc:mysql://localhost:5306/dk-sass?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true
username: root
password: 383200134
# 从库数据源
@@ -92,18 +92,6 @@ redisson:
subscriptionConnectionPoolSize: 50
mail:
enabled: false
agora:
app-id: 58ff3a37d91d48c7a8ef7a56fb8f62d0
key: 0cca1a53262c4c74b0a8c653a9b7540e
secret: 4a4f734285f34aea86cef63b2a186f27
yunxin:
app-key: 0aaefeb8a80a9889987c5346244b58e2
app-secret: 470345ca2832
cai:
enable-api-encryption: true
open-pay-proxy: false
proxy-ip: 159.75.218.177
proxy-host: 7693
springdoc:
api-docs:
enabled: false

View File

@@ -144,6 +144,25 @@ security:
# - /actuator
# - /actuator/**
# 多租户配置
tenant:
# 是否开启
enable: true
# 排除表
excludes:
- sys_menu
- sys_tenant
- sys_tenant_package
- sys_role_dept
- sys_role_menu
- sys_user_post
- sys_user_role
- sys_client
- sys_oss_config
- sys_dict_type
- sys_dict_data
- sys_config
# MyBatisPlus配置
# https://baomidou.com/config/
mybatis-plus: