From 6ce8f38aaf4e503a0228baba4f64eeea82df78d1 Mon Sep 17 00:00:00 2001 From: click33 <2393584716@qq.com> Date: Fri, 16 May 2025 00:43:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20sa-token-okhttps?= =?UTF-8?q?=20=E6=8F=92=E4=BB=B6=EF=BC=8C=E6=95=B4=E5=90=88=20OkHttps=20?= =?UTF-8?q?=E4=BD=9C=E4=B8=BA=E8=AF=B7=E6=B1=82=E5=A4=84=E7=90=86=E5=99=A8?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sa-token-bom/pom.xml | 10 ++++ .../cn/dev33/satoken/http/SaHttpUtil.java | 51 +++++++++++++++++++ sa-token-dependencies/pom.xml | 8 +++ sa-token-doc/start/download.md | 3 +- sa-token-plugin/pom.xml | 1 + sa-token-plugin/sa-token-okhttps/pom.xml | 25 +++++++++ .../http/SaHttpTemplateForOkHttps.java | 47 +++++++++++++++++ .../plugin/SaTokenPluginForOkHttps.java | 35 +++++++++++++ .../cn.dev33.satoken.plugin.SaTokenPlugin | 1 + 9 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 sa-token-core/src/main/java/cn/dev33/satoken/http/SaHttpUtil.java create mode 100644 sa-token-plugin/sa-token-okhttps/pom.xml create mode 100644 sa-token-plugin/sa-token-okhttps/src/main/java/cn/dev33/satoken/http/SaHttpTemplateForOkHttps.java create mode 100644 sa-token-plugin/sa-token-okhttps/src/main/java/cn/dev33/satoken/plugin/SaTokenPluginForOkHttps.java create mode 100644 sa-token-plugin/sa-token-okhttps/src/main/resources/META-INF/satoken/cn.dev33.satoken.plugin.SaTokenPlugin diff --git a/sa-token-bom/pom.xml b/sa-token-bom/pom.xml index ff95e99f..ffe5a0b8 100644 --- a/sa-token-bom/pom.xml +++ b/sa-token-bom/pom.xml @@ -139,6 +139,16 @@ sa-token-redis-jackson ${revision} + + cn.dev33 + sa-token-forest + ${revision} + + + cn.dev33 + sa-token-okhttps + ${revision} + cn.dev33 sa-token-redisson-spring-boot-starter diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/http/SaHttpUtil.java b/sa-token-core/src/main/java/cn/dev33/satoken/http/SaHttpUtil.java new file mode 100644 index 00000000..337e4b79 --- /dev/null +++ b/sa-token-core/src/main/java/cn/dev33/satoken/http/SaHttpUtil.java @@ -0,0 +1,51 @@ +/* + * 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.http; + +import cn.dev33.satoken.SaManager; + +import java.util.Map; + +/** + * Http 请求处理器 工具类 + * + * @author click33 + * @since 1.43.0 + */ +public class SaHttpUtil { + + /** + * get 请求 + * + * @param url / + * @return / + */ + public static String get(String url) { + return SaManager.getSaHttpTemplate().get(url); + } + + /** + * post 请求,form-data 格式参数 + * + * @param url / + * @param params / + * @return / + */ + public static String postByFormData(String url, Map params) { + return SaManager.getSaHttpTemplate().postByFormData(url, params); + } + +} diff --git a/sa-token-dependencies/pom.xml b/sa-token-dependencies/pom.xml index 746668bf..0abc052a 100644 --- a/sa-token-dependencies/pom.xml +++ b/sa-token-dependencies/pom.xml @@ -41,6 +41,7 @@ 5.8.36 3.2.0 1.6.4 + 4.1.0 @@ -282,6 +283,13 @@ ${forest.version} + + + cn.zhxu + okhttps + ${okhttps.version} + + cn.dev33 diff --git a/sa-token-doc/start/download.md b/sa-token-doc/start/download.md index a2a9e815..6655f2b5 100644 --- a/sa-token-doc/start/download.md +++ b/sa-token-doc/start/download.md @@ -214,7 +214,8 @@ Maven依赖一直无法加载成功?[参考解决方案](https://sa-token.cc/d ├── sa-token-grpc // [插件] Sa-Token 整合 gRPC (RPC 调用鉴权、状态传递) ├── sa-token-quick-login // [插件] Sa-Token 快速注入登录页插件 ├── sa-token-redisson-spring-boot-starter // [插件] Sa-Token 整合 Redisson - SpringBoot 自动配置包 (数据缓存插件) - ├── sa-token-forest // [插件] Sa-Token 整合 Forest,http 请求 + ├── sa-token-forest // [插件] Sa-Token 整合 Forest,http 请求处理器 + ├── sa-token-okhttps // [插件] Sa-Token 整合 OkHttps,http 请求处理器 ├── sa-token-demo // [示例] Sa-Token 示例合集 ├── sa-token-demo-alone-redis // [示例] Sa-Token 集成 alone-redis 模块 ├── sa-token-demo-alone-redis-cluster // [示例] Sa-Token 集成 alone-redis 模块、集群模式 diff --git a/sa-token-plugin/pom.xml b/sa-token-plugin/pom.xml index 7266d955..92c9c783 100644 --- a/sa-token-plugin/pom.xml +++ b/sa-token-plugin/pom.xml @@ -39,6 +39,7 @@ sa-token-redisx sa-token-serializer-features sa-token-forest + sa-token-okhttps sa-token-redis-template diff --git a/sa-token-plugin/sa-token-okhttps/pom.xml b/sa-token-plugin/sa-token-okhttps/pom.xml new file mode 100644 index 00000000..c8892294 --- /dev/null +++ b/sa-token-plugin/sa-token-okhttps/pom.xml @@ -0,0 +1,25 @@ + + + + sa-token-plugin + cn.dev33 + ${revision} + ../pom.xml + + 4.0.0 + + sa-token-okhttps + + + + cn.dev33 + sa-token-core + + + cn.zhxu + okhttps + + + \ No newline at end of file diff --git a/sa-token-plugin/sa-token-okhttps/src/main/java/cn/dev33/satoken/http/SaHttpTemplateForOkHttps.java b/sa-token-plugin/sa-token-okhttps/src/main/java/cn/dev33/satoken/http/SaHttpTemplateForOkHttps.java new file mode 100644 index 00000000..bc753e77 --- /dev/null +++ b/sa-token-plugin/sa-token-okhttps/src/main/java/cn/dev33/satoken/http/SaHttpTemplateForOkHttps.java @@ -0,0 +1,47 @@ +/* + * 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.http; + +import cn.dev33.satoken.SaManager; +import cn.zhxu.okhttps.OkHttps; + +import java.util.Map; + +/** + * Http 请求处理器, OkHttps 版实现 + * + * @author click33 + * @since 1.43.0 + */ +public class SaHttpTemplateForOkHttps implements SaHttpTemplate { + + @Override + public String get(String url) { + SaManager.log.debug("发起请求,GET:{}", url); + String res = OkHttps.sync(url).get().getBody().toString(); + SaManager.log.debug("返回结果:{}", res); + return res; + } + + @Override + public String postByFormData(String url, Map params) { + SaManager.log.debug("发起请求,POST:{}\t参数:{}", url, params); + String res = OkHttps.sync(url).addBodyPara(params).post().getBody().toString(); + SaManager.log.debug("返回结果:{}", res); + return res; + } + +} diff --git a/sa-token-plugin/sa-token-okhttps/src/main/java/cn/dev33/satoken/plugin/SaTokenPluginForOkHttps.java b/sa-token-plugin/sa-token-okhttps/src/main/java/cn/dev33/satoken/plugin/SaTokenPluginForOkHttps.java new file mode 100644 index 00000000..c51ff8eb --- /dev/null +++ b/sa-token-plugin/sa-token-okhttps/src/main/java/cn/dev33/satoken/plugin/SaTokenPluginForOkHttps.java @@ -0,0 +1,35 @@ +/* + * 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.http.SaHttpTemplateForOkHttps; + +/** + * SaToken 插件安装:Http 请求处理器 - OkHttps 版 + * + * @author click33 + * @since 1.43.0 + */ +public class SaTokenPluginForOkHttps implements SaTokenPlugin { + + @Override + public void install() { + // 设置 OkHttps 作为 Http 请求处理器 + SaManager.setSaHttpTemplate(new SaHttpTemplateForOkHttps()); + } + +} \ No newline at end of file diff --git a/sa-token-plugin/sa-token-okhttps/src/main/resources/META-INF/satoken/cn.dev33.satoken.plugin.SaTokenPlugin b/sa-token-plugin/sa-token-okhttps/src/main/resources/META-INF/satoken/cn.dev33.satoken.plugin.SaTokenPlugin new file mode 100644 index 00000000..aff8e913 --- /dev/null +++ b/sa-token-plugin/sa-token-okhttps/src/main/resources/META-INF/satoken/cn.dev33.satoken.plugin.SaTokenPlugin @@ -0,0 +1 @@ +cn.dev33.satoken.plugin.SaTokenPluginForOkHttps \ No newline at end of file