认证
This commit is contained in:
parent
833689270d
commit
21ac49f83c
@ -23,7 +23,6 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
@ -35,15 +34,10 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.Security;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 验证工具类
|
||||
@ -67,7 +61,7 @@ public class VerifyUtil {
|
||||
/**
|
||||
* OkHttpClient实例
|
||||
*/
|
||||
private OkHttpClient client = null;
|
||||
private final OkHttpClient client;
|
||||
|
||||
@Resource
|
||||
private WechatConfig wechatConfig;
|
||||
@ -95,7 +89,7 @@ public class VerifyUtil {
|
||||
/**
|
||||
* 微信工具类构造函数。
|
||||
*/
|
||||
public VerifyUtil() throws IOException {
|
||||
public VerifyUtil(){
|
||||
client = new OkHttpClient();
|
||||
securityContext = new SecurityContext();
|
||||
//设置加解密上下⽂类调⽤SM2加解密类
|
||||
@ -116,7 +110,7 @@ public class VerifyUtil {
|
||||
params.put("secret", wechatConfig.getAppSecret());
|
||||
params.put("js_code", 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.setKey(realNameVerifyConfig.getApiKey());
|
||||
// 2. 调用第三方实名验证
|
||||
JsonNode result = httpPost("验证用户实名", REALNAME_VERIFY_URL, ebikeRealNameVerifyDto, null);
|
||||
JsonNode result = httpPost(ebikeRealNameVerifyDto);
|
||||
log.info("验证用户实名结果: {}", result);
|
||||
if (result == null) {
|
||||
throw new EbikeException("验证用户实名失败");
|
||||
@ -192,7 +186,7 @@ public class VerifyUtil {
|
||||
if (!"10000".equals(code)) {
|
||||
String message = result.has("message") ? result.get("message").asText() : "未知错误";
|
||||
log.error("验证用户实名失败, code: {}, message: {}", code, message);
|
||||
throw new EbikeException(message);
|
||||
throw new EbikeException(message.replace("(","").replace(")",""));
|
||||
}
|
||||
// 3. 解密并解析结果
|
||||
String data = securityContext.decrypt(result.get("data").asText(), realNameVerifyConfig.getClientPrivateKey());
|
||||
@ -251,65 +245,46 @@ public class VerifyUtil {
|
||||
/**
|
||||
* 发送HTTP GET请求。
|
||||
*
|
||||
* @param func_ 功能描述
|
||||
* @param url 请求URL
|
||||
* @param params 请求参数
|
||||
* @return 响应结果
|
||||
*/
|
||||
private JsonNode httpGet(String func_, String url, Map<String, String> params) {
|
||||
private JsonNode httpGet(Map<String, String> params) {
|
||||
StringJoiner paramJoiner = new StringJoiner("&");
|
||||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||||
paramJoiner.add(entry.getKey() + "=" + entry.getValue());
|
||||
}
|
||||
String requestUrl = url + "?" + paramJoiner.toString();
|
||||
String requestUrl = WECHAT_LOGIN_URL + "?" + paramJoiner;
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(requestUrl)
|
||||
.get()
|
||||
.build();
|
||||
|
||||
return executeAndParseResponse(func_, url, request);
|
||||
return executeAndParseResponse("获取openId", WECHAT_LOGIN_URL, request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送HTTP POST参数请求。
|
||||
*
|
||||
* @param func_ 功能描述
|
||||
* @param url 请求URL
|
||||
* @param dto 请求参数
|
||||
* @param dto 请求参数
|
||||
* @return 响应结果
|
||||
*/
|
||||
private JsonNode httpPost(String func_, String url, EbikeRealNameVerifyDto dto, Map<String, Object> body) {
|
||||
private JsonNode httpPost(EbikeRealNameVerifyDto dto) {
|
||||
StringBuilder queryString = new StringBuilder();
|
||||
appendParam(queryString, "name", dto.getName());
|
||||
appendParam(queryString, "idcard", dto.getIdCard());
|
||||
appendParam(queryString, "timestamp", dto.getTimestamp());
|
||||
appendParam(queryString, "sign", dto.getSign());
|
||||
appendParam(queryString, "key", dto.getKey());
|
||||
|
||||
String requestUrl = url;
|
||||
String requestUrl = REALNAME_VERIFY_URL;
|
||||
if (!queryString.isEmpty()) {
|
||||
requestUrl += "?" + queryString.toString();
|
||||
requestUrl += "?" + queryString;
|
||||
}
|
||||
|
||||
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();
|
||||
return executeAndParseResponse(func_, url, request);
|
||||
return executeAndParseResponse("验证用户实名", REALNAME_VERIFY_URL, request);
|
||||
}
|
||||
|
||||
private JsonNode executeAndParseResponse(String func_, String url, Request request) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user