添加支持redis的String作为session序列化的方式

This commit is contained in:
sikadai 2022-07-18 18:39:25 +08:00
parent 1252d66267
commit 54d435b457
7 changed files with 353 additions and 48 deletions

View File

@ -108,7 +108,14 @@ public class SaSession implements Serializable {
/**
* 此Session绑定的token签名列表
*/
private final List<TokenSign> tokenSignList = new Vector<>();
private Vector<TokenSign> tokenSignList = new Vector<>();
/**
* 设置此Session绑定的token签名列表-反序列化需要
*/
public void setTokenSignList(Vector<TokenSign> tokenSignList) {
this.tokenSignList = tokenSignList;
}
/**
* 此Session绑定的token签名列表
@ -597,6 +604,4 @@ public class SaSession implements Serializable {
return dataMap.keySet();
}
}

View File

@ -3,62 +3,77 @@ package cn.dev33.satoken.session;
import java.io.Serializable;
/**
* Token 签名 Model
*
* Token 签名 Model
* <p>
* 挂在到SaSession上的token签名
*
* @author kong
*
* @author kong
*/
public class TokenSign implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1406115065849845073L;
/**
*
*/
private static final long serialVersionUID = 1406115065849845073L;
/**
* token值
*/
private String value;
/**
* token值
*/
private String value;
/**
* 所属设备类型
*/
private String device;
/**
* 所属设备类型
*/
private String device;
/** 构建一个 */
public TokenSign() {
}
/**
* 构建一个
*/
public TokenSign() {
}
/**
* 构建一个
*
* @param value token值
* @param device 所属设备类型
*/
public TokenSign(String value, String device) {
this.value = value;
this.device = device;
}
/**
* 构建一个
*
* @param value token值
* @param device 所属设备类型
*/
public TokenSign(String value, String device) {
this.value = value;
this.device = device;
}
/**
* @return token值
*/
public String getValue() {
return value;
}
/**
* @return token值
*/
public String getValue() {
return value;
}
/**
* @return 所属设备类型
*/
public String getDevice() {
return device;
}
/**
* @return 所属设备类型
*/
public String getDevice() {
return device;
}
@Override
public String toString() {
return "TokenSign [value=" + value + ", device=" + device + "]";
}
/**
* 反序列化需要
*/
public void setValue(String value) {
this.value = value;
}
/**
* 反序列化需要
*/
public void setDevice(String device) {
this.device = device;
}
@Override
public String toString() {
return "TokenSign [value=" + value + ", device=" + device + "]";
}
}

View File

@ -22,6 +22,7 @@
<module>sa-token-dao-redis</module>
<module>sa-token-dao-redis-jackson</module>
<module>sa-token-dao-redisx</module>
<module>sa-token-dao-redis-string</module>
<module>sa-token-dialect-thymeleaf</module>
<module>sa-token-sso</module>
<module>sa-token-oauth2</module>

View File

