mirror of
https://gitee.com/dromara/sa-token.git
synced 2026-02-27 16:50:24 +08:00
修复 sa-token-dao-redis-fastjson 插件 session.getModel 无法反序列化实体类的问题。
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
<module>sa-token-dao-redis</module>
|
||||
<module>sa-token-dao-redis-jackson</module>
|
||||
<module>sa-token-dao-redis-fastjson</module>
|
||||
<module>sa-token-dao-redis-fastjson2</module>
|
||||
<module>sa-token-dao-redisx</module>
|
||||
<module>sa-token-dialect-thymeleaf</module>
|
||||
<module>sa-token-sso</module>
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
<version>2.3.3.RELEASE</version>
|
||||
</dependency>
|
||||
<!-- fastjson -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package cn.dev33.satoken.dao;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
|
||||
import cn.dev33.satoken.session.SaSession;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
|
||||
/**
|
||||
* Fastjson 定制版 SaSession,重写类型转换API
|
||||
*
|
||||
* @author kong
|
||||
* @since 2022-10-19
|
||||
*/
|
||||
public class SaSessionForFastjsonCustomized extends SaSession {
|
||||
|
||||
private static final long serialVersionUID = -7600983549653130681L;
|
||||
|
||||
public SaSessionForFastjsonCustomized() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建一个 SaSession 对象
|
||||
* @param id Session 的 id
|
||||
*/
|
||||
public SaSessionForFastjsonCustomized(String id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取值 (指定转换类型)
|
||||
* @param <T> 泛型
|
||||
* @param key key
|
||||
* @param cs 指定转换类型
|
||||
* @return 值
|
||||
*/
|
||||
@Override
|
||||
public <T> T getModel(String key, Class<T> cs) {
|
||||
if(SaFoxUtil.isBasicType(cs)) {
|
||||
return SaFoxUtil.getValueByType(get(key), cs);
|
||||
}
|
||||
return JSON.parseObject(getString(key), cs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取值 (指定转换类型, 并指定值为Null时返回的默认值)
|
||||
* @param <T> 泛型
|
||||
* @param key key
|
||||
* @param cs 指定转换类型
|
||||
* @param defaultValue 值为Null时返回的默认值
|
||||
* @return 值
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getModel(String key, Class<T> cs, Object defaultValue) {
|
||||
Object value = get(key);
|
||||
if(valueIsNull(value)) {
|
||||
return (T)defaultValue;
|
||||
}
|
||||
if(SaFoxUtil.isBasicType(cs)) {
|
||||
return SaFoxUtil.getValueByType(get(key), cs);
|
||||
}
|
||||
return JSON.parseObject(getString(key), cs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 忽略 timeout 字段的序列化
|
||||
*/
|
||||
@Override
|
||||
@JSONField(serialize = false)
|
||||
public long getTimeout() {
|
||||
return super.getTimeout();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,18 +1,21 @@
|
||||
package cn.dev33.satoken.dao;
|
||||
|
||||
import cn.dev33.satoken.session.SaSession;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
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.StringRedisTemplate;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
||||
import cn.dev33.satoken.session.SaSession;
|
||||
import cn.dev33.satoken.strategy.SaStrategy;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
|
||||
/**
|
||||
* Sa-Token持久层接口 [Redis版 (使用JSON字符串进行序列化)]
|
||||
@@ -44,6 +47,10 @@ public class SaTokenDaoRedisFastjson implements SaTokenDao {
|
||||
if(this.isInit) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 重写 SaSession 生成策略
|
||||
SaStrategy.me.createSession = (sessionId) -> new SaSessionForFastjsonCustomized(sessionId);
|
||||
System.out.println("------------------ 执行了");
|
||||
|
||||
// 指定相应的序列化方案
|
||||
StringRedisSerializer keySerializer = new StringRedisSerializer();
|
||||
@@ -155,7 +162,7 @@ public class SaTokenDaoRedisFastjson implements SaTokenDao {
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
return JSON.parseObject(obj.toString(), SaSession.class);
|
||||
return JSON.parseObject(obj.toString(), SaSessionForFastjsonCustomized.class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -32,7 +32,7 @@ import cn.dev33.satoken.strategy.SaStrategy;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
|
||||
/**
|
||||
* Sa-Token持久层接口 [Redis版] (使用 jackson 序列化方式)
|
||||
* Sa-Token 持久层实现 [Redis存储、Jackson序列化]
|
||||
*
|
||||
* @author kong
|
||||
*
|
||||
@@ -97,7 +97,7 @@ public class SaTokenDaoRedisJackson implements SaTokenDao {
|
||||
timeModule.addSerializer(new LocalTimeSerializer(TIME_FORMATTER));
|
||||
timeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(TIME_FORMATTER));
|
||||
this.objectMapper.registerModule(timeModule);
|
||||
// 重写Session生成策略
|
||||
// 重写 SaSession 生成策略
|
||||
SaStrategy.me.createSession = (sessionId) -> new SaSessionForJacksonCustomized(sessionId);
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getMessage());
|
||||
|
||||
@@ -16,7 +16,7 @@ import org.springframework.stereotype.Component;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
|
||||
/**
|
||||
* Sa-Token持久层接口 [Redis版 (使用JDK默认序列化方式)]
|
||||
* Sa-Token 持久层实现 [Redis存储、JDK默认序列化]
|
||||
*
|
||||
* @author kong
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user