获取ecology标准接口token示例

master
youhong.ai 2023-11-21 14:39:58 +08:00
commit ebf3a791c2
4 changed files with 219 additions and 0 deletions

39
.gitignore vendored Normal file
View File

@ -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

33
pom.xml Normal file
View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SH_EBU7_DEV1</groupId>
<artifactId>Ecology_External_Examples</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>aliyunmaven</id>
<name>aliyun macen</name>
<url>https://maven.aliyun.com/repository/public</url>
</repository>
</repositories>
</project>

View File

@ -0,0 +1,16 @@
package com.weaver.ebu7dev;
/**
* <h1>${Description}</h1>
*
* <p>create: 2023/11/21 14:26</p>
*
* @author youHong.ai
*/
public class Main {
public static void main(String[] args) {
// 具体示例信息请查看对应的包请求
System.out.println("Hello world!");
}
}

View File

@ -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;
/**
* <h1>ecologytoken</h1>
*
* <p>create: 2023/11/21 13:42</p>
*
* @author youHong.ai
*/
public class RegisterEcologyAndTokenUtil {
/** ecology方提供的appID */
private static final String APP_ID = "e5407287-820c-11ee-b7a0-cc208c8416bc";
/**
*
*/
private static final Map<String, String> SYSTEM_CACHE = new HashMap<>();
/**
* <h2></h2>
*
* @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);
}
/**
* <h2>ec</h2>
*
* @param ecUrlHost ecology
*
* @return
*/
private static Map<String, Object> 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<String, Object> 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;
}
/**
* <h2>ecologyapitoken</h2>
*
* @param ecUrlHost ecology
*
* @return token
*/
private static Map<String, Object> 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<String, Object> datas = JSONUtil.parseObj(data);
// ECOLOGY返回的token
// TODO 为Token缓存设置过期时间
SYSTEM_CACHE.put("SERVER_TOKEN", StrUtil.nullToEmpty((String) datas.get("token")));
return datas;
}
}