refactor: 重构 sa-token-fastjson、sa-token-fastjson2 适配 SPI 机制

This commit is contained in:
click33
2025-02-22 02:56:49 +08:00
parent 0d9fed1558
commit 7c3febda60
24 changed files with 223 additions and 603 deletions

View File

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

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-fastjson</artifactId>
<dependencies>
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-core</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,56 @@
/*
* 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.session.SaSessionForFastjsonCustomized;
import com.alibaba.fastjson.JSON;
import java.util.Map;
/**
* JSON 转换器, Fastjson 版实现
*
* @author click33
* @since 1.34.0
*/
public class SaJsonTemplateForFastjson implements SaJsonTemplate {
/**
* 序列化:对象 -> json 字符串
*/
@Override
public String objectToJson(Object obj) {
return JSON.toJSONString(obj);
}
/**
* 反序列化json 字符串 → 对象
*/
@Override
public Object jsonToObject(String jsonStr) {
// TODO: 此处待更改,需要让其自动识别类型
return JSON.parseObject(jsonStr, SaSessionForFastjsonCustomized.class);
}
/**
* 反序列化json 字符串 → Map
*/
@Override
public Map<String, Object> jsonToMap(String jsonStr) {
return JSON.parseObject(jsonStr, Map.class);
}
}

View File

@@ -0,0 +1,42 @@
/*
* 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.SaJsonTemplateForFastjson;
import cn.dev33.satoken.session.SaSessionForFastjsonCustomized;
import cn.dev33.satoken.strategy.SaStrategy;
/**
* SaToken 插件安装JSON 转换器 - Fastjson 版
*
* @author click33
* @since 1.41.0
*/
public class SaTokenPluginFastjson implements SaTokenPlugin {
@Override
public void setup() {
// 设置JSON转换器Fastjson 版
SaManager.setSaJsonTemplate(new SaJsonTemplateForFastjson());
// 重写 SaSession 生成策略
SaStrategy.instance.createSession = SaSessionForFastjsonCustomized::new;
}
}

View File

@@ -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 com.alibaba.fastjson.JSON;
/**
* Fastjson 定制版 SaSession重写类型转换API
*
* @author click33
* @since 1.34.0
*/
public class SaSessionForFastjsonCustomized extends SaSession {
private static final long serialVersionUID = -7600983549653130681L;
/**
* 构建一个 SaSession 对象
* @param id Session 的 id
*/
public SaSessionForFastjsonCustomized(String id) {
super(id);
}
/**
* 取值 (指定转换类型)
* @param <T> 泛型
* @param key key
* @param cs 指定转换类型
* @return 值
*/
@Override
public <T> T getModel(String key, Class<T> cs) {
if(SaFoxUtil.isBasicType(cs)) {
return SaFoxUtil.getValueByType(get(key), cs);
}
return JSON.parseObject(getString(key), cs);
}
/**
* 取值 (指定转换类型, 并指定值为Null时返回的默认值)
* @param <T> 泛型
* @param key key
* @param cs 指定转换类型
* @param defaultValue 值为Null时返回的默认值
* @return 值
*/
@Override
@SuppressWarnings("unchecked")
public <T> T getModel(String key, Class<T> cs, Object defaultValue) {
Object value = get(key);
if(valueIsNull(value)) {
return (T)defaultValue;
}
if(SaFoxUtil.isBasicType(cs)) {
return SaFoxUtil.getValueByType(get(key), cs);
}
return JSON.parseObject(getString(key), cs);
}
}

View File

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