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);
}
}