From a780fa0ad7e3d2fa2fac6fa02f29390ecf54bf4d Mon Sep 17 00:00:00 2001 From: noear Date: Mon, 3 Mar 2025 20:07:12 +0800 Subject: [PATCH 1/6] =?UTF-8?q?perf:=20noear-redisx=20=E5=8D=87=E4=B8=BA?= =?UTF-8?q?=201.6.9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sa-token-dependencies/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sa-token-dependencies/pom.xml b/sa-token-dependencies/pom.xml index 8e10d7f1..11d06d0d 100644 --- a/sa-token-dependencies/pom.xml +++ b/sa-token-dependencies/pom.xml @@ -25,7 +25,7 @@ 3.0.9.RELEASE 2.3.34 3.0.1 - 1.6.2 + 1.6.9 3.2.88 4.9.17 3.14.4 From fce89c2efa675d950641c56868b1abc2b1bab4fe Mon Sep 17 00:00:00 2001 From: noear Date: Mon, 3 Mar 2025 20:07:42 +0800 Subject: [PATCH 2/6] =?UTF-8?q?perf:=20noear-snack3=20=E5=8D=87=E4=B8=BA?= =?UTF-8?q?=203.2.127?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sa-token-dependencies/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sa-token-dependencies/pom.xml b/sa-token-dependencies/pom.xml index 11d06d0d..728ce526 100644 --- a/sa-token-dependencies/pom.xml +++ b/sa-token-dependencies/pom.xml @@ -26,7 +26,7 @@ 2.3.34 3.0.1 1.6.9 - 3.2.88 + 3.2.127 4.9.17 3.14.4 2.5.0 From caeb4eba1579dafcf765af43be58188fbc3b692e Mon Sep 17 00:00:00 2001 From: noear Date: Mon, 3 Mar 2025 20:23:09 +0800 Subject: [PATCH 3/6] =?UTF-8?q?perf:=20sa-token-solon-plugin=20=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=20dao=20=E4=B8=8B=E7=9A=84=E4=BB=A3=E7=A0=81=EF=BC=88?= =?UTF-8?q?=E7=94=B1=E5=85=B7=E4=BD=93=E6=8F=92=E4=BB=B6=E5=A4=84=E7=90=86?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dao/SaSessionForJacksonCustomized.java | 47 --- .../satoken/solon/dao/SaTokenDaoOfRedis.java | 38 --- .../solon/dao/SaTokenDaoOfRedisBase64.java | 169 ---------- .../solon/dao/SaTokenDaoOfRedisJson.java | 176 ----------- .../dao/SaTokenDaoOfRedissonJackson.java | 298 ------------------ 5 files changed, 728 deletions(-) delete mode 100644 sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/dao/SaSessionForJacksonCustomized.java delete mode 100644 sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/dao/SaTokenDaoOfRedis.java delete mode 100644 sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/dao/SaTokenDaoOfRedisBase64.java delete mode 100644 sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/dao/SaTokenDaoOfRedisJson.java delete mode 100644 sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/dao/SaTokenDaoOfRedissonJackson.java diff --git a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/dao/SaSessionForJacksonCustomized.java b/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/dao/SaSessionForJacksonCustomized.java deleted file mode 100644 index 86a26973..00000000 --- a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/dao/SaSessionForJacksonCustomized.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2020-2099 sa-token.cc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package cn.dev33.satoken.solon.dao; - -import cn.dev33.satoken.session.SaSession; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; - -/** - * Jackson定制版SaSession,忽略 timeout 等属性的序列化 - * - * @author click33 - * @since 1.34.0 - */ -@JsonIgnoreProperties({"timeout"}) -public class SaSessionForJacksonCustomized extends SaSession { - - /** - * - */ - private static final long serialVersionUID = -7600983549653130681L; - - public SaSessionForJacksonCustomized() { - super(); - } - - /** - * 构建一个Session对象 - * @param id Session的id - */ - public SaSessionForJacksonCustomized(String id) { - super(id); - } - -} diff --git a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/dao/SaTokenDaoOfRedis.java b/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/dao/SaTokenDaoOfRedis.java deleted file mode 100644 index 0d39ff60..00000000 --- a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/dao/SaTokenDaoOfRedis.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2020-2099 sa-token.cc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package cn.dev33.satoken.solon.dao; - -import org.noear.redisx.RedisClient; - -import java.util.Properties; - -/** - * SaTokenDao 的 redis 适配 - * - * @author noear - * @since 1.6 - */ -public class SaTokenDaoOfRedis extends SaTokenDaoOfRedisBase64 { - - public SaTokenDaoOfRedis(Properties props) { - super(props); - } - - public SaTokenDaoOfRedis(RedisClient redisClient) { - super(redisClient); - } - -} \ No newline at end of file diff --git a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/dao/SaTokenDaoOfRedisBase64.java b/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/dao/SaTokenDaoOfRedisBase64.java deleted file mode 100644 index c92eb8e4..00000000 --- a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/dao/SaTokenDaoOfRedisBase64.java +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Copyright 2020-2099 sa-token.cc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package cn.dev33.satoken.solon.dao; - -import cn.dev33.satoken.dao.SaTokenDao; -import cn.dev33.satoken.dao.auto.SaTokenDaoBySessionFollowObject; -import cn.dev33.satoken.util.SaFoxUtil; -import org.noear.redisx.RedisClient; -import org.noear.redisx.plus.RedisBucket; - -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; -import java.util.Set; - -/** - * SaTokenDao 的 redis 适配(可以完全精准还原所有序列化类型) - * - * @author noear - * @since 1.6 - */ -public class SaTokenDaoOfRedisBase64 implements SaTokenDaoBySessionFollowObject { - private final RedisBucket redisBucket; - - public SaTokenDaoOfRedisBase64(Properties props) { - this(new RedisClient(props)); - } - - public SaTokenDaoOfRedisBase64(RedisClient redisClient) { - redisBucket = redisClient.getBucket(); - } - - - /** - * 获取Value,如无返空 - */ - @Override - public String get(String key) { - return redisBucket.get(key); - } - - /** - * 写入Value,并设定存活时间 (单位: 秒) - */ - @Override - public void set(String key, String value, long timeout) { - if (timeout > 0 || timeout == SaTokenDao.NEVER_EXPIRE) { - redisBucket.store(key, value, (int) timeout); - } - } - - /** - * 修改指定key-value键值对 (过期时间不变) - */ - @Override - public void update(String key, String value) { - long expire = getTimeout(key); - this.set(key, value, expire); - } - - /** - * 删除Value - */ - @Override - public void delete(String key) { - redisBucket.remove(key); - } - - /** - * 获取Value的剩余存活时间 (单位: 秒) - */ - @Override - public long getTimeout(String key) { - return redisBucket.ttl(key); - } - - /** - * 修改Value的剩余存活时间 (单位: 秒) - */ - @Override - public void updateTimeout(String key, long timeout) { - if (redisBucket.exists(key)) { - redisBucket.delay(key, (int) timeout); - } - } - - - /** - * 获取Object,如无返空 - */ - @Override - public Object getObject(String key) { - return redisBucket.getAndDeserialize(key); - } - - @Override - public T getObject(String key, Class classType) { - return redisBucket.getAndDeserialize(key, classType); - } - - /** - * 写入Object,并设定存活时间 (单位: 秒) - */ - @Override - public void setObject(String key, Object object, long timeout) { - if (timeout > 0 || timeout == SaTokenDao.NEVER_EXPIRE) { - redisBucket.storeAndSerialize(key, object, (int) timeout); - } - } - - /** - * 更新Object (过期时间不变) - */ - @Override - public void updateObject(String key, Object object) { - long expire = getObjectTimeout(key); - this.setObject(key, object, expire); - } - - /** - * 删除Object - */ - @Override - public void deleteObject(String key) { - redisBucket.remove(key); - } - - /** - * 获取Object的剩余存活时间 (单位: 秒) - */ - @Override - public long getObjectTimeout(String key) { - return redisBucket.ttl(key); - } - - /** - * 修改Object的剩余存活时间 (单位: 秒) - */ - @Override - public void updateObjectTimeout(String key, long timeout) { - if (redisBucket.exists(key)) { - redisBucket.delay(key, (int) timeout); - } - } - - - /** - * 搜索数据 - */ - @Override - public List searchData(String prefix, String keyword, int start, int size, boolean sortType) { - Set keys = redisBucket.keys(prefix + "*" + keyword + "*"); - List list = new ArrayList(keys); - return SaFoxUtil.searchList(list, start, size, sortType); - } -} \ No newline at end of file diff --git a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/dao/SaTokenDaoOfRedisJson.java b/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/dao/SaTokenDaoOfRedisJson.java deleted file mode 100644 index 161733d7..00000000 --- a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/dao/SaTokenDaoOfRedisJson.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Copyright 2020-2099 sa-token.cc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package cn.dev33.satoken.solon.dao; - -import cn.dev33.satoken.dao.SaTokenDao; -import cn.dev33.satoken.dao.auto.SaTokenDaoBySessionFollowObject; -import cn.dev33.satoken.util.SaFoxUtil; -import org.noear.redisx.RedisClient; -import org.noear.redisx.plus.RedisBucket; -import org.noear.snack.ONode; - -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; -import java.util.Set; - -/** - * SaTokenDao 的 redis 适配(基于json序列化,不能完全精准还原所有类型) - * - * @author noear - * @since 1.6 - */ -public class SaTokenDaoOfRedisJson implements SaTokenDaoBySessionFollowObject { - private final RedisBucket redisBucket; - - public SaTokenDaoOfRedisJson(Properties props) { - this(new RedisClient(props)); - } - - public SaTokenDaoOfRedisJson(RedisClient redisClient) { - redisBucket = redisClient.getBucket(); - - // 重写 SaSession 生成策略 - //SaStrategy.instance.createSession = (sessionId) -> new SaSessionForJson(sessionId); - } - - - /** - * 获取Value,如无返空 - */ - @Override - public String get(String key) { - return redisBucket.get(key); - } - - /** - * 写入Value,并设定存活时间 (单位: 秒) - */ - @Override - public void set(String key, String value, long timeout) { - if (timeout > 0 || timeout == SaTokenDao.NEVER_EXPIRE) { - redisBucket.store(key, value, (int) timeout); - } - } - - /** - * 修改指定key-value键值对 (过期时间不变) - */ - @Override - public void update(String key, String value) { - long expire = getTimeout(key); - this.set(key, value, expire); - } - - /** - * 删除Value - */ - @Override - public void delete(String key) { - redisBucket.remove(key); - } - - /** - * 获取Value的剩余存活时间 (单位: 秒) - */ - @Override - public long getTimeout(String key) { - return redisBucket.ttl(key); - } - - /** - * 修改Value的剩余存活时间 (单位: 秒) - */ - @Override - public void updateTimeout(String key, long timeout) { - if (redisBucket.exists(key)) { - redisBucket.delay(key, (int) timeout); - } - } - - - /** - * 获取Object,如无返空 - */ - @Override - public Object getObject(String key) { - String value = get(key); - return ONode.deserialize(value); - } - - @Override - public T getObject(String key, Class classType) { - String value = get(key); - return ONode.deserialize(value, classType); - } - - /** - * 写入Object,并设定存活时间 (单位: 秒) - */ - @Override - public void setObject(String key, Object object, long timeout) { - if (timeout > 0 || timeout == SaTokenDao.NEVER_EXPIRE) { - String value = ONode.serialize(object); - set(key, value, timeout); - } - } - - /** - * 更新Object (过期时间不变) - */ - @Override - public void updateObject(String key, Object object) { - long expire = getObjectTimeout(key); - this.setObject(key, object, expire); - } - - /** - * 删除Object - */ - @Override - public void deleteObject(String key) { - redisBucket.remove(key); - } - - /** - * 获取Object的剩余存活时间 (单位: 秒) - */ - @Override - public long getObjectTimeout(String key) { - return redisBucket.ttl(key); - } - - /** - * 修改Object的剩余存活时间 (单位: 秒) - */ - @Override - public void updateObjectTimeout(String key, long timeout) { - if (redisBucket.exists(key)) { - redisBucket.delay(key, (int) timeout); - } - } - - - /** - * 搜索数据 - */ - @Override - public List searchData(String prefix, String keyword, int start, int size, boolean sortType) { - Set keys = redisBucket.keys(prefix + "*" + keyword + "*"); - List list = new ArrayList<>(keys); - return SaFoxUtil.searchList(list, start, size, sortType); - } -} \ No newline at end of file diff --git a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/dao/SaTokenDaoOfRedissonJackson.java b/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/dao/SaTokenDaoOfRedissonJackson.java deleted file mode 100644 index cbecff38..00000000 --- a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/dao/SaTokenDaoOfRedissonJackson.java +++ /dev/null @@ -1,298 +0,0 @@ -/* - * Copyright 2020-2099 sa-token.cc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package cn.dev33.satoken.solon.dao; - -import cn.dev33.satoken.dao.SaTokenDao; -import cn.dev33.satoken.dao.auto.SaTokenDaoBySessionFollowObject; -import cn.dev33.satoken.strategy.SaStrategy; -import cn.dev33.satoken.util.SaFoxUtil; -import com.fasterxml.jackson.annotation.JsonTypeInfo; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; -import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; -import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; -import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer; -import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; -import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; -import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer; -import org.redisson.api.RBatch; -import org.redisson.api.RBucket; -import org.redisson.api.RBucketAsync; -import org.redisson.api.RedissonClient; -import org.redisson.client.codec.Codec; -import org.redisson.codec.JsonJacksonCodec; - -import java.time.Duration; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.format.DateTimeFormatter; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -/** - * Sa-Token 持久层实现 [ Redisson客户端、Redis存储、Jackson序列化 ] - * - * @author 疯狂的狮子Li - * @author noear - * @since 1.34.0 - */ -public class SaTokenDaoOfRedissonJackson implements SaTokenDaoBySessionFollowObject { - - public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; - public static final String DATE_PATTERN = "yyyy-MM-dd"; - public static final String TIME_PATTERN = "HH:mm:ss"; - public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN); - public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(DATE_PATTERN); - public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern(TIME_PATTERN); - - /** - * ObjectMapper 对象 (以 public 作用域暴露出此对象,方便开发者二次更改配置) - * - *

例如: - *

-	 *      SaTokenDaoRedisJackson redisJackson = (SaTokenDaoRedisJackson) SaManager.getSaTokenDao();
-	 *      redisJackson.objectMapper.xxx = xxx;
-	 * 	
- *

- */ - public final ObjectMapper objectMapper; - - /** - * 序列化方式 - */ - public final Codec codec; - - /** - * redisson 客户端 - */ - public final RedissonClient redissonClient; - - public SaTokenDaoOfRedissonJackson(RedissonClient redissonClient) { - this.objectMapper = new ObjectMapper(); - - this.objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); - - // 配置[忽略未知字段] - this.objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - - // 配置[时间类型转换] - JavaTimeModule timeModule = new JavaTimeModule(); - // LocalDateTime序列化与反序列化 - timeModule.addSerializer(new LocalDateTimeSerializer(DATE_TIME_FORMATTER)); - timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DATE_TIME_FORMATTER)); - // LocalDate序列化与反序列化 - timeModule.addSerializer(new LocalDateSerializer(DATE_FORMATTER)); - timeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DATE_FORMATTER)); - // LocalTime序列化与反序列化 - timeModule.addSerializer(new LocalTimeSerializer(TIME_FORMATTER)); - timeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(TIME_FORMATTER)); - this.objectMapper.registerModule(timeModule); - - // 重写 SaSession 生成策略 - SaStrategy.instance.createSession = (sessionId) -> new SaSessionForJacksonCustomized(sessionId); - - - // 开始初始化相关组件 - this.codec = new JsonJacksonCodec(objectMapper); - this.redissonClient = redissonClient; - } - - - /** - * 获取Value,如无返空 - */ - @Override - public String get(String key) { - RBucket rBucket = redissonClient.getBucket(key, codec); - return rBucket.get(); - } - - /** - * 写入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) { - RBucket bucket = redissonClient.getBucket(key, codec); - bucket.set(value); - } else { - RBatch batch = redissonClient.createBatch(); - RBucketAsync bucket = batch.getBucket(key, codec); - bucket.setAsync(value); - bucket.expireAsync(Duration.ofSeconds(timeout)); - batch.execute(); - } - } - - /** - * 修修改指定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) { - redissonClient.getBucket(key, codec).delete(); - } - - /** - * 获取Value的剩余存活时间 (单位: 秒) - */ - @Override - public long getTimeout(String key) { - RBucket rBucket = redissonClient.getBucket(key, codec); - long timeout = rBucket.remainTimeToLive(); - return timeout < 0 ? timeout : timeout / 1000; - } - - /** - * 修改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; - } - RBucket rBucket = redissonClient.getBucket(key, codec); - rBucket.expire(Duration.ofSeconds(timeout)); - } - - - - /** - * 获取Object,如无返空 - */ - @Override - public Object getObject(String key) { - RBucket rBucket = redissonClient.getBucket(key, codec); - return rBucket.get(); - } - - @Override - public T getObject(String key, Class classType) { - // TODO 待实现 - return null; - } - - /** - * 写入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) { - RBucket bucket = redissonClient.getBucket(key, codec); - bucket.set(object); - } else { - RBatch batch = redissonClient.createBatch(); - RBucketAsync bucket = batch.getBucket(key, codec); - bucket.setAsync(object); - bucket.expireAsync(Duration.ofSeconds(timeout)); - batch.execute(); - } - - } - - /** - * 更新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) { - redissonClient.getBucket(key, codec).delete(); - } - - /** - * 获取Object的剩余存活时间 (单位: 秒) - */ - @Override - public long getObjectTimeout(String key) { - RBucket rBucket = redissonClient.getBucket(key, codec); - long timeout = rBucket.remainTimeToLive(); - return timeout < 0 ? timeout : timeout / 1000; - } - - /** - * 修改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; - } - RBucket rBucket = redissonClient.getBucket(key, codec); - rBucket.expire(Duration.ofSeconds(timeout)); - } - - - /** - * 搜索数据 - */ - @Override - public List searchData(String prefix, String keyword, int start, int size, boolean sortType) { - Stream stream = redissonClient.getKeys().getKeysStreamByPattern(prefix + "*" + keyword + "*"); - List list = stream.collect(Collectors.toList()); - return SaFoxUtil.searchList(list, start, size, sortType); - } -} From 905f6714e2439e13d7cdd63b1ee28d3c43922766 Mon Sep 17 00:00:00 2001 From: noear Date: Mon, 3 Mar 2025 20:32:23 +0800 Subject: [PATCH 4/6] =?UTF-8?q?perf:=20sa-token-redisx=20=E8=B0=83?= =?UTF-8?q?=E6=95=B4=20SaTokenDaoOfRedisJson=20=E7=B1=BB=E4=BF=9D=E6=8C=81?= =?UTF-8?q?=E4=B8=8E=20SaTokenDaoForRedisTemplate=20=E7=9B=B8=E4=BC=BC?= =?UTF-8?q?=E7=9A=84=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sa-token-plugin/sa-token-redisx/pom.xml | 6 - .../dev33/satoken/dao/SaSessionForJson.java | 89 ---------- ...edisJson.java => SaTokenDaoForRedisx.java} | 106 ++++------- .../dev33/satoken/dao/SaTokenDaoOfRedis.java | 37 ---- .../satoken/dao/SaTokenDaoOfRedisBase64.java | 168 ------------------ 5 files changed, 31 insertions(+), 375 deletions(-) delete mode 100644 sa-token-plugin/sa-token-redisx/src/main/java/cn/dev33/satoken/dao/SaSessionForJson.java rename sa-token-plugin/sa-token-redisx/src/main/java/cn/dev33/satoken/dao/{SaTokenDaoOfRedisJson.java => SaTokenDaoForRedisx.java} (55%) delete mode 100644 sa-token-plugin/sa-token-redisx/src/main/java/cn/dev33/satoken/dao/SaTokenDaoOfRedis.java delete mode 100644 sa-token-plugin/sa-token-redisx/src/main/java/cn/dev33/satoken/dao/SaTokenDaoOfRedisBase64.java diff --git a/sa-token-plugin/sa-token-redisx/pom.xml b/sa-token-plugin/sa-token-redisx/pom.xml index d9e1c784..88f5896e 100644 --- a/sa-token-plugin/sa-token-redisx/pom.xml +++ b/sa-token-plugin/sa-token-redisx/pom.xml @@ -28,12 +28,6 @@ redisx - - org.noear - snack3 - provided - - org.noear solon-test diff --git a/sa-token-plugin/sa-token-redisx/src/main/java/cn/dev33/satoken/dao/SaSessionForJson.java b/sa-token-plugin/sa-token-redisx/src/main/java/cn/dev33/satoken/dao/SaSessionForJson.java deleted file mode 100644 index 19b3205e..00000000 --- a/sa-token-plugin/sa-token-redisx/src/main/java/cn/dev33/satoken/dao/SaSessionForJson.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2020-2099 sa-token.cc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package cn.dev33.satoken.dao; - -import cn.dev33.satoken.session.SaSession; -import cn.dev33.satoken.util.SaFoxUtil; -import org.noear.snack.ONode; - -/** - * Snack3 定制版 SaSession,重写类型转换API - * - * @author noear - * @since 1.34.0 - */ -@Deprecated -public class SaSessionForJson extends SaSession { - - private static final long serialVersionUID = -7600983549653130681L; - - public SaSessionForJson() { - super(); - } - - /** - * 构建一个 SaSession 对象 - * @param id Session 的 id - */ - public SaSessionForJson(String id) { - super(id); - } - - /** - * 取值 (指定转换类型) - * @param 泛型 - * @param key key - * @param cs 指定转换类型 - * @return 值 - */ - @Override - public T getModel(String key, Class cs) { - if(SaFoxUtil.isBasicType(cs)) { - return SaFoxUtil.getValueByType(get(key), cs); - } - return ONode.deserialize(getString(key), cs); - } - - /** - * 取值 (指定转换类型, 并指定值为Null时返回的默认值) - * @param 泛型 - * @param key key - * @param cs 指定转换类型 - * @param defaultValue 值为Null时返回的默认值 - * @return 值 - */ - @Override - @SuppressWarnings("unchecked") - public T getModel(String key, Class cs, Object defaultValue) { - Object value = get(key); - if(valueIsNull(value)) { - return (T)defaultValue; - } - if(SaFoxUtil.isBasicType(cs)) { - return SaFoxUtil.getValueByType(get(key), cs); - } - return ONode.deserialize(getString(key), cs); - } - - /** - * 忽略 timeout 字段的序列化 - */ - @Override - public long timeout() { - return super.timeout(); - } - -} diff --git a/sa-token-plugin/sa-token-redisx/src/main/java/cn/dev33/satoken/dao/SaTokenDaoOfRedisJson.java b/sa-token-plugin/sa-token-redisx/src/main/java/cn/dev33/satoken/dao/SaTokenDaoForRedisx.java similarity index 55% rename from sa-token-plugin/sa-token-redisx/src/main/java/cn/dev33/satoken/dao/SaTokenDaoOfRedisJson.java rename to sa-token-plugin/sa-token-redisx/src/main/java/cn/dev33/satoken/dao/SaTokenDaoForRedisx.java index 33fdc1a4..dccfb338 100644 --- a/sa-token-plugin/sa-token-redisx/src/main/java/cn/dev33/satoken/dao/SaTokenDaoOfRedisJson.java +++ b/sa-token-plugin/sa-token-redisx/src/main/java/cn/dev33/satoken/dao/SaTokenDaoForRedisx.java @@ -15,11 +15,10 @@ */ package cn.dev33.satoken.dao; -import cn.dev33.satoken.dao.auto.SaTokenDaoBySessionFollowObject; +import cn.dev33.satoken.dao.auto.SaTokenDaoByObjectFollowString; import cn.dev33.satoken.util.SaFoxUtil; import org.noear.redisx.RedisClient; import org.noear.redisx.plus.RedisBucket; -import org.noear.snack.ONode; import java.util.ArrayList; import java.util.List; @@ -31,20 +30,17 @@ import java.util.Set; * * @author noear * @since 1.34.0 + * @since 1.41.0 */ -public class SaTokenDaoOfRedisJson implements SaTokenDaoBySessionFollowObject { +public class SaTokenDaoForRedisx implements SaTokenDaoByObjectFollowString { private final RedisBucket redisBucket; - public SaTokenDaoOfRedisJson(Properties props) { + public SaTokenDaoForRedisx(Properties props) { this(new RedisClient(props)); } - public SaTokenDaoOfRedisJson(RedisClient redisClient) { + public SaTokenDaoForRedisx(RedisClient redisClient) { redisBucket = redisClient.getBucket(); - - // 重写 SaSession 生成策略 - //SaStrategy.instance.createSession = (sessionId) -> new SaSessionForJson(sessionId); - } /** @@ -60,7 +56,14 @@ public class SaTokenDaoOfRedisJson implements SaTokenDaoBySessionFollowObject { */ @Override public void set(String key, String value, long timeout) { - if (timeout > 0 || timeout == SaTokenDao.NEVER_EXPIRE) { + if (timeout == 0 || timeout <= SaTokenDao.NOT_VALUE_EXPIRE) { + return; + } + + // 判断是否为永不过期 + if (timeout == SaTokenDao.NEVER_EXPIRE) { + redisBucket.store(key, value); + } else { redisBucket.store(key, value, (int) timeout); } } @@ -71,6 +74,11 @@ public class SaTokenDaoOfRedisJson implements SaTokenDaoBySessionFollowObject { @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); } @@ -95,74 +103,22 @@ public class SaTokenDaoOfRedisJson implements SaTokenDaoBySessionFollowObject { */ @Override public void updateTimeout(String key, long timeout) { - if (redisBucket.exists(key)) { - redisBucket.delay(key, (int) 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; } + + + redisBucket.delay(key, (int) timeout); } - - /** - * 获取Object,如无返空 - */ - @Override - public Object getObject(String key) { - String value = get(key); - return ONode.deserialize(value); - } - - @Override - public T getObject(String key, Class classType) { - String value = get(key); - return ONode.deserialize(value, classType); - } - - /** - * 写入Object,并设定存活时间 (单位: 秒) - */ - @Override - public void setObject(String key, Object object, long timeout) { - if (timeout > 0 || timeout == SaTokenDao.NEVER_EXPIRE) { - String value = ONode.serialize(object); - set(key, value, timeout); - } - } - - /** - * 更新Object (过期时间不变) - */ - @Override - public void updateObject(String key, Object object) { - long expire = getObjectTimeout(key); - this.setObject(key, object, expire); - } - - /** - * 删除Object - */ - @Override - public void deleteObject(String key) { - redisBucket.remove(key); - } - - /** - * 获取Object的剩余存活时间 (单位: 秒) - */ - @Override - public long getObjectTimeout(String key) { - return redisBucket.ttl(key); - } - - /** - * 修改Object的剩余存活时间 (单位: 秒) - */ - @Override - public void updateObjectTimeout(String key, long timeout) { - if (redisBucket.exists(key)) { - redisBucket.delay(key, (int) timeout); - } - } - - /** * 搜索数据 */ diff --git a/sa-token-plugin/sa-token-redisx/src/main/java/cn/dev33/satoken/dao/SaTokenDaoOfRedis.java b/sa-token-plugin/sa-token-redisx/src/main/java/cn/dev33/satoken/dao/SaTokenDaoOfRedis.java deleted file mode 100644 index 430ba2f7..00000000 --- a/sa-token-plugin/sa-token-redisx/src/main/java/cn/dev33/satoken/dao/SaTokenDaoOfRedis.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2020-2099 sa-token.cc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package cn.dev33.satoken.dao; - -import org.noear.redisx.RedisClient; - -import java.util.Properties; - -/** - * SaTokenDao 的 redis 适配 - * - * @author noear - * @since 1.34.0 - */ -public class SaTokenDaoOfRedis extends SaTokenDaoOfRedisBase64 { - - public SaTokenDaoOfRedis(Properties props) { - super(props); - } - - public SaTokenDaoOfRedis(RedisClient redisClient) { - super(redisClient); - } -} \ No newline at end of file diff --git a/sa-token-plugin/sa-token-redisx/src/main/java/cn/dev33/satoken/dao/SaTokenDaoOfRedisBase64.java b/sa-token-plugin/sa-token-redisx/src/main/java/cn/dev33/satoken/dao/SaTokenDaoOfRedisBase64.java deleted file mode 100644 index 96eb5ca2..00000000 --- a/sa-token-plugin/sa-token-redisx/src/main/java/cn/dev33/satoken/dao/SaTokenDaoOfRedisBase64.java +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright 2020-2099 sa-token.cc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package cn.dev33.satoken.dao; - -import cn.dev33.satoken.dao.auto.SaTokenDaoBySessionFollowObject; -import cn.dev33.satoken.util.SaFoxUtil; -import org.noear.redisx.RedisClient; -import org.noear.redisx.plus.RedisBucket; - -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; -import java.util.Set; - -/** - * SaTokenDao 的 redis 适配(可以完全精准还原所有序列化类型) - * - * @author noear - * @since 1.34.0 - */ -public class SaTokenDaoOfRedisBase64 implements SaTokenDaoBySessionFollowObject { - private final RedisBucket redisBucket; - - public SaTokenDaoOfRedisBase64(Properties props) { - this(new RedisClient(props)); - } - - public SaTokenDaoOfRedisBase64(RedisClient redisClient) { - redisBucket = redisClient.getBucket(); - } - - - /** - * 获取Value,如无返空 - */ - @Override - public String get(String key) { - return redisBucket.get(key); - } - - /** - * 写入Value,并设定存活时间 (单位: 秒) - */ - @Override - public void set(String key, String value, long timeout) { - if (timeout > 0 || timeout == SaTokenDao.NEVER_EXPIRE) { - redisBucket.store(key, value, (int) timeout); - } - } - - /** - * 修改指定key-value键值对 (过期时间不变) - */ - @Override - public void update(String key, String value) { - long expire = getTimeout(key); - this.set(key, value, expire); - } - - /** - * 删除Value - */ - @Override - public void delete(String key) { - redisBucket.remove(key); - } - - /** - * 获取Value的剩余存活时间 (单位: 秒) - */ - @Override - public long getTimeout(String key) { - return redisBucket.ttl(key); - } - - /** - * 修改Value的剩余存活时间 (单位: 秒) - */ - @Override - public void updateTimeout(String key, long timeout) { - if (redisBucket.exists(key)) { - redisBucket.delay(key, (int) timeout); - } - } - - - /** - * 获取Object,如无返空 - */ - @Override - public Object getObject(String key) { - return redisBucket.getAndDeserialize(key); - } - - @Override - public T getObject(String key, Class classType) { - return redisBucket.getAndDeserialize(key, classType); - } - - /** - * 写入Object,并设定存活时间 (单位: 秒) - */ - @Override - public void setObject(String key, Object object, long timeout) { - if (timeout > 0 || timeout == SaTokenDao.NEVER_EXPIRE) { - redisBucket.storeAndSerialize(key, object, (int) timeout); - } - } - - /** - * 更新Object (过期时间不变) - */ - @Override - public void updateObject(String key, Object object) { - long expire = getObjectTimeout(key); - this.setObject(key, object, expire); - } - - /** - * 删除Object - */ - @Override - public void deleteObject(String key) { - redisBucket.remove(key); - } - - /** - * 获取Object的剩余存活时间 (单位: 秒) - */ - @Override - public long getObjectTimeout(String key) { - return redisBucket.ttl(key); - } - - /** - * 修改Object的剩余存活时间 (单位: 秒) - */ - @Override - public void updateObjectTimeout(String key, long timeout) { - if (redisBucket.exists(key)) { - redisBucket.delay(key, (int) timeout); - } - } - - - /** - * 搜索数据 - */ - @Override - public List searchData(String prefix, String keyword, int start, int size, boolean sortType) { - Set keys = redisBucket.keys(prefix + "*" + keyword + "*"); - List list = new ArrayList(keys); - return SaFoxUtil.searchList(list, start, size, sortType); - } -} \ No newline at end of file From 8c256d893ba65b353147ed9b10a74881d0858d33 Mon Sep 17 00:00:00 2001 From: noear Date: Mon, 3 Mar 2025 20:45:42 +0800 Subject: [PATCH 5/6] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=96=B0?= =?UTF-8?q?=E6=89=80=E8=8F=9C=E6=8F=92=E4=BB=B6=20sa-token-snack3=20?= =?UTF-8?q?=EF=BC=88=E9=80=9A=E7=94=A8=20json=20=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sa-token-bom/pom.xml | 5 ++ sa-token-plugin/pom.xml | 1 + sa-token-plugin/sa-token-snack3/pom.xml | 25 +++++++ .../satoken/json/SaJsonTemplateForSnack3.java | 44 +++++++++++ .../plugin/SaTokenPluginForSnack3.java | 46 ++++++++++++ .../session/SaSessionForSnack3Customized.java | 75 +++++++++++++++++++ .../cn.dev33.satoken.plugin.SaTokenPlugin | 1 + 7 files changed, 197 insertions(+) create mode 100644 sa-token-plugin/sa-token-snack3/pom.xml create mode 100644 sa-token-plugin/sa-token-snack3/src/main/java/cn/dev33/satoken/json/SaJsonTemplateForSnack3.java create mode 100644 sa-token-plugin/sa-token-snack3/src/main/java/cn/dev33/satoken/plugin/SaTokenPluginForSnack3.java create mode 100644 sa-token-plugin/sa-token-snack3/src/main/java/cn/dev33/satoken/session/SaSessionForSnack3Customized.java create mode 100644 sa-token-plugin/sa-token-snack3/src/main/resources/META-INF/satoken/cn.dev33.satoken.plugin.SaTokenPlugin diff --git a/sa-token-bom/pom.xml b/sa-token-bom/pom.xml index 6eb680d5..4c69c691 100644 --- a/sa-token-bom/pom.xml +++ b/sa-token-bom/pom.xml @@ -129,6 +129,11 @@ sa-token-fastjson2 ${revision} + + cn.dev33 + sa-token-snack3 + ${revision} + cn.dev33 sa-token-redis-jackson diff --git a/sa-token-plugin/pom.xml b/sa-token-plugin/pom.xml index c50722ed..339d3e85 100644 --- a/sa-token-plugin/pom.xml +++ b/sa-token-plugin/pom.xml @@ -22,6 +22,7 @@ sa-token-jackson sa-token-fastjson sa-token-fastjson2 + sa-token-snack3 sa-token-hutool-timed-cache sa-token-thymeleaf sa-token-freemarker diff --git a/sa-token-plugin/sa-token-snack3/pom.xml b/sa-token-plugin/sa-token-snack3/pom.xml new file mode 100644 index 00000000..a7db5709 --- /dev/null +++ b/sa-token-plugin/sa-token-snack3/pom.xml @@ -0,0 +1,25 @@ + + + + sa-token-plugin + cn.dev33 + ${revision} + ../pom.xml + + 4.0.0 + + sa-token-snack3 + + + + cn.dev33 + sa-token-core + + + org.noear + snack3 + + + \ No newline at end of file diff --git a/sa-token-plugin/sa-token-snack3/src/main/java/cn/dev33/satoken/json/SaJsonTemplateForSnack3.java b/sa-token-plugin/sa-token-snack3/src/main/java/cn/dev33/satoken/json/SaJsonTemplateForSnack3.java new file mode 100644 index 00000000..01457c1d --- /dev/null +++ b/sa-token-plugin/sa-token-snack3/src/main/java/cn/dev33/satoken/json/SaJsonTemplateForSnack3.java @@ -0,0 +1,44 @@ +/* + * Copyright 2020-2099 sa-token.cc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package cn.dev33.satoken.json; + +import org.noear.snack.ONode; + +/** + * JSON 转换器, Snack3 版实现 + * + * @author click33 + * @author noear + * @since 1.41.0 + */ +public class SaJsonTemplateForSnack3 implements SaJsonTemplate { + + /** + * 序列化:对象 -> json 字符串 + */ + @Override + public String objectToJson(Object obj) { + return ONode.stringify(obj); + } + + /** + * 反序列化:json 字符串 → 对象 + */ + @Override + public T jsonToObject(String jsonStr, Class type) { + return ONode.deserialize(jsonStr, type); + } +} diff --git a/sa-token-plugin/sa-token-snack3/src/main/java/cn/dev33/satoken/plugin/SaTokenPluginForSnack3.java b/sa-token-plugin/sa-token-snack3/src/main/java/cn/dev33/satoken/plugin/SaTokenPluginForSnack3.java new file mode 100644 index 00000000..d3c279cc --- /dev/null +++ b/sa-token-plugin/sa-token-snack3/src/main/java/cn/dev33/satoken/plugin/SaTokenPluginForSnack3.java @@ -0,0 +1,46 @@ +/* + * Copyright 2020-2099 sa-token.cc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package cn.dev33.satoken.plugin; + +import cn.dev33.satoken.SaManager; +import cn.dev33.satoken.json.SaJsonTemplateForSnack3; +import cn.dev33.satoken.session.SaSessionForSnack3Customized; +import cn.dev33.satoken.strategy.SaStrategy; + +/** + * SaToken 插件安装:JSON 转换器 - Snack3 版 + * + * @author click33 + * @author noear + * @since 1.41.0 + */ +public class SaTokenPluginForSnack3 implements SaTokenPlugin { + + @Override + public void install() { + + // 设置JSON转换器:Fastjson 版 + SaManager.setSaJsonTemplate(new SaJsonTemplateForSnack3()); + + // 重写 SaSession 生成策略 + SaStrategy.instance.createSession = SaSessionForSnack3Customized::new; + + // 指定 SaSession 类型 + SaStrategy.instance.sessionClassType = SaSessionForSnack3Customized.class; + + } + +} \ No newline at end of file diff --git a/sa-token-plugin/sa-token-snack3/src/main/java/cn/dev33/satoken/session/SaSessionForSnack3Customized.java b/sa-token-plugin/sa-token-snack3/src/main/java/cn/dev33/satoken/session/SaSessionForSnack3Customized.java new file mode 100644 index 00000000..f7c4c368 --- /dev/null +++ b/sa-token-plugin/sa-token-snack3/src/main/java/cn/dev33/satoken/session/SaSessionForSnack3Customized.java @@ -0,0 +1,75 @@ +/* + * Copyright 2020-2099 sa-token.cc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package cn.dev33.satoken.session; + +import cn.dev33.satoken.util.SaFoxUtil; +import org.noear.snack.ONode; + +/** + * Fastjson 定制版 SaSession,重写类型转换API + * + * @author click33 + * @author noear + * @since 1.34.0 + */ +public class SaSessionForSnack3Customized extends SaSession { + + private static final long serialVersionUID = -7600983549653130681L; + + /** + * 构建一个 SaSession 对象 + */ + public SaSessionForSnack3Customized() { + super(); + } + + /** + * 构建一个 SaSession 对象 + * + * @param id Session 的 id + */ + public SaSessionForSnack3Customized(String id) { + super(id); + } + + /** + * 取值 (指定转换类型) + * + * @param 泛型 + * @param key key + * @param cs 指定转换类型 + * @return 值 + */ + @Override + public T getModel(String key, Class cs) { + // 如果是想取出为基础类型 + Object value = get(key); + if (SaFoxUtil.isBasicType(cs)) { + return SaFoxUtil.getValueByType(value, cs); + } + // 为空提前返回 + if (valueIsNull(value)) { + return null; + } + // 如果是 JSONObject 类型直接转,否则先转为 String 再转 + if (value instanceof ONode) { + ONode jo = (ONode) value; + return jo.toObject(cs); + } else { + return ONode.deserialize(value.toString(), cs); + } + } +} diff --git a/sa-token-plugin/sa-token-snack3/src/main/resources/META-INF/satoken/cn.dev33.satoken.plugin.SaTokenPlugin b/sa-token-plugin/sa-token-snack3/src/main/resources/META-INF/satoken/cn.dev33.satoken.plugin.SaTokenPlugin new file mode 100644 index 00000000..d69d4491 --- /dev/null +++ b/sa-token-plugin/sa-token-snack3/src/main/resources/META-INF/satoken/cn.dev33.satoken.plugin.SaTokenPlugin @@ -0,0 +1 @@ +cn.dev33.satoken.plugin.SaTokenPluginForSnack3 \ No newline at end of file From 2be9b0f173a9a1bb9150fd7cf34495fd2a58e8c1 Mon Sep 17 00:00:00 2001 From: noear Date: Mon, 3 Mar 2025 20:47:11 +0800 Subject: [PATCH 6/6] =?UTF-8?q?docs:=20sa-token-plugin=20=E7=9A=84=20pom.x?= =?UTF-8?q?ml=EF=BC=8Csa-token-redisx=20=E8=B0=83=E6=95=B4=E5=88=B0?= =?UTF-8?q?=E2=80=9C=E9=80=9A=E7=94=A8=E6=8F=92=E4=BB=B6=E2=80=9D=EF=BC=88?= =?UTF-8?q?=E5=AE=83=E6=98=AF=E9=80=9A=E7=94=A8=E7=9A=84=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sa-token-plugin/pom.xml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/sa-token-plugin/pom.xml b/sa-token-plugin/pom.xml index 339d3e85..d3e436bd 100644 --- a/sa-token-plugin/pom.xml +++ b/sa-token-plugin/pom.xml @@ -33,6 +33,7 @@ sa-token-sso sa-token-oauth2 sa-token-redisson + sa-token-redisx sa-token-serializer-features @@ -45,10 +46,6 @@ sa-token-grpc sa-token-quick-login sa-token-redisson-spring-boot-starter - - - sa-token-redisx -