mirror of
https://gitee.com/dromara/sa-token.git
synced 2026-05-14 12:52:08 +08:00
refactor: 重构 SaTokenDao 接口,拆分存储与序列化操作
This commit is contained in:
@@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package cn.dev33.satoken.dao;
|
package cn.dev33.satoken.dao;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.SaManager;
|
||||||
import cn.dev33.satoken.session.SaSession;
|
import cn.dev33.satoken.session.SaSession;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -90,45 +91,62 @@ public interface SaTokenDao {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取 Object,如无返空
|
* 获取 Object,如无返空
|
||||||
* @param key 键名称
|
*
|
||||||
|
* @param key 键名称
|
||||||
* @return object
|
* @return object
|
||||||
*/
|
*/
|
||||||
Object getObject(String key);
|
default Object getObject(String key) {
|
||||||
|
String jsonString = get(key);
|
||||||
|
return SaManager.getSaJsonTemplate().jsonToObject(jsonString);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 写入 Object,并设定存活时间 (单位: 秒)
|
* 写入 Object,并设定存活时间 (单位: 秒)
|
||||||
* @param key 键名称
|
*
|
||||||
* @param object 值
|
* @param key 键名称
|
||||||
|
* @param object 值
|
||||||
* @param timeout 存活时间(值大于0时限时存储,值=-1时永久存储,值=0或小于-2时不存储)
|
* @param timeout 存活时间(值大于0时限时存储,值=-1时永久存储,值=0或小于-2时不存储)
|
||||||
*/
|
*/
|
||||||
void setObject(String key, Object object, long timeout);
|
default void setObject(String key, Object object, long timeout) {
|
||||||
|
String jsonString = SaManager.getSaJsonTemplate().objectToJson(object);
|
||||||
|
set(key, jsonString, timeout);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 更新 Object (过期时间不变)
|
* 更新 Object (过期时间不变)
|
||||||
* @param key 键名称
|
* @param key 键名称
|
||||||
* @param object 值
|
* @param object 值
|
||||||
*/
|
*/
|
||||||
void updateObject(String key, Object object);
|
default void updateObject(String key, Object object) {
|
||||||
|
String jsonString = SaManager.getSaJsonTemplate().objectToJson(object);
|
||||||
|
update(key, jsonString);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除 Object
|
* 删除 Object
|
||||||
* @param key 键名称
|
* @param key 键名称
|
||||||
*/
|
*/
|
||||||
void deleteObject(String key);
|
default void deleteObject(String key) {
|
||||||
|
delete(key);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取 Object 的剩余存活时间 (单位: 秒)
|
* 获取 Object 的剩余存活时间 (单位: 秒)
|
||||||
* @param key 指定 key
|
* @param key 指定 key
|
||||||
* @return 这个 key 的剩余存活时间
|
* @return 这个 key 的剩余存活时间
|
||||||
*/
|
*/
|
||||||
long getObjectTimeout(String key);
|
default long getObjectTimeout(String key) {
|
||||||
|
return getTimeout(key);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改 Object 的剩余存活时间(单位: 秒)
|
* 修改 Object 的剩余存活时间(单位: 秒)
|
||||||
* @param key 指定 key
|
* @param key 指定 key
|
||||||
* @param timeout 剩余存活时间
|
* @param timeout 剩余存活时间
|
||||||
*/
|
*/
|
||||||
void updateObjectTimeout(String key, long timeout);
|
default void updateObjectTimeout(String key, long timeout) {
|
||||||
|
updateTimeout(key, timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// --------------------- SaSession 读写 (默认复用 Object 读写方法) ---------------------
|
// --------------------- SaSession 读写 (默认复用 Object 读写方法) ---------------------
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import cn.dev33.satoken.stp.SaLoginConfig;
|
|||||||
import cn.dev33.satoken.stp.StpUtil;
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
import cn.dev33.satoken.util.SaFoxUtil;
|
import cn.dev33.satoken.util.SaFoxUtil;
|
||||||
import cn.dev33.satoken.util.SaResult;
|
import cn.dev33.satoken.util.SaResult;
|
||||||
|
import com.pj.model.SysUser;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
@@ -35,7 +36,13 @@ public class TestController {
|
|||||||
System.out.println("------------进来了 " + SaFoxUtil.formatDate(new Date()));
|
System.out.println("------------进来了 " + SaFoxUtil.formatDate(new Date()));
|
||||||
// StpUtil.getLoginId();
|
// StpUtil.getLoginId();
|
||||||
// StpUtil.getAnonTokenSession();
|
// StpUtil.getAnonTokenSession();
|
||||||
StpUtil.setTokenValue("xxx");
|
// StpUtil.setTokenValue("xxx");
|
||||||
|
StpUtil.getSession().set("name", "zhang");
|
||||||
|
StpUtil.getSession().set("age", 18);
|
||||||
|
SysUser user = new SysUser(10001, "lisi", 22);
|
||||||
|
StpUtil.getSession().set("user", user);
|
||||||
|
StpUtil.getTokenSession().set("user", user);
|
||||||
|
|
||||||
// 返回
|
// 返回
|
||||||
return SaResult.data(null);
|
return SaResult.data(null);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,24 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
package cn.dev33.satoken.dao;
|
package cn.dev33.satoken.dao;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import cn.dev33.satoken.strategy.SaStrategy;
|
||||||
import java.time.LocalDate;
|
import cn.dev33.satoken.util.SaFoxUtil;
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.time.LocalTime;
|
|
||||||
import java.time.format.DateTimeFormatter;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
||||||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
|
||||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||||
@@ -42,9 +26,23 @@ import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
|
|||||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
|
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
|
||||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
||||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
|
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
||||||
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import cn.dev33.satoken.strategy.SaStrategy;
|
import java.lang.reflect.Field;
|
||||||
import cn.dev33.satoken.util.SaFoxUtil;
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.LocalTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sa-Token 持久层实现 [ Redis存储、Jackson序列化 ]
|
* Sa-Token 持久层实现 [ Redis存储、Jackson序列化 ]
|
||||||
@@ -225,80 +223,6 @@ public class SaTokenDaoRedisJackson implements SaTokenDao {
|
|||||||
}
|
}
|
||||||
stringRedisTemplate.expire(key, timeout, TimeUnit.SECONDS);
|
stringRedisTemplate.expire(key, timeout, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取Object,如无返空
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Object getObject(String key) {
|
|
||||||
return objectRedisTemplate.opsForValue().get(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 写入Object,并设定存活时间 (单位: 秒)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void setObject(String key, Object object, long timeout) {
|
|
||||||
if(timeout == 0 || timeout <= SaTokenDao.NOT_VALUE_EXPIRE) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// 判断是否为永不过期
|
|
||||||
if(timeout == SaTokenDao.NEVER_EXPIRE) {
|
|
||||||
objectRedisTemplate.opsForValue().set(key, object);
|
|
||||||
} else {
|
|
||||||
objectRedisTemplate.opsForValue().set(key, object, timeout, TimeUnit.SECONDS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 更新Object (过期时间不变)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void updateObject(String key, Object object) {
|
|
||||||
long expire = getObjectTimeout(key);
|
|
||||||
// -2 = 无此键
|
|
||||||
if(expire == SaTokenDao.NOT_VALUE_EXPIRE) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.setObject(key, object, expire);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除Object
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void deleteObject(String key) {
|
|
||||||
objectRedisTemplate.delete(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取Object的剩余存活时间 (单位: 秒)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public long getObjectTimeout(String key) {
|
|
||||||
return objectRedisTemplate.getExpire(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 修改Object的剩余存活时间 (单位: 秒)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void updateObjectTimeout(String key, long timeout) {
|
|
||||||
// 判断是否想要设置为永久
|
|
||||||
if(timeout == SaTokenDao.NEVER_EXPIRE) {
|
|
||||||
long expire = getObjectTimeout(key);
|
|
||||||
if(expire == SaTokenDao.NEVER_EXPIRE) {
|
|
||||||
// 如果其已经被设置为永久,则不作任何处理
|
|
||||||
} else {
|
|
||||||
// 如果尚未被设置为永久,那么再次set一次
|
|
||||||
this.setObject(key, this.getObject(key), timeout);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
objectRedisTemplate.expire(key, timeout, TimeUnit.SECONDS);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ package cn.dev33.satoken.spring.json;
|
|||||||
import cn.dev33.satoken.error.SaSpringBootErrorCode;
|
import cn.dev33.satoken.error.SaSpringBootErrorCode;
|
||||||
import cn.dev33.satoken.exception.SaJsonConvertException;
|
import cn.dev33.satoken.exception.SaJsonConvertException;
|
||||||
import cn.dev33.satoken.json.SaJsonTemplate;
|
import cn.dev33.satoken.json.SaJsonTemplate;
|
||||||
|
import cn.dev33.satoken.util.SaFoxUtil;
|
||||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
@@ -81,6 +82,9 @@ public class SaJsonTemplateForJackson implements SaJsonTemplate {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Object jsonToObject(String jsonStr) {
|
public Object jsonToObject(String jsonStr) {
|
||||||
|
if(SaFoxUtil.isEmpty(jsonStr)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
Object value = objectMapper.readValue(jsonStr, Object.class);
|
Object value = objectMapper.readValue(jsonStr, Object.class);
|
||||||
return value;
|
return value;
|
||||||
@@ -94,6 +98,9 @@ public class SaJsonTemplateForJackson implements SaJsonTemplate {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> jsonToMap(String jsonStr) {
|
public Map<String, Object> jsonToMap(String jsonStr) {
|
||||||
|
if(SaFoxUtil.isEmpty(jsonStr)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Map<String, Object> map = objectMapper.readValue(jsonStr, Map.class);
|
Map<String, Object> map = objectMapper.readValue(jsonStr, Map.class);
|
||||||
|
|||||||
Reference in New Issue
Block a user