feat: 新增 SaSerializerTemplate 序列化器

This commit is contained in:
click33 2025-02-23 21:43:27 +08:00
parent b90754839c
commit e898ad70c5
5 changed files with 118 additions and 0 deletions

View File

@ -30,6 +30,8 @@ import cn.dev33.satoken.listener.SaTokenEventCenter;
import cn.dev33.satoken.log.SaLog;
import cn.dev33.satoken.log.SaLogForConsole;
import cn.dev33.satoken.same.SaSameTemplate;
import cn.dev33.satoken.serializer.SaSerializerTemplate;
import cn.dev33.satoken.serializer.SaSerializerTemplateDefaultImpl;
import cn.dev33.satoken.sign.SaSignTemplate;
import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpInterfaceDefaultImpl;
@ -226,6 +228,25 @@ public class SaManager {
return saJsonTemplate;
}
/**
* 序列化器
*/
private volatile static SaSerializerTemplate saSerializerTemplate;
public static void setSaSerializerTemplate(SaSerializerTemplate saSerializerTemplate) {
SaManager.saSerializerTemplate = saSerializerTemplate;
SaTokenEventCenter.doRegisterComponent("SaSerializerTemplate", saSerializerTemplate);
}
public static SaSerializerTemplate getSaSerializerTemplate() {
if (saSerializerTemplate == null) {
synchronized (SaManager.class) {
if (saSerializerTemplate == null) {
SaManager.saSerializerTemplate = new SaSerializerTemplateDefaultImpl();
}
}
}
return saSerializerTemplate;
}
/**
* API 参数签名
*/

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.serializer;
/**
* 序列化器
*
* @author click33
* @since 1.41.0
*/
public interface SaSerializerTemplate {
/**
* 序列化对象 -> 字符串
*
* @param obj /
* @return /
*/
String objectToString(Object obj);
/**
* 反序列化字符串 对象
*
* @param str /
* @return /
*/
Object stringToObject(String str);
}

View File

@ -0,0 +1,38 @@
/*
* 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.serializer;
import cn.dev33.satoken.SaManager;
/**
* 序列化器默认实现类 (使用 json 转换器)
*
* @author click33
* @since 1.41.0
*/
public class SaSerializerTemplateDefaultImpl implements SaSerializerTemplate {
@Override
public String objectToString(Object obj) {
return SaManager.getSaJsonTemplate().objectToJson(obj);
}
@Override
public Object stringToObject(String str) {
return SaManager.getSaJsonTemplate().jsonToObject(str);
}
}

View File

@ -29,6 +29,7 @@ import cn.dev33.satoken.listener.SaTokenEventCenter;
import cn.dev33.satoken.listener.SaTokenListener;
import cn.dev33.satoken.log.SaLog;
import cn.dev33.satoken.same.SaSameTemplate;
import cn.dev33.satoken.serializer.SaSerializerTemplate;
import cn.dev33.satoken.sign.SaSignTemplate;
import cn.dev33.satoken.solon.json.SaJsonTemplateForSnack3;
import cn.dev33.satoken.solon.model.SaContextForSolon;
@ -131,6 +132,11 @@ public class XPluginImp implements Plugin {
SaManager.setSaJsonTemplate(bean);
});
// Sa-Token 序列化器 Bean
context.getBeanAsync(SaSerializerTemplate.class, bean -> {
SaManager.setSaSerializerTemplate(bean);
});
// Sa-Token 参数签名算法 Bean
context.getBeanAsync(SaSignTemplate.class, bean -> {
SaManager.setSaSignTemplate(bean);

View File

@ -31,6 +31,7 @@ import cn.dev33.satoken.listener.SaTokenListener;
import cn.dev33.satoken.log.SaLog;
import cn.dev33.satoken.plugin.SaTokenPluginLoader;
import cn.dev33.satoken.same.SaSameTemplate;
import cn.dev33.satoken.serializer.SaSerializerTemplate;
import cn.dev33.satoken.sign.SaSignTemplate;
import cn.dev33.satoken.spring.pathmatch.SaPathMatcherHolder;
import cn.dev33.satoken.stp.StpInterface;
@ -185,6 +186,16 @@ public class SaBeanInject {
SaManager.setSaJsonTemplate(saJsonTemplate);
}
/**
* 注入自定义的序列化器 Bean
*
* @param saSerializerTemplate 序列化器
*/
@Autowired(required = false)
public void setSaSerializerTemplate(SaSerializerTemplate saSerializerTemplate) {
SaManager.setSaSerializerTemplate(saSerializerTemplate);
}
/**
* 注入自定义的 参数签名 Bean
*