|
|
@@ -0,0 +1,80 @@
|
|
|
+package net.mingsoft.tf.wx.util;
|
|
|
+
|
|
|
+import cn.hutool.core.codec.Base64Encoder;
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.logging.log4j.Level;
|
|
|
+import org.apache.logging.log4j.LogManager;
|
|
|
+import org.apache.logging.log4j.core.LoggerContext;
|
|
|
+
|
|
|
+import javax.crypto.Mac;
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.security.InvalidKeyException;
|
|
|
+import java.security.Key;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 实名认证
|
|
|
+ *
|
|
|
+ * @author hx
|
|
|
+ * @date 2026-01-21
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public class RealNameCheck {
|
|
|
+ public static String calcAuthorization(String secretId, String secretKey) throws NoSuchAlgorithmException, InvalidKeyException {
|
|
|
+ Calendar cd = Calendar.getInstance();
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
|
|
|
+ sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
|
|
|
+ String datetime = sdf.format(cd.getTime());
|
|
|
+ String signStr = "x-date: " + datetime;
|
|
|
+ Mac mac = Mac.getInstance("HmacSHA1");
|
|
|
+ Key sKey = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), mac.getAlgorithm());
|
|
|
+ mac.init(sKey);
|
|
|
+ String sig = Base64Encoder.encode(mac.doFinal(signStr.getBytes(StandardCharsets.UTF_8)));
|
|
|
+ return "{\"id\":\"" + secretId + "\", \"x-date\":\"" + datetime + "\", \"signature\":\"" + sig + "\"}";
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String urlencode(Map<?, ?> map) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (Map.Entry<?, ?> entry : map.entrySet()) {
|
|
|
+ if (sb.length() > 0) {
|
|
|
+ sb.append("&");
|
|
|
+ }
|
|
|
+ sb.append(String.format("%s=%s",
|
|
|
+ URLEncoder.encode(entry.getKey().toString(), StandardCharsets.UTF_8),
|
|
|
+ URLEncoder.encode(entry.getValue().toString(), StandardCharsets.UTF_8)
|
|
|
+ ));
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Optional<RealNameResults> check(String idcard, String name, String secretId, String secretKey) {
|
|
|
+ try (HttpResponse response = HttpUtil.createPost("https://ap-shanghai.cloudmarket-apigw.com/service-isr6xhvr/id_name/check")
|
|
|
+ .header("request-id", UUID.randomUUID().toString())
|
|
|
+ .header("Authorization", calcAuthorization(secretId, secretKey))
|
|
|
+ .form("idcard", idcard)
|
|
|
+ .form("name", name)
|
|
|
+ .execute()) {
|
|
|
+ return Optional.ofNullable(response.body())
|
|
|
+ .map(JSONObject::new)
|
|
|
+ .filter(json -> {
|
|
|
+ if (json.getInt("code") != 200) {
|
|
|
+ log.warn("身份验证失败 name:{} idcard:{} msg:{}", name, idcard, json.getStr("msg"));
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ })
|
|
|
+ .map(json -> json.getJSONObject("data"))
|
|
|
+ .map(json -> json.toBean(RealNameResults.class));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("身份验证异常 name:{} idcard:{}", name, idcard, e);
|
|
|
+ return Optional.empty();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|