mirror of https://github.com/jeecgboot/jeecg-boot
使用原生方式对接口进行测试 修复授权管理页面重置AK,SK未写入到数据库中的异常
使用原生方式对接口进行测试 修复授权管理页面重置AK,SK未写入到数据库中的异常pull/8358/head
parent
0b10096f1c
commit
2d62bad2a9
|
@ -8,8 +8,6 @@ import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|||
import org.springframework.http.converter.StringHttpMessageConverter;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
@ -32,42 +30,18 @@ public class RestUtil {
|
|||
// 微服务版集成企业微信单点登录
|
||||
// 因为微服务版没有端口号,导致 SpringContextUtils.getDomain() 方法获取的域名的端口号变成了:-1所以出问题了,只需要把这个-1给去掉就可以了。
|
||||
String port=":-1";
|
||||
//单元测试导致无端口号的问题
|
||||
if (!domain.endsWith(port)){
|
||||
checkAndAddPort();
|
||||
}
|
||||
if (domain.endsWith(port)) {
|
||||
domain = domain.substring(0, domain.length() - 3);
|
||||
}
|
||||
}
|
||||
return domain;
|
||||
}
|
||||
|
||||
private static void checkAndAddPort() {
|
||||
try {
|
||||
URL url = new URL(domain);
|
||||
int port = url.getPort();
|
||||
if (port == -1) {
|
||||
domain = domain+":"+getPort();
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
log.warn("获取端口号异常");
|
||||
}
|
||||
}
|
||||
|
||||
private static String getPath() {
|
||||
if (path == null) {
|
||||
path = SpringContextUtils.getApplicationContext().getEnvironment().getProperty("server.servlet.context-path");
|
||||
}
|
||||
return oConvertUtils.getString(path);
|
||||
}
|
||||
private static String getPort() {
|
||||
String property = SpringContextUtils.getApplicationContext().getEnvironment().getProperty("server.port");
|
||||
if (StringUtils.isEmpty(property)||"0".equals(property)) {
|
||||
property = "8080";
|
||||
}
|
||||
return oConvertUtils.getString(property);
|
||||
}
|
||||
|
||||
public static String getBaseUrl() {
|
||||
String basepath = getDomain() + getPath();
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -87,8 +87,10 @@ export const batchDelete = (params, handleSuccess) => {
|
|||
* @param isUpdate
|
||||
*/
|
||||
export const saveOrUpdate = (params, isUpdate) => {
|
||||
let url = isUpdate ? Api.edit : Api.save;
|
||||
return defHttp.post({ url: url, params }, { isTransformResponse: false });
|
||||
if (isUpdate) {
|
||||
return defHttp.put({ url: Api.edit, params }, { isTransformResponse: false });
|
||||
}
|
||||
return defHttp.post({ url: Api.save, params }, { isTransformResponse: false });
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -80,13 +80,12 @@
|
|||
batchDelete,
|
||||
getImportUrl,
|
||||
getExportUrl,
|
||||
getGenAKSK
|
||||
getGenAKSK, saveOrUpdate
|
||||
} from "./OpenApiAuth.api";
|
||||
import OpenApiAuthModal from './components/OpenApiAuthModal.vue'
|
||||
import AuthModal from './components/AuthModal.vue'
|
||||
import { useUserStore } from '/@/store/modules/user';
|
||||
import JSearchSelect from "../../components/Form/src/jeecg/components/JSearchSelect.vue";
|
||||
import { saveOrUpdate } from "@/views/openapi/OpenApi.api";
|
||||
|
||||
const formRef = ref();
|
||||
const queryParam = reactive<any>({});
|
||||
|
|
Loading…
Reference in New Issue