@ -0,0 +1,13 @@
target/
node_modules/
bin/
.settings/
unpackage/
.classpath
.project
.factorypath
.idea/
.iml

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>sa-token-plugin</artifactId>
<groupId>cn.dev33</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sa-token-dao-redix-string</artifactId>
<dependencies>
<!-- sa-token-spring-boot-starter -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-core</artifactId>
<version>${revision}</version>
</dependency>
<!-- RedisTemplate 相关操作API -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,236 @@
package cn.dev33.satoken.dao;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.util.SaFoxUtil;
import com.alibaba.fastjson.JSON;
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;
/**
* Sa-Token持久层接口 [Redis版 (使用JDK默认序列化方式)]
*
* @author kong
*
*/
@Component
public class SaTokenDaoRedisString implements SaTokenDao {
/**
* String专用
*/
public StringRedisTemplate stringRedisTemplate;
/**
* Objecy专用
*/
public StringRedisTemplate objectRedisTemplate;
/**
* 标记是否已初始化成功
*/
public boolean isInit;
@Autowired
public void init(RedisConnectionFactory connectionFactory) {
// 指定相应的序列化方案
StringRedisSerializer keySerializer = new StringRedisSerializer();
StringRedisSerializer valueSerializer = new StringRedisSerializer();
// 构建StringRedisTemplate
StringRedisTemplate stringTemplate = new StringRedisTemplate();
stringTemplate.setConnectionFactory(connectionFactory);
stringTemplate.afterPropertiesSet();
// 构建RedisTemplate
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(connectionFactory);
template.setKeySerializer(keySerializer);
template.setHashKeySerializer(keySerializer);
template.setValueSerializer(valueSerializer);
template.setHashValueSerializer(valueSerializer);
template.afterPropertiesSet();
// 开始初始化相关组件
if(!this.isInit) {
this.stringRedisTemplate = stringTemplate;
this.objectRedisTemplate = template;
this.isInit = true;
}
}
/**
* 获取Value如无返空
*/
@Override
public String get(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
/**
* 写入Value并设定存活时间 (单位: )
*/
@Override
public void set(String key, String value, long timeout) {
if(timeout == 0 || timeout <= SaTokenDao.NOT_VALUE_EXPIRE) {
return;
}
// 判断是否为永不过期
if(timeout == SaTokenDao.NEVER_EXPIRE) {
stringRedisTemplate.opsForValue().set(key, value);
} else {
stringRedisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
}
}
/**
* 修改指定key-value键值对 (过期时间不变)
*/
@Override
public void update(String key, String value) {
long expire = getTimeout(key);
// -2 = 无此键
if(expire == SaTokenDao.NOT_VALUE_EXPIRE) {
return;
}
this.set(key, value, expire);
}
/**
* 删除Value
*/
@Override
public void delete(String key) {
stringRedisTemplate.delete(key);
}
/**
* 获取Value的剩余存活时间 (单位: )
*/
@Override
public long getTimeout(String key) {
return stringRedisTemplate.getExpire(key);
}
/**
* 修改Value的剩余存活时间 (单位: )
*/
@Override
public void updateTimeout(String key, long timeout) {
// 判断是否想要设置为永久
if(timeout == SaTokenDao.NEVER_EXPIRE) {
long expire = getTimeout(key);
if(expire == SaTokenDao.NEVER_EXPIRE) {
// 如果其已经被设置为永久则不作任何处理
} else {
// 如果尚未被设置为永久那么再次set一次
this.set(key, this.get(key), timeout);
}
return;
}
stringRedisTemplate.expire(key, timeout, TimeUnit.SECONDS);
}
/**
* 获取Object如无返空
*/
@Override
public Object getObject(String key) {
return objectRedisTemplate.opsForValue().get(key);
}
@Override
public SaSession getSession(String sessionId) {
Object obj = getObject(sessionId);
if (obj == null) {
return null;
}
return JSON.parseObject(obj.toString(), SaSession.class);
}
/**
* 写入Object并设定存活时间 (单位: )
*/
@Override
public void setObject(String key, Object object, long timeout) {
if(timeout == 0 || timeout <= SaTokenDao.NOT_VALUE_EXPIRE) {
return;
}
String toValue = JSON.toJSONString(object);
// 判断是否为永不过期
if(timeout == SaTokenDao.NEVER_EXPIRE) {
objectRedisTemplate.opsForValue().set(key, toValue);
} else {
objectRedisTemplate.opsForValue().set(key, toValue, 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);
}
/**
* 搜索数据
*/
@Override
public List<String> searchData(String prefix, String keyword, int start, int size) {
Set<String> keys = stringRedisTemplate.keys(prefix + "*" + keyword + "*");
List<String> list = new ArrayList<String>(keys);
return SaFoxUtil.searchList(list, start, size);
}
}

View File

@ -0,0 +1 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.dev33.satoken.dao.SaTokenDaoRedisString