package com.sinata.core.util.juhe;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.alipay.api.internal.util.file.IOUtils;
|
import java.io.UnsupportedEncodingException;
|
import java.net.URLEncoder;
|
import java.security.MessageDigest;
|
import java.util.HashMap;
|
import java.util.Map;
|
import lombok.RequiredArgsConstructor;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.http.HttpEntity;
|
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.util.EntityUtils;
|
import org.springframework.stereotype.Component;
|
|
/**三网手机实名认证
|
* @author mitao
|
* @date 2024/4/1
|
*/
|
@Slf4j
|
@Component
|
@RequiredArgsConstructor
|
public class TelecomUtil {
|
private final JuHeProperties juHeProperties;
|
//设置超时时间为5秒
|
public static RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(5000).build();
|
//明文查询地址
|
public static String query_url = "https://v.juhe.cn/telecom/query?key=";
|
//加密查询地址
|
public static String queryEncry_url = "https://v.juhe.cn/telecom/queryEncry?key=";
|
|
public boolean verify(String realname, String idcard, String mobile) {
|
// ----------------------三网手机实名制认证-----------------------------------------------------------------------
|
// int queryType = 1;// 1:普通查询 2:加密查询
|
// String realname = "";// 姓名
|
// String idcard = "";// 身份证
|
//String mobile="" // 手机号
|
// int type = 1;// 是否显示手机运营商 1:显示 0:不显示(默认)
|
//int showid = 1 //是否显示聚合订单账号 1:显示 0:不显示(默认)
|
//int province = 1 //是否显示号码归属地 1:显示 0:不显示(默认)
|
//int detail = 1 //是否买显示匹配详情码 1:显示 0:不显示(默认)
|
// Map<String, Object> params = new HashMap<>();
|
// params.put("realname", realname);
|
// params.put("idcard", idcard);
|
// params.put("mobile", mobile);
|
|
// ----------------------调用三网手机实名认证(加密版)-----------------------------------------------------------------------
|
String key = MD5(juHeProperties.getOpenid()).substring(0, 16);//取前16位作为加密密钥
|
int queryType = 2;// 加密版本
|
realname = SecurityAESTool.encrypt(realname, key);//加密姓名
|
idcard = SecurityAESTool.encrypt(idcard, key);//加密身份证
|
mobile = SecurityAESTool.encrypt(mobile, key);//
|
Map<String, Object> params = new HashMap<>();//组合参数
|
params.put("realname", realname);
|
params.put("idcard", idcard);
|
params.put("mobile", mobile);
|
//请求接口
|
JSONObject result = null;
|
try {
|
result = JSONObject.parseObject(queryResult(params, queryType));
|
} catch (Exception e) {
|
log.error("调用三网手机实名认证失败", e);
|
}
|
//打印结果
|
System.out.println(result);
|
JSONObject innerResult = result.getJSONObject("result");
|
if (result != null && innerResult != null && innerResult.getInteger("res") == 1) {
|
return true;
|
}
|
return false;
|
}
|
|
/**
|
* 请求接口查询数据
|
*
|
* @param params 参数
|
* @param queryType 类型,1明文查询(默认),2加密版
|
* @return 结果
|
* @throws Exception
|
*/
|
|
private String queryResult(Map<String, Object> params, int queryType) throws Exception {
|
|
CloseableHttpClient httpClient = HttpClients.createDefault();
|
CloseableHttpResponse response = null;
|
String result = null;
|
String url = query_url;
|
switch (queryType) {
|
case 2:
|
url = queryEncry_url + juHeProperties.getAppkey();
|
break;
|
}
|
try {
|
url = new StringBuffer(url).append("&").append(urlencode(params)).toString();
|
HttpGet httpget = new HttpGet(url);
|
httpget.setConfig(config);
|
response = httpClient.execute(httpget);
|
HttpEntity resEntity = response.getEntity();
|
if (resEntity != null) {
|
result = IOUtils.toString(resEntity.getContent(), "UTF-8");
|
}
|
EntityUtils.consume(resEntity);
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
response.close();
|
httpClient.close();
|
}
|
return result;
|
}
|
|
|
// 将map型转为请求参数型
|
public static String urlencode(Map<String, ?> data) {
|
StringBuilder sb = new StringBuilder();
|
for (Map.Entry<String, ?> i : data.entrySet()) {
|
try {
|
sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue() + "", "UTF-8")).append("&");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
}
|
String result = sb.toString();
|
result = result.substring(0, result.lastIndexOf("&"));
|
return result;
|
}
|
|
|
/**
|
* md5加密
|
*
|
* @param data
|
* @return 加密结果
|
*/
|
public static String MD5(String data) {
|
StringBuffer md5str = new StringBuffer();
|
byte[] input = data.getBytes();
|
try {
|
// 创建一个提供信息摘要算法的对象,初始化为md5算法对象
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
// 计算后获得字节数组
|
byte[] buff = md.digest(input);
|
// 把数组每一字节换成16进制连成md5字符串
|
int digital;
|
for (int i = 0; i < buff.length; i++) {
|
digital = buff[i];
|
if (digital < 0) {
|
digital += 256;
|
}
|
if (digital < 16) {
|
md5str.append("0");
|
}
|
md5str.append(Integer.toHexString(digital));
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return md5str.toString();
|
}
|
}
|