mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2025-12-08 17:12:28 +08:00
使用原生方式对接口进行测试 修复授权管理页面重置AK,SK未写入到数据库中的异常
使用原生方式对接口进行测试 修复授权管理页面重置AK,SK未写入到数据库中的异常
This commit is contained in:
@ -2,34 +2,83 @@ package org.jeecg.modules.openapi.test;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.jeecg.JeecgSystemApplication;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.methods.*;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,classes = JeecgSystemApplication.class)
|
||||
@AutoConfigureMockMvc
|
||||
public class SampleOpenApiTest {
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
private final String base_url = "http://localhost:8080/jeecg-boot";
|
||||
private final String appKey = "ak-eAU25mrMxhtaZsyS";
|
||||
private final String searchKey = "rjxMqB6YyUXpSHAz4DCIz8vZ5aozQQiV";
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
String url = "/openapi/call/wYAu6xwg";
|
||||
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get(url)
|
||||
.param("id","a7d7e77e06c84325a40932163adcdaa6")
|
||||
.header("appkey","ak-8CVxh8aYRkzZ0Z2u")
|
||||
.header("signature","3ec15caeaf9b6281d0ab825795f61e2d")
|
||||
.header("timestamp","1747403650402");
|
||||
String result = mockMvc.perform(requestBuilder).andReturn().getResponse().getContentAsString();
|
||||
JSONObject jsonObject = JSON.parseObject(result);
|
||||
Assertions.assertEquals(true, jsonObject.getBoolean("success"));
|
||||
System.out.println(jsonObject);
|
||||
String url = base_url+"/openapi/call/wYAu6xwg?id=a7d7e77e06c84325a40932163adcdaa6";
|
||||
JSONObject header = genTimestampAndSignature();
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
// 设置请求头
|
||||
httpGet.setHeader("Content-Type", "application/json");
|
||||
httpGet.setHeader("appkey",appKey);
|
||||
httpGet.setHeader("signature",header.get("signature").toString());
|
||||
httpGet.setHeader("timestamp",header.get("timestamp").toString());
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
CloseableHttpResponse response = httpClient.execute(httpGet);) {
|
||||
// 获取响应状态码
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
System.out.println("[debug] 响应状态码: " + statusCode);
|
||||
|
||||
HttpEntity entity = response.getEntity();
|
||||
System.out.println(entity);
|
||||
// 获取响应内容
|
||||
String responseBody = EntityUtils.toString(response.getEntity());
|
||||
System.out.println("[debug] 响应内容: " + responseBody);
|
||||
|
||||
// 解析JSON响应
|
||||
JSONObject res = JSON.parseObject(responseBody);
|
||||
System.out.println("[info] 调用成功: " + res.toJSONString());
|
||||
}
|
||||
|
||||
}
|
||||
private JSONObject genTimestampAndSignature(){
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
long timestamp = System.currentTimeMillis();
|
||||
jsonObject.put("timestamp",timestamp);
|
||||
jsonObject.put("signature", md5(appKey + searchKey + timestamp));
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成md5
|
||||
* @param sourceStr
|
||||
* @return
|
||||
*/
|
||||
protected String md5(String sourceStr) {
|
||||
String result = "";
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
md.update(sourceStr.getBytes("utf-8"));
|
||||
byte[] hash = md.digest();
|
||||
int i;
|
||||
StringBuffer buf = new StringBuffer(32);
|
||||
for (int offset = 0; offset < hash.length; offset++) {
|
||||
i = hash[offset];
|
||||
if (i < 0) {
|
||||
i += 256;
|
||||
}
|
||||
if (i < 16) {
|
||||
buf.append("0");
|
||||
}
|
||||
buf.append(Integer.toHexString(i));
|
||||
}
|
||||
result = buf.toString();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("sign签名错误", e);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user