Appearance
快速开始
成为接入方
联系管理员申请accessKey
和secretKey
即可成为接入方。
生成签名
签名使用HMACSHA256
加密生成。 原始加密串为Gsoft-open
+请求的api
+当前时间戳
,加密key为secretKey
。
java
//申请的secretKey
String secretKey = "453dd5dbff98430eb8cc617e5b59490d";
//需要请求的api
String api = "/role/getRoleList";
//当前时间戳
Date date = new Date();
Long timestamp = date.getTime();
//原始加密串
String originalStr = String.format("Gsoft-open%s%d", api, timestamp);
//生成签名
String sign = HMacUtils.HMACSHA256(originalStr, secretKey);
HMACSHA256(Java):
java
public static String HMACSHA256(String data, String key) {
StringBuilder sb = new StringBuilder();
try {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"),"HmacSHA256");
sha256_HMAC.init(secret_key);
byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
for (byte item : array) {
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1,3));
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
设置请求头
请求api时,需要携带认证和当前时间戳的请求头。
认证请求头由"Gsoft
+accessKey
+:
+sign
组成。
java
//生成认证请求头
String authorization = String.format("Gsoft %s:%s", accessKey, sign);
//设置认证请求头
httpPost.setHeader("Gsoft-Open-Authorization", authorization);
//设置当前时间戳请求头
httpPost.setHeader("Gsoft-Open-Timestamp", timestamp.toString());
发送请求
java
//申请的accessKey
String accessKey = "fyXKROkcMeMYXGeX";
//申请的secretKey
String secretKey = "453dd5dbff98430eb8cc617e5b59490d";
//需要请求的api
String api = "/mas-admin/open/role/getRoleList";
//当前时间戳
Date date = new Date();
Long timestamp = date.getTime();
//原始加密串
String originalStr = String.format("Gsoft-open%s%d", api, timestamp);
//生成签名
String sign = HMacUtils.HMACSHA256(originalStr, secretKey);
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
String domain = "http://192.168.1.220/open-api";
HttpPost httpPost = new HttpPost(domain + api);
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
//生成认证请求头
String authorization = String.format("Gsoft %s:%s", accessKey, sign);
//设置认证请求头
httpPost.setHeader("Gsoft-Open-Authorization", authorization);
//设置当前时间戳请求头
httpPost.setHeader("Gsoft-Open-Timestamp", timestamp.toString());
CloseableHttpResponse response;
try {
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
//响应内容为:{"code":0,"data":[{"id":7,"createTime":"2023-07-03 16:06:57","creator":5,"lastModifyTime":"2023-07-03 16:06:57","lastModifier":5,"sortNumber":1,"roleCode":"valid","roleName":"验证问题","roleType":1,"siteId":5},{"id":37,"createTime":"2023-07-31 15:36:05","creator":5,"lastModifyTime":"2023-07-31 15:36:05","lastModifier":5,"sortNumber":1,"roleCode":"cscode","roleName":"测试名称","roleType":1,"siteId":5}],"msg":"执行成功"}