This commit is contained in:
attiya 2026-01-09 10:40:13 +08:00
parent 833689270d
commit 21ac49f83c

View File

@ -23,7 +23,6 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import okhttp3.MediaType;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.RequestBody; import okhttp3.RequestBody;
@ -35,15 +34,10 @@ import org.springframework.transaction.annotation.Transactional;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.Security; import java.security.Security;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Base64; import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;
/** /**
* 验证工具类 * 验证工具类
@ -67,7 +61,7 @@ public class VerifyUtil {
/** /**
* OkHttpClient实例 * OkHttpClient实例
*/ */
private OkHttpClient client = null; private final OkHttpClient client;
@Resource @Resource
private WechatConfig wechatConfig; private WechatConfig wechatConfig;
@ -95,7 +89,7 @@ public class VerifyUtil {
/** /**
* 微信工具类构造函数 * 微信工具类构造函数
*/ */
public VerifyUtil() throws IOException { public VerifyUtil(){
client = new OkHttpClient(); client = new OkHttpClient();
securityContext = new SecurityContext(); securityContext = new SecurityContext();
//设置加解密上下类调SM2加解密类 //设置加解密上下类调SM2加解密类
@ -116,7 +110,7 @@ public class VerifyUtil {
params.put("secret", wechatConfig.getAppSecret()); params.put("secret", wechatConfig.getAppSecret());
params.put("js_code", code); params.put("js_code", code);
params.put("grant_type", "authorization_code"); params.put("grant_type", "authorization_code");
return httpGet("获取openId", WECHAT_LOGIN_URL, params); return httpGet(params);
} }
/** /**
@ -183,7 +177,7 @@ public class VerifyUtil {
ebikeRealNameVerifyDto.setSign(sign); ebikeRealNameVerifyDto.setSign(sign);
ebikeRealNameVerifyDto.setKey(realNameVerifyConfig.getApiKey()); ebikeRealNameVerifyDto.setKey(realNameVerifyConfig.getApiKey());
// 2. 调用第三方实名验证 // 2. 调用第三方实名验证
JsonNode result = httpPost("验证用户实名", REALNAME_VERIFY_URL, ebikeRealNameVerifyDto, null); JsonNode result = httpPost(ebikeRealNameVerifyDto);
log.info("验证用户实名结果: {}", result); log.info("验证用户实名结果: {}", result);
if (result == null) { if (result == null) {
throw new EbikeException("验证用户实名失败"); throw new EbikeException("验证用户实名失败");
@ -192,7 +186,7 @@ public class VerifyUtil {
if (!"10000".equals(code)) { if (!"10000".equals(code)) {
String message = result.has("message") ? result.get("message").asText() : "未知错误"; String message = result.has("message") ? result.get("message").asText() : "未知错误";
log.error("验证用户实名失败, code: {}, message: {}", code, message); log.error("验证用户实名失败, code: {}, message: {}", code, message);
throw new EbikeException(message); throw new EbikeException(message.replace("(","").replace(")",""));
} }
// 3. 解密并解析结果 // 3. 解密并解析结果
String data = securityContext.decrypt(result.get("data").asText(), realNameVerifyConfig.getClientPrivateKey()); String data = securityContext.decrypt(result.get("data").asText(), realNameVerifyConfig.getClientPrivateKey());
@ -251,65 +245,46 @@ public class VerifyUtil {
/** /**
* 发送HTTP GET请求 * 发送HTTP GET请求
* *
* @param func_ 功能描述
* @param url 请求URL
* @param params 请求参数 * @param params 请求参数
* @return 响应结果 * @return 响应结果
*/ */
private JsonNode httpGet(String func_, String url, Map<String, String> params) { private JsonNode httpGet(Map<String, String> params) {
StringJoiner paramJoiner = new StringJoiner("&"); StringJoiner paramJoiner = new StringJoiner("&");
for (Map.Entry<String, String> entry : params.entrySet()) { for (Map.Entry<String, String> entry : params.entrySet()) {
paramJoiner.add(entry.getKey() + "=" + entry.getValue()); paramJoiner.add(entry.getKey() + "=" + entry.getValue());
} }
String requestUrl = url + "?" + paramJoiner.toString(); String requestUrl = WECHAT_LOGIN_URL + "?" + paramJoiner;
Request request = new Request.Builder() Request request = new Request.Builder()
.url(requestUrl) .url(requestUrl)
.get() .get()
.build(); .build();
return executeAndParseResponse(func_, url, request); return executeAndParseResponse("获取openId", WECHAT_LOGIN_URL, request);
} }
/** /**
* 发送HTTP POST参数请求 * 发送HTTP POST参数请求
* *
* @param func_ 功能描述
* @param url 请求URL
* @param dto 请求参数 * @param dto 请求参数
* @return 响应结果 * @return 响应结果
*/ */
private JsonNode httpPost(String func_, String url, EbikeRealNameVerifyDto dto, Map<String, Object> body) { private JsonNode httpPost(EbikeRealNameVerifyDto dto) {
StringBuilder queryString = new StringBuilder(); StringBuilder queryString = new StringBuilder();
appendParam(queryString, "name", dto.getName()); appendParam(queryString, "name", dto.getName());
appendParam(queryString, "idcard", dto.getIdCard()); appendParam(queryString, "idcard", dto.getIdCard());
appendParam(queryString, "timestamp", dto.getTimestamp()); appendParam(queryString, "timestamp", dto.getTimestamp());
appendParam(queryString, "sign", dto.getSign()); appendParam(queryString, "sign", dto.getSign());
appendParam(queryString, "key", dto.getKey()); appendParam(queryString, "key", dto.getKey());
String requestUrl = REALNAME_VERIFY_URL;
String requestUrl = url;
if (!queryString.isEmpty()) { if (!queryString.isEmpty()) {
requestUrl += "?" + queryString.toString(); requestUrl += "?" + queryString;
} }
Request.Builder builder = new Request.Builder().url(requestUrl); Request.Builder builder = new Request.Builder().url(requestUrl);
if (body != null) {
MediaType typeJson = MediaType.parse("application/json; charset=utf-8");
try {
String jsonBody = objectMapper.writeValueAsString(body);
RequestBody requestBody = RequestBody.create(jsonBody, typeJson);
builder.post(requestBody);
} catch (JsonProcessingException e) {
log.error("{}, 请求体序列化失败", func_, e);
return null;
}
} else {
builder.post(RequestBody.create(new byte[0])); builder.post(RequestBody.create(new byte[0]));
}
Request request = builder.build(); Request request = builder.build();
return executeAndParseResponse(func_, url, request); return executeAndParseResponse("验证用户实名", REALNAME_VERIFY_URL, request);
} }
private JsonNode executeAndParseResponse(String func_, String url, Request request) { private JsonNode executeAndParseResponse(String func_, String url, Request request) {