bucket = CLIENT.getBucket(key);
+ return bucket.setIfExists(value, duration);
+ }
+
/**
* 注册对象监听器
*
@@ -243,6 +255,18 @@ public class RedisUtils {
return rList.addAll(dataList);
}
+ /**
+ * 追加缓存List数据
+ *
+ * @param key 缓存的键值
+ * @param data 待缓存的数据
+ * @return 缓存的对象
+ */
+ public static boolean addCacheList(final String key, final T data) {
+ RList rList = CLIENT.getList(key);
+ return rList.add(data);
+ }
+
/**
* 注册List监听器
*
@@ -267,6 +291,19 @@ public class RedisUtils {
return rList.readAll();
}
+ /**
+ * 获得缓存的list对象(范围)
+ *
+ * @param key 缓存的键值
+ * @param form 起始下标
+ * @param to 截止下标
+ * @return 缓存键值对应的数据
+ */
+ public static List getCacheListRange(final String key, int form, int to) {
+ RList rList = CLIENT.getList(key);
+ return rList.range(form, to);
+ }
+
/**
* 缓存Set
*
@@ -279,6 +316,18 @@ public class RedisUtils {
return rSet.addAll(dataSet);
}
+ /**
+ * 追加缓存Set数据
+ *
+ * @param key 缓存的键值
+ * @param data 待缓存的数据
+ * @return 缓存的对象
+ */
+ public static boolean addCacheSet(final String key, final T data) {
+ RSet rSet = CLIENT.getSet(key);
+ return rSet.add(data);
+ }
+
/**
* 注册Set监听器
*
diff --git a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/config/SaTokenConfig.java b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/config/SaTokenConfig.java
index 5915baa..aab2380 100644
--- a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/config/SaTokenConfig.java
+++ b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/config/SaTokenConfig.java
@@ -19,7 +19,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
*/
@AutoConfiguration
@PropertySource(value = "classpath:common-satoken.yml", factory = YmlPropertySourceFactory.class)
-public class SaTokenConfig implements WebMvcConfigurer {
+public class SaTokenConfig {
@Bean
public StpLogic getStpLogicJwt() {
diff --git a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/core/dao/FlexSaTokenDao.java b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/core/dao/FlexSaTokenDao.java
index 3736e96..627e35c 100644
--- a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/core/dao/FlexSaTokenDao.java
+++ b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/core/dao/FlexSaTokenDao.java
@@ -45,12 +45,9 @@ public class FlexSaTokenDao implements SaTokenDao {
*/
@Override
public void update(String key, String value) {
- long expire = getTimeout(key);
- // -2 = 无此键
- if (expire == NOT_VALUE_EXPIRE) {
- return;
+ if (RedisUtils.hasKey(key)) {
+ RedisUtils.setCacheObject(key, value, true);
}
- this.set(key, value, expire);
}
/**
@@ -75,17 +72,6 @@ public class FlexSaTokenDao implements SaTokenDao {
*/
@Override
public void updateTimeout(String key, long timeout) {
- // 判断是否想要设置为永久
- if (timeout == NEVER_EXPIRE) {
- long expire = getTimeout(key);
- if (expire == NEVER_EXPIRE) {
- // 如果其已经被设置为永久,则不作任何处理
- } else {
- // 如果尚未被设置为永久,那么再次set一次
- this.set(key, this.get(key), timeout);
- }
- return;
- }
RedisUtils.expire(key, Duration.ofSeconds(timeout));
}
@@ -119,12 +105,9 @@ public class FlexSaTokenDao implements SaTokenDao {
*/
@Override
public void updateObject(String key, Object object) {
- long expire = getObjectTimeout(key);
- // -2 = 无此键
- if (expire == NOT_VALUE_EXPIRE) {
- return;
+ if (RedisUtils.hasKey(key)) {
+ RedisUtils.setCacheObject(key, object, true);
}
- this.setObject(key, object, expire);
}
/**
@@ -149,17 +132,6 @@ public class FlexSaTokenDao implements SaTokenDao {
*/
@Override
public void updateObjectTimeout(String key, long timeout) {
- // 判断是否想要设置为永久
- if (timeout == NEVER_EXPIRE) {
- long expire = getObjectTimeout(key);
- if (expire == NEVER_EXPIRE) {
- // 如果其已经被设置为永久,则不作任何处理
- } else {
- // 如果尚未被设置为永久,那么再次set一次
- this.setObject(key, this.getObject(key), timeout);
- }
- return;
- }
RedisUtils.expire(key, Duration.ofSeconds(timeout));
}
diff --git a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java
index 8d4687e..8883600 100644
--- a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java
+++ b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/handler/GlobalExceptionHandler.java
@@ -11,7 +11,6 @@ import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import com.ruoyi.common.core.core.domain.R;
-import com.ruoyi.common.core.exception.DemoModeException;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StreamUtils;
import org.springframework.context.support.DefaultMessageSourceResolvable;
@@ -161,12 +160,4 @@ public class GlobalExceptionHandler {
String message = e.getBindingResult().getFieldError().getDefaultMessage();
return R.fail(message);
}
-
- /**
- * 演示模式异常
- */
- @ExceptionHandler(DemoModeException.class)
- public R handleDemoModeException(DemoModeException e) {
- return R.fail("演示模式,不允许操作");
- }
}
diff --git a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/utils/LoginHelper.java b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/utils/LoginHelper.java
index cbe802b..51b750a 100644
--- a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/utils/LoginHelper.java
+++ b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/utils/LoginHelper.java
@@ -15,6 +15,7 @@ import com.ruoyi.common.core.core.domain.model.LoginUser;
import com.ruoyi.common.core.enums.UserType;
import java.util.Set;
+import java.util.function.Supplier;
/**
* 登录鉴权助手
@@ -36,6 +37,7 @@ public class LoginHelper {
public static final String USER_KEY = "userId";
public static final String DEPT_KEY = "deptId";
public static final String CLIENT_KEY = "clientid";
+ public static final String TENANT_ADMIN_KEY = "isTenantAdmin";
/**
* 登录系统 基于 设备类型
@@ -56,32 +58,45 @@ public class LoginHelper {
model.setExtra(TENANT_KEY, loginUser.getTenantId())
.setExtra(USER_KEY, loginUser.getUserId())
.setExtra(DEPT_KEY, loginUser.getDeptId()));
- StpUtil.getSession().set(LOGIN_USER_KEY, loginUser);
+ //StpUtil.getSession().set(LOGIN_USER_KEY, loginUser);
+ StpUtil.getTokenSession().set(LOGIN_USER_KEY, loginUser);
}
/**
* 获取用户(多级缓存)
*/
public static LoginUser getLoginUser() {
- LoginUser loginUser = (LoginUser) SaHolder.getStorage().get(LOGIN_USER_KEY);
- if (loginUser != null) {
- return loginUser;
- }
- SaSession session = StpUtil.getSession();
- if (ObjectUtil.isNull(session)) {
- return null;
- }
- loginUser = (LoginUser) session.get(LOGIN_USER_KEY);
- SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser);
- return loginUser;
+// LoginUser loginUser = (LoginUser) SaHolder.getStorage().get(LOGIN_USER_KEY);
+// if (loginUser != null) {
+// return loginUser;
+// }
+// SaSession session = StpUtil.getSession();
+// if (ObjectUtil.isNull(session)) {
+// return null;
+// }
+// loginUser = (LoginUser) session.get(LOGIN_USER_KEY);
+// SaHolder.getStorage().set(LOGIN_USER_KEY, loginUser);
+// return loginUser;
+ return (LoginUser) getStorageIfAbsentSet(LOGIN_USER_KEY, () -> {
+ SaSession session = StpUtil.getTokenSession();
+ if (ObjectUtil.isNull(session)) {
+ return null;
+ }
+ return session.get(LOGIN_USER_KEY);
+ });
}
/**
* 获取用户基于token
*/
public static LoginUser getLoginUser(String token) {
- Object loginId = StpUtil.getLoginIdByToken(token);
- SaSession session = StpUtil.getSessionByLoginId(loginId);
+// Object loginId = StpUtil.getLoginIdByToken(token);
+// SaSession session = StpUtil.getSessionByLoginId(loginId);
+// if (ObjectUtil.isNull(session)) {
+// return null;
+// }
+// return (LoginUser) session.get(LOGIN_USER_KEY);
+ SaSession session = StpUtil.getTokenSessionByToken(token);
if (ObjectUtil.isNull(session)) {
return null;
}
@@ -98,8 +113,8 @@ public class LoginHelper {
/**
* 获取租户ID
*/
- public static String getTenantId() {
- return Convert.toStr(getExtra(TENANT_KEY));
+ public static Long getTenantId() {
+ return Convert.toLong(getExtra(TENANT_KEY));
}
/**
@@ -110,17 +125,18 @@ public class LoginHelper {
}
private static Object getExtra(String key) {
- Object obj;
- try {
- obj = SaHolder.getStorage().get(key);
- if (ObjectUtil.isNull(obj)) {
- obj = StpUtil.getExtra(key);
- SaHolder.getStorage().set(key, obj);
- }
- } catch (Exception e) {
- return null;
- }
- return obj;
+// Object obj;
+// try {
+// obj = SaHolder.getStorage().get(key);
+// if (ObjectUtil.isNull(obj)) {
+// obj = StpUtil.getExtra(key);
+// SaHolder.getStorage().set(key, obj);
+// }
+// } catch (Exception e) {
+// return null;
+// }
+// return obj;
+ return getStorageIfAbsentSet(key, () -> StpUtil.getExtra(key));
}
/**
@@ -163,7 +179,27 @@ public class LoginHelper {
}
public static boolean isTenantAdmin() {
- return isTenantAdmin(getLoginUser().getRolePermission());
+ Object value = getStorageIfAbsentSet(TENANT_ADMIN_KEY, () -> {
+ return isTenantAdmin(getLoginUser().getRolePermission());
+ });
+ return Convert.toBool(value);
+ }
+
+ public static boolean isLogin() {
+ return getLoginUser() != null;
+ }
+
+ public static Object getStorageIfAbsentSet(String key, Supplier