最近更新时间:2024-08-12 10:30:23
java签名DEMO如下:
package example;
import org.apache.commons.codec.binary.Hex;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
public class KsyunIamClient {
private static final String AK = "Your AK"; // 请替换为您的AccessKey
private static final String SK = "Your SK"; // 请替换为您的SecretKey
private static final String SERVIER = "iam"; //访问的服务
private static final String REGION = "cn-beijing-6"; // 访问的区域
private static final String API_URL = "http://iam.api.ksyun.com"; // 接口地址
private static final String HOST = "iam.api.ksyun.com"; // 请求头Host
public static void main(String[] args) throws Exception {
String action = "ListUsers"; // 接口名称
String version = "2015-11-01"; // 接口版本
Map<String, String> params = new LinkedHashMap<>();
// 固定参数
params.put("Accesskey", AK);
params.put("Service", SERVIER);
params.put("Region", REGION);
params.put("Action", action);
params.put("Version", version);
params.put("Timestamp", getUTCTime());
params.put("SignatureVersion", "1.0");
params.put("SignatureMethod", "HMAC-SHA256");
// 接口参数
params.put("Marker", "1");
params.put("MaxItems", "100");
String signature = sign(params, SK);
StringBuilder requestURL = new StringBuilder(API_URL + "?");
for (Map.Entry<String, String> entry : params.entrySet()) {
requestURL.append(URLEncoder.encode(entry.getKey(), "UTF-8"))
.append("=")
.append(URLEncoder.encode(entry.getValue(), "UTF-8"))
.append("&");
}
requestURL.append("Signature=").append(URLEncoder.encode(signature, "UTF-8"));
HttpClient client = HttpClients.createDefault();
HttpGet request = new HttpGet(requestURL.toString());
// 设置请求头
request.setHeader(HttpHeaders.ACCEPT, "application/json");
request.setHeader(HttpHeaders.HOST, HOST);
// 发送请求并处理响应
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
String responseBody = EntityUtils.toString(entity);
System.out.println("Response Status: " + response.getStatusLine());
System.out.println("Response Body: " + responseBody);
}
}
private static String sign(Map<String, String> params, String secretKey) throws Exception {
List<String> keys = new ArrayList<>(params.keySet());
Collections.sort(keys);
StringBuilder paramStr = new StringBuilder();
for (String key : keys) {
paramStr.append(URLEncoder.encode(key, "UTF-8"))
.append("=")
.append(URLEncoder.encode(params.get(key), "UTF-8"))
.append("&");
}
paramStr.deleteCharAt(paramStr.length() - 1);
Mac hmac = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
hmac.init(secretKeySpec);
byte[] hash = hmac.doFinal(paramStr.toString().getBytes(StandardCharsets.UTF_8));
return Hex.encodeHexString(hash);
}
private static String getUTCTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
return sdf.format(new Date());
}
}
纯净模式
鼠标选中内容,快速反馈问题