From ebf3a791c26a982ef12c5b3d44cd77fbdcd163b5 Mon Sep 17 00:00:00 2001 From: "youhong.ai" Date: Tue, 21 Nov 2023 14:39:58 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96ecology=E6=A0=87=E5=87=86?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3token=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 39 ++++++ pom.xml | 33 +++++ src/main/java/com.weaver.ebu7dev/Main.java | 16 +++ .../RegisterEcologyAndTokenUtil.java | 131 ++++++++++++++++++ 4 files changed, 219 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/com.weaver.ebu7dev/Main.java create mode 100644 src/main/java/com.weaver.ebu7dev/examples/registertoken/RegisterEcologyAndTokenUtil.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..acb7754 --- /dev/null +++ b/.gitignore @@ -0,0 +1,39 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr +.idea + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..cdfeb36 --- /dev/null +++ b/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + SH_EBU7_DEV1 + Ecology_External_Examples + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + + + cn.hutool + hutool-all + 5.3.0 + + + + + + aliyunmaven + aliyun macen + https://maven.aliyun.com/repository/public + + + + \ No newline at end of file diff --git a/src/main/java/com.weaver.ebu7dev/Main.java b/src/main/java/com.weaver.ebu7dev/Main.java new file mode 100644 index 0000000..6bd61e8 --- /dev/null +++ b/src/main/java/com.weaver.ebu7dev/Main.java @@ -0,0 +1,16 @@ +package com.weaver.ebu7dev; + + +/** + *

${Description}

+ * + *

create: 2023/11/21 14:26

+ * + * @author youHong.ai + */ +public class Main { + public static void main(String[] args) { + // 具体示例信息请查看对应的包请求 + System.out.println("Hello world!"); + } +} \ No newline at end of file diff --git a/src/main/java/com.weaver.ebu7dev/examples/registertoken/RegisterEcologyAndTokenUtil.java b/src/main/java/com.weaver.ebu7dev/examples/registertoken/RegisterEcologyAndTokenUtil.java new file mode 100644 index 0000000..7cea7a0 --- /dev/null +++ b/src/main/java/com.weaver.ebu7dev/examples/registertoken/RegisterEcologyAndTokenUtil.java @@ -0,0 +1,131 @@ +package environment; + +import cn.hutool.core.util.StrUtil; +import cn.hutool.crypto.asymmetric.KeyType; +import cn.hutool.crypto.asymmetric.RSA; +import cn.hutool.http.HttpRequest; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; + +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +/** + *

ecology注册并获取token

+ * + *

create: 2023/11/21 13:42

+ * + * @author youHong.ai + */ +public class RegisterEcologyAndTokenUtil { + + + /** ecology方提供的appID */ + private static final String APP_ID = "e5407287-820c-11ee-b7a0-cc208c8416bc"; + + /** + * 模拟缓存服务 + */ + private static final Map SYSTEM_CACHE = new HashMap<>(); + + /** + *

主方法,测试

+ * + * @param args 参数 + */ + public static void main(String[] args) { + // 注意这里是对应ECOLOGY 系统的地址数据信息 + String ecUrlHost = "http://10.86.0.248"; + // ECOLOGY返回的token + String token = SYSTEM_CACHE.get("SERVER_TOKEN"); + if (StrUtil.isEmpty(token)) { + token = (String) getEcToken(ecUrlHost).get("token"); + } + String spk = SYSTEM_CACHE.get("SERVER_PUBLIC_KEY"); + // 封装请求头参数 + RSA rsa = new RSA(null, spk); + // 对用户信息进行加密传输,暂仅支持传输OA用户ID + String encryptUserid = rsa.encryptBase64("1", StandardCharsets.UTF_8, KeyType.PublicKey); + // 调用ECOLOGY系统接口,注意此处的disableCookie,可翻阅hutool的文档查看 + JSONObject jsonParams = new JSONObject(); + String data = HttpRequest + .get(ecUrlHost + "/api/hrm/organization/getCompanyFormField") + .header("appid", APP_ID) + .header("token", token) + .header("userid", encryptUserid) + .execute().body(); + System.out.println("call ecology api result is:" + data); + } + + /** + *

向ec注册服务

+ * + * @param ecUrlHost ecology域名 + * + * @return 注册后返回的密钥和公钥 + */ + + private static Map registerEc(String ecUrlHost) { + // 获取当前系统RSA加密的公钥 + RSA rsa = new RSA(); + String publicKey = rsa.getPublicKeyBase64(); + String privateKey = rsa.getPrivateKeyBase64(); + + // 客户端RSA私钥 + SYSTEM_CACHE.put("LOCAL_PRIVATE_KEY", privateKey); + // 客户端RSA公钥 + SYSTEM_CACHE.put("LOCAL_PUBLIC_KEY", publicKey); + + // 调用ECOLOGY系统接口进行注册 + String data = HttpRequest.post(ecUrlHost + "/api/ec/dev/auth/regist") + .header("appid", APP_ID) + .header("cpk", publicKey) + .timeout(2000) + .execute().body(); + Map datas = JSONUtil.parseObj(data); + // ECOLOGY返回的系统公钥 + SYSTEM_CACHE.put("SERVER_PUBLIC_KEY", StrUtil.nullToEmpty((String) datas.get("spk"))); + // ECOLOGY返回的系统密钥 + SYSTEM_CACHE.put("SERVER_SECRET", StrUtil.nullToEmpty((String) datas.get("secrit"))); + return datas; + } + + + /** + *

从ecology系统获取api调用token

+ * + * @param ecUrlHost ecology域名 + * + * @return token数据信息 + */ + private static Map getEcToken(String ecUrlHost) { + // 从系统缓存或者数据库中获取ECOLOGY系统公钥和Secret信息 + String secret = SYSTEM_CACHE.get("SERVER_SECRET"); + String spk = SYSTEM_CACHE.get("SERVER_PUBLIC_KEY"); + // 如果为空,说明还未进行注册,调用注册接口进行注册认证与数据更新 + if (Objects.isNull(secret) || Objects.isNull(spk)) { + registerEc(ecUrlHost); + // 重新获取最新ECOLOGY系统公钥和Secret信息 + secret = SYSTEM_CACHE.get("SERVER_SECRET"); + spk = SYSTEM_CACHE.get("SERVER_PUBLIC_KEY"); + } + // 公钥加密,所以RSA对象私钥为null + RSA rsa = new RSA(null, spk); + // 对秘钥进行加密传输,防止篡改数据 + String encryptSecret = rsa.encryptBase64(secret, StandardCharsets.UTF_8, KeyType.PublicKey); + String data = HttpRequest.post(ecUrlHost + "/api/ec/dev/auth/applytoken") + .header("appid", APP_ID) + .header("secret", encryptSecret) + .header("time", "3600") + .execute().body(); + + System.out.println("Ecology token is :" + data); + Map datas = JSONUtil.parseObj(data); + // ECOLOGY返回的token + // TODO 为Token缓存设置过期时间 + SYSTEM_CACHE.put("SERVER_TOKEN", StrUtil.nullToEmpty((String) datas.get("token"))); + return datas; + } +}