Files
muscipro/ruoyi-system/src/main/java/com/ruoyi/system/config/UmengConfig.java

114 lines
4.4 KiB
Java
Raw Normal View History

2025-07-14 23:21:19 +08:00
package com.ruoyi.system.config;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.uuid.UUID;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.Header;
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.HttpClients;
2025-07-19 14:20:38 +08:00
import org.apache.http.util.EntityUtils;
2025-07-14 23:21:19 +08:00
import org.springframework.stereotype.Component;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
2025-07-19 14:20:38 +08:00
import java.io.IOException;
2025-07-14 23:21:19 +08:00
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
@Component
public class UmengConfig {
2025-07-19 14:20:38 +08:00
public static CloseableHttpResponse send(String token){
String umAppkey = "5df88f83570df3b8d40012337";
String appKey = "LTAI5tDWWzwqxumUXTFnkQFd";
String appSecret = "KOD3y6OxbHJ23wxAf68NFpUQXCQEPX";
2025-07-14 23:21:19 +08:00
// 下面的url要和阿里云云市场购买的商品对应
String url = "https://verify5.market.alicloudapi.com/api/v1/mobile/info?appkey=" + umAppkey;
HttpPost httpPost = new HttpPost(url);
/**
* body
*/
JSONObject object = new JSONObject();
object.put("token", token);
StringEntity stringEntity = new StringEntity(object.toJSONString(), StandardCharsets.UTF_8);
httpPost.setEntity(stringEntity);
/**
* header
*/
httpPost.setHeader("Content-Type", "application/json; charset=UTF-8");
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("X-Ca-Version", "1");
httpPost.setHeader("X-Ca-Signature-Headers", "X-Ca-Version,X-Ca-Stage,X-Ca-Key,X-Ca-Timestamp");
httpPost.setHeader("X-Ca-Stage", "RELEASE");
httpPost.setHeader("X-Ca-Key", appKey);
httpPost.setHeader("X-Ca-Timestamp", String.valueOf(System.currentTimeMillis()));
httpPost.setHeader("X-Ca-Nonce", UUID.randomUUID().toString());
httpPost.setHeader("Content-MD5", Base64.encodeBase64String(DigestUtils.md5(object.toJSONString())));
/**
* sign
*/
String stringToSign = getSignString(httpPost);
Mac hmacSha256;
try {
hmacSha256 = Mac.getInstance("HmacSHA256");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
byte[] keyBytes = appSecret.getBytes(StandardCharsets.UTF_8);
try {
hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, "HmacSHA256"));
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
}
String sign = new String(Base64.encodeBase64(hmacSha256.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8))));
httpPost.setHeader("X-Ca-Signature", sign);
/**
* execute
*/
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
CloseableHttpResponse response = httpclient.execute(httpPost);
return response;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
2025-07-19 14:20:38 +08:00
public static void main(String[] args) throws IOException {
CloseableHttpResponse sss = send("1234");
String reponseContent = EntityUtils.toString(sss.getEntity());
System.out.println(reponseContent);
System.out.println(sss.getStatusLine().getReasonPhrase());
2025-07-14 23:21:19 +08:00
}
private static String getSignString(HttpPost httpPost) {
Header[] headers = httpPost.getAllHeaders();
Map<String, String> map = new HashMap<>();
for (Header header : headers) {
map.put(header.getName(), header.getValue());
}
return httpPost.getMethod() + "\n" +
map.get("Accept") + "\n" +
map.get("Content-MD5") + "\n" +
map.get("Content-Type") + "\n\n" +
"X-Ca-Key:" + map.get("X-Ca-Key") + "\n" +
"X-Ca-Stage:" + map.get("X-Ca-Stage") + "\n" +
"X-Ca-Timestamp:" + map.get("X-Ca-Timestamp") + "\n" +
"X-Ca-Version:" + map.get("X-Ca-Version") + "\n" +
httpPost.getURI().getPath() + "?" + httpPost.getURI().getQuery();
}
}