feat: 添加 sa-token-snack4
This commit is contained in:
@@ -23,6 +23,7 @@
|
||||
<module>sa-token-fastjson</module>
|
||||
<module>sa-token-fastjson2</module>
|
||||
<module>sa-token-snack3</module>
|
||||
<module>sa-token-snack4</module>
|
||||
<module>sa-token-hutool-timed-cache</module>
|
||||
<module>sa-token-caffeine</module>
|
||||
<module>sa-token-thymeleaf</module>
|
||||
|
||||
@@ -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>
|
||||
+54
@@ -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);
|
||||
}
|
||||
}
|
||||
+45
@@ -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;
|
||||
|
||||
}
|
||||
}
|
||||
+78
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
cn.dev33.satoken.plugin.SaTokenPluginForSnack4
|
||||
Reference in New Issue
Block a user