diff --git a/epmet-module/epmet-demo/epmet-demo-server/pom.xml b/epmet-module/epmet-demo/epmet-demo-server/pom.xml index 2a2ee9e597..03635124a5 100644 --- a/epmet-module/epmet-demo/epmet-demo-server/pom.xml +++ b/epmet-module/epmet-demo/epmet-demo-server/pom.xml @@ -92,6 +92,11 @@ 2.0.0 compile + + com.epmet + epmet-openapi-sdk + 2.0.0 + diff --git a/epmet-module/epmet-demo/epmet-demo-server/src/test/java/com/epmet/test/openapi/TestTakeTokenOpenApi.java b/epmet-module/epmet-demo/epmet-demo-server/src/test/java/com/epmet/test/openapi/TestTakeTokenOpenApi.java new file mode 100644 index 0000000000..c07080e687 --- /dev/null +++ b/epmet-module/epmet-demo/epmet-demo-server/src/test/java/com/epmet/test/openapi/TestTakeTokenOpenApi.java @@ -0,0 +1,135 @@ +package com.epmet.test.openapi; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.epmet.openapi.sdk.sign.OpenApiSignUtils; +import com.google.gson.JsonObject; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; +import org.junit.Test; + +import java.util.HashMap; +import java.util.UUID; + +/** + * TakeToken方式的第三方平台接入测试类 + */ +public class TestTakeTokenOpenApi { + + String appId = "1504335474091569153"; + String authType = "take_token"; + String secret = "70e7ee0592d94affaa6e7b463926a3dd3cf1606945644baf810f93e8e9638c50"; + + @Test + public void testIt() throws Exception { + String accessToken = getAccessToken(); + //execBusinessWithSign(accessToken); + execBusinessWithoutSign(accessToken); + + } + + /** + * 获取accessToken + * @return + * @throws Exception + */ + private String getAccessToken() throws Exception { + String timestamp = String.valueOf(System.currentTimeMillis()); + String nonce = UUID.randomUUID().toString().replace("-", ""); + + HashMap content = new HashMap<>(); + content.put("app_id", appId); + content.put("timestamp", timestamp); + content.put("nonce", nonce); + content.put("auth_type", authType); + + String sign = OpenApiSignUtils.createSign(content, secret); + + String takeTokenUrl = String.format("http://localhost:8080/api/epmet/ext/open-api/get-access-token?auth_type=%s&app_id=%s×tamp=%s&sign=%s&nonce=%s", authType, appId, timestamp, sign, nonce); + + CloseableHttpClient httpClient = HttpClientBuilder.create().build(); + HttpPost httpPost = new HttpPost(takeTokenUrl); + CloseableHttpResponse response = httpClient.execute(httpPost); + String result = EntityUtils.toString(response.getEntity()); + JSONObject resultObject = JSON.parseObject(result).getJSONObject("data"); + + return resultObject.getString("accessToken"); + } + + /** + * 执行需要校验签名业务方法 + * @param accessToken + */ + private void execBusinessWithSign(String accessToken) throws Exception { + String orgId = "1"; + String test = "2"; + + String timestamp = String.valueOf(System.currentTimeMillis()); + String nonce = UUID.randomUUID().toString().replace("-", ""); + + // 签名参数 + HashMap createSignParams = new HashMap<>(); + createSignParams.put("orgId", orgId); + createSignParams.put("test", test); + + createSignParams.put("app_id", appId); + createSignParams.put("timestamp", timestamp); + createSignParams.put("nonce", nonce); + createSignParams.put("auth_type", authType); + + String sign = OpenApiSignUtils.createSign(createSignParams, secret); + + String businessUrl = String.format("http://localhost:8080/api/epmet/ext/open-api/get-org-detail?auth_type=%s&app_id=%s×tamp=%s&sign=%s&nonce=%s", authType, appId, timestamp, sign, nonce); + + // 业务参数 + JsonObject bizParam = new JsonObject(); + bizParam.addProperty("orgId", "3"); + bizParam.addProperty("test", test); + + // 发送请求 + CloseableHttpClient httpClient = HttpClientBuilder.create().build(); + HttpPost httpPost = new HttpPost(businessUrl); + httpPost.addHeader("accesstoken", accessToken); + httpPost.addHeader("content-type", "application/json;charset=utf-8"); + httpPost.setEntity(new StringEntity(bizParam.toString(), "utf-8")); + CloseableHttpResponse response = httpClient.execute(httpPost); + + // 解析结果 + String result = EntityUtils.toString(response.getEntity()); + System.out.println(result); + } + + /** + * 执行不校验签名业务方法 + * @param accessToken + */ + private void execBusinessWithoutSign(String accessToken) throws Exception { + String orgId = "1"; + String test = "2"; + + // 不需要签名,但是仍然要传递app_id和auth_type参数,以及accesstoken + String businessUrl = String.format("http://localhost:8080/api/epmet/ext/open-api/get-org-detail?auth_type=%s&app_id=%s", authType, appId); + + // 业务参数 + JsonObject bizParam = new JsonObject(); + bizParam.addProperty("orgId", "3"); + bizParam.addProperty("test", test); + + // 发送请求 + CloseableHttpClient httpClient = HttpClientBuilder.create().build(); + HttpPost httpPost = new HttpPost(businessUrl); + httpPost.addHeader("accesstoken", accessToken); + httpPost.addHeader("content-type", "application/json;charset=utf-8"); + httpPost.setEntity(new StringEntity(bizParam.toString(), "utf-8")); + CloseableHttpResponse response = httpClient.execute(httpPost); + + // 解析结果 + String result = EntityUtils.toString(response.getEntity()); + System.out.println(result); + } + +} diff --git a/epmet-module/epmet-ext/epmet-ext-client/src/main/java/com/epmet/dto/form/openapi/GetOrgDetailFormDTO.java b/epmet-module/epmet-ext/epmet-ext-client/src/main/java/com/epmet/dto/form/openapi/GetOrgDetailFormDTO.java index 820225682a..207e88bb27 100644 --- a/epmet-module/epmet-ext/epmet-ext-client/src/main/java/com/epmet/dto/form/openapi/GetOrgDetailFormDTO.java +++ b/epmet-module/epmet-ext/epmet-ext-client/src/main/java/com/epmet/dto/form/openapi/GetOrgDetailFormDTO.java @@ -5,7 +5,7 @@ import lombok.Data; import javax.validation.constraints.NotBlank; @Data -public class GetOrgDetailFormDTO extends OpenApiBaseFormDTO { +public class GetOrgDetailFormDTO { @NotBlank(message = "orgId不能为空") private String orgId; diff --git a/epmet-module/epmet-ext/epmet-ext-server/src/main/java/com/epmet/controller/OpenApiOrgController.java b/epmet-module/epmet-ext/epmet-ext-server/src/main/java/com/epmet/controller/OpenApiOrgController.java index 30e97da2bf..24bde19031 100644 --- a/epmet-module/epmet-ext/epmet-ext-server/src/main/java/com/epmet/controller/OpenApiOrgController.java +++ b/epmet-module/epmet-ext/epmet-ext-server/src/main/java/com/epmet/controller/OpenApiOrgController.java @@ -19,7 +19,7 @@ public class OpenApiOrgController { @PostMapping("/get-org-detail") public Result getOrgDetail(@RequestBody GetOrgDetailFormDTO input, @RequestHeader("AppId") String appId) { - return new Result().ok("测试org"); + return new Result().ok(String.format("测试org:orgId=%s,test=%s", input.getOrgId(), input.getTest())); }