Browse Source

新增

1.第三方平台接入示例
dev
wangxianzhang 3 years ago
parent
commit
bd9857c150
  1. 5
      epmet-module/epmet-demo/epmet-demo-server/pom.xml
  2. 135
      epmet-module/epmet-demo/epmet-demo-server/src/test/java/com/epmet/test/openapi/TestTakeTokenOpenApi.java
  3. 2
      epmet-module/epmet-ext/epmet-ext-client/src/main/java/com/epmet/dto/form/openapi/GetOrgDetailFormDTO.java
  4. 2
      epmet-module/epmet-ext/epmet-ext-server/src/main/java/com/epmet/controller/OpenApiOrgController.java

5
epmet-module/epmet-demo/epmet-demo-server/pom.xml

@ -92,6 +92,11 @@
<version>2.0.0</version> <version>2.0.0</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>com.epmet</groupId>
<artifactId>epmet-openapi-sdk</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

135
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<String, String> 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&timestamp=%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<String, String> 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&timestamp=%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);
}
}

2
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; import javax.validation.constraints.NotBlank;
@Data @Data
public class GetOrgDetailFormDTO extends OpenApiBaseFormDTO { public class GetOrgDetailFormDTO {
@NotBlank(message = "orgId不能为空") @NotBlank(message = "orgId不能为空")
private String orgId; private String orgId;

2
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") @PostMapping("/get-org-detail")
public Result getOrgDetail(@RequestBody GetOrgDetailFormDTO input, public Result getOrgDetail(@RequestBody GetOrgDetailFormDTO input,
@RequestHeader("AppId") String appId) { @RequestHeader("AppId") String appId) {
return new Result().ok("测试org"); return new Result().ok(String.format("测试org:orgId=%s,test=%s", input.getOrgId(), input.getTest()));
} }

Loading…
Cancel
Save