diff --git a/sa-token-dependencies/pom.xml b/sa-token-dependencies/pom.xml index 1701c92126a2e3537fb8d8af754ef20f28c4fe19..64ca31c91e554fa67f4680ba3ee754b0773adac6 100644 --- a/sa-token-dependencies/pom.xml +++ b/sa-token-dependencies/pom.xml @@ -26,8 +26,9 @@ 3.0.9.RELEASE 2.3.34 3.2.1 - 1.6.9 - 3.2.133 + 1.8.2 + 3.2.139 + 4.0.17 4.9.17 3.14.4 1.1.2-java8 @@ -111,6 +112,13 @@ snack3 ${noear-snack3.version} + + + + org.noear + snack4 + ${noear-snack4.version} + diff --git a/sa-token-plugin/pom.xml b/sa-token-plugin/pom.xml index 92c9c783a1c27302b330e49e5effc14460fb47f7..9a64fa6f5e1cfed03848c48a224f5c5c8d34e2fe 100644 --- a/sa-token-plugin/pom.xml +++ b/sa-token-plugin/pom.xml @@ -23,6 +23,7 @@ sa-token-fastjson sa-token-fastjson2 sa-token-snack3 + sa-token-snack4 sa-token-hutool-timed-cache sa-token-caffeine sa-token-thymeleaf diff --git a/sa-token-plugin/sa-token-snack4/pom.xml b/sa-token-plugin/sa-token-snack4/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..aae42629dd5e90fc9078b54e7b8fccfb1c9932aa --- /dev/null +++ b/sa-token-plugin/sa-token-snack4/pom.xml @@ -0,0 +1,25 @@ + + + + sa-token-plugin + cn.dev33 + ${revision} + ../pom.xml + + 4.0.0 + + sa-token-snack4 + + + + cn.dev33 + sa-token-core + + + org.noear + snack4 + + + \ No newline at end of file diff --git a/sa-token-plugin/sa-token-snack4/src/main/java/cn/dev33/satoken/json/SaJsonTemplateForSnack4.java b/sa-token-plugin/sa-token-snack4/src/main/java/cn/dev33/satoken/json/SaJsonTemplateForSnack4.java new file mode 100644 index 0000000000000000000000000000000000000000..63e8302c6f25e21d2408f1d61a182c9598171255 --- /dev/null +++ b/sa-token-plugin/sa-token-snack4/src/main/java/cn/dev33/satoken/json/SaJsonTemplateForSnack4.java @@ -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 jsonToObject(String jsonStr, Class type) { + if (SaFoxUtil.isEmpty(jsonStr)) { + return null; + } + return ONode.deserialize(jsonStr, type, options); + } +} diff --git a/sa-token-plugin/sa-token-snack4/src/main/java/cn/dev33/satoken/plugin/SaTokenPluginForSnack4.java b/sa-token-plugin/sa-token-snack4/src/main/java/cn/dev33/satoken/plugin/SaTokenPluginForSnack4.java new file mode 100644 index 0000000000000000000000000000000000000000..2068f7ddaa386b0452b1197bcd2f5b36c789233a --- /dev/null +++ b/sa-token-plugin/sa-token-snack4/src/main/java/cn/dev33/satoken/plugin/SaTokenPluginForSnack4.java @@ -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; + + } +} \ No newline at end of file diff --git a/sa-token-plugin/sa-token-snack4/src/main/java/cn/dev33/satoken/session/SaSessionForSnack4Customized.java b/sa-token-plugin/sa-token-snack4/src/main/java/cn/dev33/satoken/session/SaSessionForSnack4Customized.java new file mode 100644 index 0000000000000000000000000000000000000000..e9a2255c1ac585654a5b5236fc19f9fedf782f1b --- /dev/null +++ b/sa-token-plugin/sa-token-snack4/src/main/java/cn/dev33/satoken/session/SaSessionForSnack4Customized.java @@ -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 泛型 + * @param key key + * @param cs 指定转换类型 + * @return 值 + */ + @Override + public T getModel(String key, Class 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); + } + } +} \ No newline at end of file diff --git a/sa-token-plugin/sa-token-snack4/src/main/resources/META-INF/satoken/cn.dev33.satoken.plugin.SaTokenPlugin b/sa-token-plugin/sa-token-snack4/src/main/resources/META-INF/satoken/cn.dev33.satoken.plugin.SaTokenPlugin new file mode 100644 index 0000000000000000000000000000000000000000..896c9535570d1bfd9bc4ed366f8089813449874c --- /dev/null +++ b/sa-token-plugin/sa-token-snack4/src/main/resources/META-INF/satoken/cn.dev33.satoken.plugin.SaTokenPlugin @@ -0,0 +1 @@ +cn.dev33.satoken.plugin.SaTokenPluginForSnack4 \ No newline at end of file