diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/SaManager.java b/sa-token-core/src/main/java/cn/dev33/satoken/SaManager.java index 1fb7c22e..43c6714e 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/SaManager.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/SaManager.java @@ -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 参数签名 */ diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/serializer/SaSerializerTemplate.java b/sa-token-core/src/main/java/cn/dev33/satoken/serializer/SaSerializerTemplate.java new file mode 100644 index 00000000..8eb8ed03 --- /dev/null +++ b/sa-token-core/src/main/java/cn/dev33/satoken/serializer/SaSerializerTemplate.java @@ -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); + +} diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/serializer/SaSerializerTemplateDefaultImpl.java b/sa-token-core/src/main/java/cn/dev33/satoken/serializer/SaSerializerTemplateDefaultImpl.java new file mode 100644 index 00000000..321c9c1b --- /dev/null +++ b/sa-token-core/src/main/java/cn/dev33/satoken/serializer/SaSerializerTemplateDefaultImpl.java @@ -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); + } + +} diff --git a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/XPluginImp.java b/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/XPluginImp.java index 51f869ac..ef968659 100644 --- a/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/XPluginImp.java +++ b/sa-token-starter/sa-token-solon-plugin/src/main/java/cn/dev33/satoken/solon/XPluginImp.java @@ -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); diff --git a/sa-token-starter/sa-token-spring-boot-autoconfig/src/main/java/cn/dev33/satoken/spring/SaBeanInject.java b/sa-token-starter/sa-token-spring-boot-autoconfig/src/main/java/cn/dev33/satoken/spring/SaBeanInject.java index e02f33d9..f8154d44 100644 --- a/sa-token-starter/sa-token-spring-boot-autoconfig/src/main/java/cn/dev33/satoken/spring/SaBeanInject.java +++ b/sa-token-starter/sa-token-spring-boot-autoconfig/src/main/java/cn/dev33/satoken/spring/SaBeanInject.java @@ -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 *