!356 添加 sa-token-snack4

Merge pull request !356 from 西东/dev
This commit is contained in:
刘潇
2026-03-01 21:08:05 +00:00
committed by Gitee
7 changed files with 214 additions and 2 deletions

View File

@@ -27,8 +27,9 @@
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version> <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<freemarker.version>2.3.34</freemarker.version> <freemarker.version>2.3.34</freemarker.version>
<solon.version>3.2.1</solon.version> <solon.version>3.2.1</solon.version>
<noear-redisx.version>1.6.9</noear-redisx.version> <noear-redisx.version>1.8.2</noear-redisx.version>
<noear-snack3.version>3.2.133</noear-snack3.version> <noear-snack3.version>3.2.139</noear-snack3.version>
<noear-snack4.version>4.0.20</noear-snack4.version>
<jfinal.version>4.9.17</jfinal.version> <jfinal.version>4.9.17</jfinal.version>
<jboot.version>3.14.4</jboot.version> <jboot.version>3.14.4</jboot.version>
<loveqq.version>1.1.5-java8</loveqq.version> <loveqq.version>1.1.5-java8</loveqq.version>
@@ -91,6 +92,13 @@
<artifactId>snack3</artifactId> <artifactId>snack3</artifactId>
<version>${noear-snack3.version}</version> <version>${noear-snack3.version}</version>
</dependency> </dependency>
<!-- snack4 -->
<dependency>
<groupId>org.noear</groupId>
<artifactId>snack4</artifactId>
<version>${noear-snack4.version}</version>
</dependency>
<!-- jboot --> <!-- jboot -->
<dependency> <dependency>

View File

@@ -24,6 +24,7 @@
<module>sa-token-fastjson</module> <module>sa-token-fastjson</module>
<module>sa-token-fastjson2</module> <module>sa-token-fastjson2</module>
<module>sa-token-snack3</module> <module>sa-token-snack3</module>
<module>sa-token-snack4</module>
<module>sa-token-hutool-timed-cache</module> <module>sa-token-hutool-timed-cache</module>
<module>sa-token-caffeine</module> <module>sa-token-caffeine</module>
<module>sa-token-thymeleaf</module> <module>sa-token-thymeleaf</module>

View File

@@ -0,0 +1,25 @@
<?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-snack4</artifactId>
<dependencies>
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-core</artifactId>
</dependency>
<dependency>
<groupId>org.noear</groupId>
<artifactId>snack4</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,54 @@
/*
* 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 cn.dev33.satoken.util.SaFoxUtil;
import org.noear.snack4.ONode;
import org.noear.snack4.Feature;
import org.noear.snack4.Options;
/**
* JSON 转换器, Snack3 版实现
*
* @author click33
* @author noear
* @since 1.41.0
*/
public class SaJsonTemplateForSnack4 implements SaJsonTemplate {
private final Options options = Options.of(Feature.Write_ClassName, Feature.Write_NotRootClassName, Feature.Read_AutoType);
/**
* 序列化:对象 -> json 字符串
*/
@Override
public String objectToJson(Object obj) {
if (SaFoxUtil.isEmpty(obj)) {
return null;
}
return ONode.ofBean(obj, options).toJson();
}
/**
* 反序列化json 字符串 → 对象
*/
@Override
public <T> T jsonToObject(String jsonStr, Class<T> type) {
if (SaFoxUtil.isEmpty(jsonStr)) {
return null;
}
return ONode.deserialize(jsonStr, type, options);
}
}

View File

@@ -0,0 +1,45 @@
/*
* 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.SaJsonTemplateForSnack4;
import cn.dev33.satoken.session.SaSessionForSnack4Customized;
import cn.dev33.satoken.strategy.SaStrategy;
/**
* SaToken 插件安装JSON 转换器 - Snack3 版
*
* @author click33
* @author noear
* @since 1.41.0
*/
public class SaTokenPluginForSnack4 implements SaTokenPlugin {
@Override
public void install() {
// 设置JSON转换器Snack3 版
SaManager.setSaJsonTemplate(new SaJsonTemplateForSnack4());
// 重写 SaSession 生成策略
SaStrategy.instance.createSession = SaSessionForSnack4Customized::new;
// 指定 SaSession 类型
SaStrategy.instance.sessionClassType = SaSessionForSnack4Customized.class;
}
}

View File

@@ -0,0 +1,78 @@
/*
* 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.snack4.ONode;
/**
* Fastjson 定制版 SaSession重写类型转换API
*
* @author click33
* @author noear
* @since 1.34.0
*/
public class SaSessionForSnack4Customized extends SaSession {
private static final long serialVersionUID = -7600983549653130681L;
/**
* 构建一个 SaSession 对象
*/
public SaSessionForSnack4Customized() {
super();
}
/**
* 构建一个 SaSession 对象
*
* @param id Session 的 id
*/
public SaSessionForSnack4Customized(String id) {
super(id);
}
/**
* 取值 (指定转换类型)
*
* @param <T> 泛型
* @param key key
* @param cs 指定转换类型
* @return 值
*/
@Override
public <T> T getModel(String key, Class<T> 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.toBean(cs);
} else if (value instanceof String) {
return ONode.deserialize((String) value, cs);
} else {
//有可能是 Map
return ONode.ofBean(value).toBean(cs);
}
}
}

View File

@@ -0,0 +1 @@
cn.dev33.satoken.plugin.SaTokenPluginForSnack4