From 54d435b457303f9d41d9eef9f7a67a31658e46d2 Mon Sep 17 00:00:00 2001 From: sikadai Date: Mon, 18 Jul 2022 18:39:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=94=AF=E6=8C=81redis?= =?UTF-8?q?=E7=9A=84String=E4=BD=9C=E4=B8=BAsession=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96=E7=9A=84=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/dev33/satoken/session/SaSession.java | 11 +- .../cn/dev33/satoken/session/TokenSign.java | 105 ++++---- sa-token-plugin/pom.xml | 1 + .../sa-token-dao-redis-string/.gitignore | 13 + .../sa-token-dao-redis-string/pom.xml | 34 +++ .../satoken/dao/SaTokenDaoRedisString.java | 236 ++++++++++++++++++ .../main/resources/META-INF/spring.factories | 1 + 7 files changed, 353 insertions(+), 48 deletions(-) create mode 100644 sa-token-plugin/sa-token-dao-redis-string/.gitignore create mode 100644 sa-token-plugin/sa-token-dao-redis-string/pom.xml create mode 100644 sa-token-plugin/sa-token-dao-redis-string/src/main/java/cn/dev33/satoken/dao/SaTokenDaoRedisString.java create mode 100644 sa-token-plugin/sa-token-dao-redis-string/src/main/resources/META-INF/spring.factories diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/session/SaSession.java b/sa-token-core/src/main/java/cn/dev33/satoken/session/SaSession.java index 0812ec49..865482e1 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/session/SaSession.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/session/SaSession.java @@ -108,7 +108,14 @@ public class SaSession implements Serializable { /** * 此Session绑定的token签名列表 */ - private final List tokenSignList = new Vector<>(); + private Vector tokenSignList = new Vector<>(); + + /** + * 设置此Session绑定的token签名列表-反序列化需要 + */ + public void setTokenSignList(Vector tokenSignList) { + this.tokenSignList = tokenSignList; + } /** * 此Session绑定的token签名列表 @@ -597,6 +604,4 @@ public class SaSession implements Serializable { return dataMap.keySet(); } - - } diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/session/TokenSign.java b/sa-token-core/src/main/java/cn/dev33/satoken/session/TokenSign.java index 4af451a3..4d65a980 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/session/TokenSign.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/session/TokenSign.java @@ -3,62 +3,77 @@ package cn.dev33.satoken.session; import java.io.Serializable; /** - * Token 签名 Model - * + * Token 签名 Model + *

* 挂在到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 + "]"; + } } diff --git a/sa-token-plugin/pom.xml b/sa-token-plugin/pom.xml index 7b6d8dbf..61ad0051 100644 --- a/sa-token-plugin/pom.xml +++ b/sa-token-plugin/pom.xml @@ -22,6 +22,7 @@ sa-token-dao-redis sa-token-dao-redis-jackson sa-token-dao-redisx + sa-token-dao-redis-string sa-token-dialect-thymeleaf sa-token-sso sa-token-oauth2 diff --git a/sa-token-plugin/sa-token-dao-redis-string/.gitignore b/sa-token-plugin/sa-token-dao-redis-string/.gitignore new file mode 100644 index 00000000..8122f47c --- /dev/null +++ b/sa-token-plugin/sa-token-dao-redis-string/.gitignore @@ -0,0 +1,13 @@ +target/ + +node_modules/ +bin/ +.settings/ +unpackage/ +.classpath +.project + +.factorypath + +.idea/ +.iml \ No newline at end of file diff --git a/sa-token-plugin/sa-token-dao-redis-string/pom.xml b/sa-token-plugin/sa-token-dao-redis-string/pom.xml new file mode 100644 index 00000000..f65399a3 --- /dev/null +++ b/sa-token-plugin/sa-token-dao-redis-string/pom.xml @@ -0,0 +1,34 @@ + + + + sa-token-plugin + cn.dev33 + ${revision} + ../pom.xml + + 4.0.0 + + sa-token-dao-redix-string + + + + + cn.dev33 + sa-token-core + ${revision} + + + + org.springframework.boot + spring-boot-starter-data-redis + 2.3.3.RELEASE + + + com.alibaba + fastjson + 1.2.78 + + + \ No newline at end of file diff --git a/sa-token-plugin/sa-token-dao-redis-string/src/main/java/cn/dev33/satoken/dao/SaTokenDaoRedisString.java b/sa-token-plugin/sa-token-dao-redis-string/src/main/java/cn/dev33/satoken/dao/SaTokenDaoRedisString.java new file mode 100644 index 00000000..3b5f3717 --- /dev/null +++ b/sa-token-plugin/sa-token-dao-redis-string/src/main/java/cn/dev33/satoken/dao/SaTokenDaoRedisString.java @@ -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 searchData(String prefix, String keyword, int start, int size) { + Set keys = stringRedisTemplate.keys(prefix + "*" + keyword + "*"); + List list = new ArrayList(keys); + return SaFoxUtil.searchList(list, start, size); + } + + +} diff --git a/sa-token-plugin/sa-token-dao-redis-string/src/main/resources/META-INF/spring.factories b/sa-token-plugin/sa-token-dao-redis-string/src/main/resources/META-INF/spring.factories new file mode 100644 index 00000000..09ff20cb --- /dev/null +++ b/sa-token-plugin/sa-token-dao-redis-string/src/main/resources/META-INF/spring.factories @@ -0,0 +1 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.dev33.satoken.dao.SaTokenDaoRedisString \ No newline at end of file