提交修改
This commit is contained in:
parent
1c12a86063
commit
2b54bc8222
@ -312,4 +312,11 @@ public class ApiConstants {
|
||||
public static final String tencent = "tencent";
|
||||
|
||||
|
||||
|
||||
/***
|
||||
* 第三方
|
||||
*/
|
||||
public static final String third = "third";
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -5,7 +5,7 @@ package com.heyu.api.data.dao.api;
|
||||
* </p>
|
||||
*
|
||||
* @author quyixiao
|
||||
* @since 2025-04-14
|
||||
* @since 2025-04-16
|
||||
*/
|
||||
import com.heyu.api.data.entity.api.ApiIdCardEntity;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
@ -35,5 +35,9 @@ public interface ApiIdCardDao extends BaseMapper<ApiIdCardEntity> {
|
||||
|
||||
|
||||
@LIMIT
|
||||
ApiIdCardEntity selectApiIdCardByIdCardNameChannel(String idCard, String realName, String channel);
|
||||
ApiIdCardEntity selectApiIdCardByIdCardNameChannelEnableStatus(String idCard, String realName, String channel,Integer enableStatus);
|
||||
|
||||
|
||||
|
||||
int updateApiIdCardUseCountById(Long useCount, Long id);
|
||||
}
|
||||
@ -10,7 +10,7 @@ import java.util.Date;import java.util.Date;
|
||||
/**
|
||||
*账户表
|
||||
* @author quyixiao
|
||||
* @since 2025-04-14
|
||||
* @since 2025-04-16
|
||||
*/
|
||||
|
||||
@Data
|
||||
@ -21,14 +21,16 @@ private static final long serialVersionUID = 1L;
|
||||
public final static String CLASS_NAME ="com.heyu.api.data.entity.api.ApiIdCardEntity:";
|
||||
|
||||
public final static String all = CLASS_NAME + "*";
|
||||
public final static String id_ = CLASS_NAME + "id"; // 主键id
|
||||
public final static String is_delete = CLASS_NAME + "is_delete"; // 是否删除:0 否 1 删除
|
||||
public final static String create_time = CLASS_NAME + "create_time"; // 创建时间
|
||||
public final static String modify_time = CLASS_NAME + "modify_time"; // 修改时间
|
||||
public final static String id_card = CLASS_NAME + "id_card"; // 身份证
|
||||
public final static String real_name = CLASS_NAME + "real_name"; // 真实姓名
|
||||
public final static String api_result = CLASS_NAME + "api_result"; // 结果
|
||||
public final static String channel_ = CLASS_NAME + "channel"; // 渠道
|
||||
public final static String id_ = CLASS_NAME + "id"; // 主键id
|
||||
public final static String is_delete = CLASS_NAME + "is_delete"; // 是否删除:0 否 1 删除
|
||||
public final static String create_time = CLASS_NAME + "create_time"; // 创建时间
|
||||
public final static String modify_time = CLASS_NAME + "modify_time"; // 修改时间
|
||||
public final static String id_card = CLASS_NAME + "id_card"; // 身份证
|
||||
public final static String real_name = CLASS_NAME + "real_name"; // 真实姓名
|
||||
public final static String api_result = CLASS_NAME + "api_result"; // 结果
|
||||
public final static String channel_ = CLASS_NAME + "channel"; // 渠道
|
||||
public final static String use_count = CLASS_NAME + "use_count"; // 使用次数
|
||||
public final static String enable_status = CLASS_NAME + "enable_status"; // 0 不可用,1 可用
|
||||
//主键id
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
@ -46,6 +48,10 @@ private static final long serialVersionUID = 1L;
|
||||
private String apiResult;
|
||||
//渠道
|
||||
private String channel;
|
||||
//使用次数
|
||||
private Long useCount;
|
||||
//0 不可用,1 可用
|
||||
private Integer enableStatus;
|
||||
/**
|
||||
* 主键id
|
||||
* @return
|
||||
@ -166,6 +172,36 @@ private static final long serialVersionUID = 1L;
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用次数
|
||||
* @return
|
||||
*/
|
||||
public Long getUseCount() {
|
||||
return useCount;
|
||||
}
|
||||
/**
|
||||
* 使用次数
|
||||
* @param useCount
|
||||
*/
|
||||
public void setUseCount(Long useCount) {
|
||||
this.useCount = useCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* 0 不可用,1 可用
|
||||
* @return
|
||||
*/
|
||||
public Integer getEnableStatus() {
|
||||
return enableStatus;
|
||||
}
|
||||
/**
|
||||
* 0 不可用,1 可用
|
||||
* @param enableStatus
|
||||
*/
|
||||
public void setEnableStatus(Integer enableStatus) {
|
||||
this.enableStatus = enableStatus;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ApiIdCardEntity{" +
|
||||
@ -177,6 +213,8 @@ private static final long serialVersionUID = 1L;
|
||||
",realName=" + realName +
|
||||
",apiResult=" + apiResult +
|
||||
",channel=" + channel +
|
||||
",useCount=" + useCount +
|
||||
",enableStatus=" + enableStatus +
|
||||
"}";
|
||||
}
|
||||
}
|
||||
@ -5,7 +5,7 @@ package com.heyu.api.data.service.api;
|
||||
* </p>
|
||||
*
|
||||
* @author quyixiao
|
||||
* @since 2025-04-14
|
||||
* @since 2025-04-16
|
||||
*/
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.heyu.api.data.entity.api.ApiIdCardEntity;
|
||||
|
||||
@ -5,7 +5,7 @@ package com.heyu.api.data.service.impl.api;
|
||||
* </p>
|
||||
*
|
||||
* @author quyixiao
|
||||
* @since 2025-04-14
|
||||
* @since 2025-04-16
|
||||
*/
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
|
||||
@ -23,12 +23,19 @@ public abstract class TencentBaseHandle<P, RR> {
|
||||
return ApiR.error(check);
|
||||
}
|
||||
RR r = run(p);
|
||||
|
||||
if(r == null){
|
||||
log.error("TencentBaseHandle run failed : {}", JSON.toJSONString(p));
|
||||
return ApiR.error("服务器异常请稍后重试");
|
||||
}
|
||||
|
||||
log.info("TencentBaseHandle result : {}", JSON.toJSONString(r));
|
||||
return ApiR.setData(r).setReceiveMoney(true);
|
||||
} catch (Exception e) {
|
||||
log.error("请求腾讯异常", e);
|
||||
}
|
||||
return ApiR.error("未知异常");
|
||||
log.error("TencentBaseHandle run end failed : {}", JSON.toJSONString(p));
|
||||
return ApiR.error("服务器异常请稍后重试");
|
||||
}
|
||||
|
||||
public abstract RR run(P ap) throws Exception;
|
||||
|
||||
@ -62,10 +62,10 @@ public class TIdCardOCRVerificationHandle extends TencentBaseHandle<TTIdCardOCRV
|
||||
public IdCardOCRVerificationResponse run(TTIdCardOCRVerificationRequest ap) throws Exception {
|
||||
IdCardOCRVerificationResponse resp = null;
|
||||
if (StringUtils.isNotBlank(ap.getIdCard()) && StringUtils.isNotBlank(ap.getName())) {
|
||||
|
||||
ApiIdCardEntity apiIdCardEntity = apiIdCardDao.selectApiIdCardByIdCardNameChannel(ap.getIdCard(), ap.getName(), ApiConstants.tencent);
|
||||
ApiIdCardEntity apiIdCardEntity = apiIdCardDao.selectApiIdCardByIdCardNameChannelEnableStatus(ap.getIdCard(), ap.getName(), ApiConstants.tencent,1);
|
||||
if (apiIdCardEntity != null) {
|
||||
resp = JSONObject.parseObject(apiIdCardEntity.getApiResult(), IdCardOCRVerificationResponse.class);
|
||||
apiIdCardDao.updateApiIdCardUseCountById(apiIdCardEntity.getUseCount(), apiIdCardEntity.getId());
|
||||
return resp;
|
||||
}
|
||||
}
|
||||
@ -76,15 +76,49 @@ public class TIdCardOCRVerificationHandle extends TencentBaseHandle<TTIdCardOCRV
|
||||
req.setName(ap.getName());
|
||||
req.setImageUrl(ap.getImageUrl());
|
||||
req.setImageBase64(ap.getImageBase64());
|
||||
|
||||
// 返回的resp是一个IdCardOCRVerificationResponse的实例,与请求对象对应
|
||||
resp = client.IdCardOCRVerification(req);
|
||||
|
||||
if(resp ==null){
|
||||
return null;
|
||||
}
|
||||
|
||||
int enable = 1;
|
||||
String result = resp.getResult();
|
||||
/***
|
||||
* 认证结果码,收费情况如下。
|
||||
* - 收费结果码:
|
||||
* 0: 姓名和身份证号一致。
|
||||
* -1: 姓名和身份证号不一致。
|
||||
* - 不收费结果码:
|
||||
* -2: 非法身份证号(长度、校验位等不正确)。
|
||||
* -3: 非法姓名(长度、格式等不正确)。
|
||||
*
|
||||
*
|
||||
* -4: 证件库服务异常。
|
||||
* -5: 证件库中无此身份证记录。
|
||||
* -6: 权威比对系统升级中,请稍后再试。
|
||||
* -7: 认证次数超过当日限制。
|
||||
*
|
||||
* 示例值:0
|
||||
*/
|
||||
if (
|
||||
"-4".equals(result)
|
||||
|| "-5".equals(result)
|
||||
|| "-6".equals(result)
|
||||
|| "-7".equals(result)
|
||||
) {
|
||||
|
||||
enable = 0 ;
|
||||
}
|
||||
|
||||
ApiIdCardEntity apiIdCardEntity = new ApiIdCardEntity();
|
||||
apiIdCardEntity.setIdCard(ap.getIdCard());
|
||||
apiIdCardEntity.setRealName(ap.getName());
|
||||
apiIdCardEntity.setChannel(ApiConstants.tencent);
|
||||
apiIdCardEntity.setApiResult(JSON.toJSONString(resp));
|
||||
apiIdCardEntity.setEnableStatus(enable);
|
||||
apiIdCardEntity.setUseCount(1L);
|
||||
apiIdCardDao.insertApiIdCard(apiIdCardEntity);
|
||||
|
||||
return resp;
|
||||
|
||||
@ -0,0 +1,112 @@
|
||||
package com.heyu.api.tencent.handle;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.heyu.api.data.constants.ApiConstants;
|
||||
import com.heyu.api.data.dao.api.ApiIdCardDao;
|
||||
import com.heyu.api.data.entity.api.ApiIdCardEntity;
|
||||
import com.heyu.api.data.utils.StringUtils;
|
||||
import com.heyu.api.tencent.TencentBaseHandle;
|
||||
import com.heyu.api.tencent.request.TTIdCardOCRVerificationRequest;
|
||||
import com.heyu.api.tencent.resp.ThirdIdCardResp;
|
||||
import com.heyu.api.utils.ThirdUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/***
|
||||
*
|
||||
*
|
||||
* https://market.cloud.tencent.com/products/17684
|
||||
*
|
||||
*
|
||||
*身份证识别及信息核验
|
||||
*
|
||||
*
|
||||
* 【身份证实名认证-身份证二要素实名认证】传入姓名、身份证号,校验是否一致,同时解析,省份、城市、性别。身份证实名认证接口主要应用在物流、金融
|
||||
* 、游戏防沉溺等政策要求注册用户身份证实名认证的场景中,产品别名也叫【身份证实名认证、身份证实名认证接口、身份证实名认证api】
|
||||
*
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class ThirdTIdCardVerificationHandle extends TencentBaseHandle<TTIdCardOCRVerificationRequest, ThirdIdCardResp> {
|
||||
|
||||
|
||||
|
||||
@Autowired
|
||||
private ApiIdCardDao apiIdCardDao;
|
||||
|
||||
@Override
|
||||
public String check(TTIdCardOCRVerificationRequest ttIdCardOCRVerificationRequest) {
|
||||
if (StringUtils.isNotBlank(ttIdCardOCRVerificationRequest.getIdCard())
|
||||
&& StringUtils.isBlank(ttIdCardOCRVerificationRequest.getName())) {
|
||||
return "姓名不能为空";
|
||||
}
|
||||
|
||||
|
||||
if (StringUtils.isBlank(ttIdCardOCRVerificationRequest.getIdCard())
|
||||
&& StringUtils.isNotBlank(ttIdCardOCRVerificationRequest.getName())) {
|
||||
return "身份证号不能为空";
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(ttIdCardOCRVerificationRequest.getIdCard())
|
||||
&& StringUtils.isBlank(ttIdCardOCRVerificationRequest.getName())
|
||||
&& StringUtils.isBlank(ttIdCardOCRVerificationRequest.getImageBase64())
|
||||
&& StringUtils.isBlank(ttIdCardOCRVerificationRequest.getImageUrl())
|
||||
) {
|
||||
return "姓名身份证号以及图片不能同时为空";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* 注意:
|
||||
* 1.解析:先判断error_code为0仅代表端口通讯成功再看业务逻辑码isok值当isok为true为匹配 false为不匹配。
|
||||
* 2.出现'库无'时 "error_code":206501,有以下几种原因
|
||||
*
|
||||
*
|
||||
* (1)现役军人、武警官兵、特殊部门人员及特殊级别官员;
|
||||
* (2)退役不到2年的军人和士兵(根据军衔、兵种不同,时间会有所不同,一般为2年);
|
||||
* (3)户口迁出,且没有在新的迁入地迁入 eg:刚上大学或刚毕业的大学生;
|
||||
* (4)户口迁入新迁入地,当地公安系统未将迁移信息上报到公安部(上报时间地域不同而有所差异);
|
||||
* (5)更改姓名,当地公安系统未将更改信息上报到公安部(上报时间因地域不同而有所差异);
|
||||
* (6)移民;
|
||||
* (7)未更换二代身份证;
|
||||
* (8)死亡。
|
||||
* (9)身份证号确实不存在
|
||||
*/
|
||||
@Override
|
||||
public ThirdIdCardResp run(TTIdCardOCRVerificationRequest ap) throws Exception {
|
||||
ThirdIdCardResp resp = null;
|
||||
if (StringUtils.isNotBlank(ap.getIdCard()) && StringUtils.isNotBlank(ap.getName())) {
|
||||
ApiIdCardEntity apiIdCardEntity = apiIdCardDao.selectApiIdCardByIdCardNameChannelEnableStatus(ap.getIdCard(), ap.getName(), ApiConstants.third, 1);
|
||||
if (apiIdCardEntity != null) {
|
||||
resp = JSONObject.parseObject(apiIdCardEntity.getApiResult(), ThirdIdCardResp.class);
|
||||
apiIdCardDao.updateApiIdCardUseCountById(apiIdCardEntity.getUseCount(), apiIdCardEntity.getId());
|
||||
return resp;
|
||||
}
|
||||
}
|
||||
ThirdIdCardResp thirdIdCardResp = ThirdUtils.getIdCard(ap.getIdCard(), ap.getName());
|
||||
if(thirdIdCardResp ==null){
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
int enable = 1 ;
|
||||
if(!"0".equals(thirdIdCardResp.getErrorCode()+"")){
|
||||
enable = 0 ;
|
||||
}
|
||||
|
||||
ApiIdCardEntity apiIdCardEntity = new ApiIdCardEntity();
|
||||
apiIdCardEntity.setIdCard(ap.getIdCard());
|
||||
apiIdCardEntity.setRealName(ap.getName());
|
||||
apiIdCardEntity.setChannel(ApiConstants.tencent);
|
||||
apiIdCardEntity.setApiResult(JSON.toJSONString(resp));
|
||||
apiIdCardEntity.setEnableStatus(enable);
|
||||
apiIdCardEntity.setUseCount(1L);
|
||||
apiIdCardDao.insertApiIdCard(apiIdCardEntity);
|
||||
return resp;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,71 @@
|
||||
package com.heyu.api.tencent.resp;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.heyu.api.data.dto.BaseResp;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/***
|
||||
* {
|
||||
* "error_code": 0,
|
||||
* "reason": "成功",
|
||||
* "result": {
|
||||
* "realname": "胡**",
|
||||
* "idcard": "360311***********7",
|
||||
* "isok": true,
|
||||
* "IdCardInfor": {
|
||||
* "province": "江西省",
|
||||
* "city": "萍乡市",
|
||||
* "district": "上栗区",
|
||||
* "area": "江西省萍乡市上栗区",
|
||||
* "sex": "女",
|
||||
* "birthday": "1993-3-8"
|
||||
* } * },
|
||||
* "sn": "041619150790665135760208371"
|
||||
* }
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
@Data
|
||||
public class ThirdIdCardResp extends BaseResp {
|
||||
|
||||
|
||||
@JsonProperty("error_code")
|
||||
private Integer errorCode;
|
||||
@JsonProperty("reason")
|
||||
private String reason;
|
||||
@JsonProperty("result")
|
||||
private ResultDTO result;
|
||||
@JsonProperty("sn")
|
||||
private String sn;
|
||||
|
||||
@NoArgsConstructor
|
||||
@Data
|
||||
public static class ResultDTO {
|
||||
@JsonProperty("realname")
|
||||
private String realname;
|
||||
@JsonProperty("idcard")
|
||||
private String idcard;
|
||||
@JsonProperty("isok")
|
||||
private Boolean isok;
|
||||
@JsonProperty("IdCardInfor")
|
||||
private IdCardInforDTO idCardInfor;
|
||||
|
||||
@NoArgsConstructor
|
||||
@Data
|
||||
public static class IdCardInforDTO {
|
||||
@JsonProperty("province")
|
||||
private String province;
|
||||
@JsonProperty("city")
|
||||
private String city;
|
||||
@JsonProperty("district")
|
||||
private String district;
|
||||
@JsonProperty("area")
|
||||
private String area;
|
||||
@JsonProperty("sex")
|
||||
private String sex;
|
||||
@JsonProperty("birthday")
|
||||
private String birthday;
|
||||
}
|
||||
}
|
||||
}
|
||||
158
api-third/src/main/java/com/heyu/api/utils/ThirdUtils.java
Normal file
158
api-third/src/main/java/com/heyu/api/utils/ThirdUtils.java
Normal file
@ -0,0 +1,158 @@
|
||||
package com.heyu.api.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.heyu.api.tencent.resp.ThirdIdCardResp;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.Key;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
public class ThirdUtils {
|
||||
|
||||
public static String calcAuthorization(String secretId, String secretKey, String datetime)
|
||||
throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
|
||||
String signStr = "x-date: " + datetime;
|
||||
Mac mac = Mac.getInstance("HmacSHA1");
|
||||
Key sKey = new SecretKeySpec(secretKey.getBytes("UTF-8"), mac.getAlgorithm());
|
||||
mac.init(sKey);
|
||||
byte[] hash = mac.doFinal(signStr.getBytes("UTF-8"));
|
||||
//String sig = new Base64Encoder().encode(hash);
|
||||
|
||||
|
||||
Base64.Encoder encoder = Base64.getMimeEncoder();
|
||||
String sig= encoder.encodeToString(hash);
|
||||
|
||||
|
||||
|
||||
String auth = "{\"id\":\"" + secretId + "\", \"x-date\":\"" + datetime + "\", \"signature\":\"" + sig + "\"}";
|
||||
return auth;
|
||||
}
|
||||
|
||||
public static String urlencode(Map<?, ?> map) throws UnsupportedEncodingException {
|
||||
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(), "UTF-8"),
|
||||
URLEncoder.encode(entry.getValue().toString(), "UTF-8")
|
||||
));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static ThirdIdCardResp getIdCard(String idcardNumber , String realName){
|
||||
BufferedReader in = null;
|
||||
try {
|
||||
//云市场分配的密钥Id
|
||||
String secretId = "32GyIZXijjQqtaTx";
|
||||
//云市场分配的密钥Key
|
||||
String secretKey = "8drHlKmmphKWNxEidbRDMbV9zA3WX8Cn";
|
||||
|
||||
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 auth = ThirdUtils.calcAuthorization(secretId, secretKey, datetime);
|
||||
// 请求方法
|
||||
String method = "POST";
|
||||
// 请求头
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
Map<String, String> headers = new HashMap<String, String>();
|
||||
headers.put("request-id", uuid);
|
||||
headers.put("Authorization", auth);
|
||||
|
||||
// 查询参数
|
||||
Map<String, String> queryParams = new HashMap<String, String>();
|
||||
|
||||
// body参数
|
||||
Map<String, String> bodyParams = new HashMap<String, String>();
|
||||
//bodyParams.put("cardNo","430529199209255030");
|
||||
//bodyParams.put("realName","瞿贻晓");
|
||||
|
||||
//bodyParams.put("cardNo","360428198505100418");
|
||||
//bodyParams.put("realName","吴林");
|
||||
|
||||
|
||||
bodyParams.put("cardNo",idcardNumber);
|
||||
bodyParams.put("realName",realName);
|
||||
String bodyParamStr = ThirdUtils.urlencode(bodyParams);
|
||||
|
||||
// url参数拼接
|
||||
String url = "https://ap-beijing.cloudmarket-apigw.com/service-18c38npd/idcard/VerifyIdcardv2";
|
||||
if (!queryParams.isEmpty()) {
|
||||
url += "?" + ThirdUtils.urlencode(queryParams);
|
||||
}
|
||||
|
||||
URL realUrl = new URL(url);
|
||||
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
|
||||
conn.setConnectTimeout(5000);
|
||||
conn.setReadTimeout(5000);
|
||||
conn.setRequestMethod(method);
|
||||
|
||||
// request headers
|
||||
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
||||
conn.setRequestProperty(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
// request body
|
||||
Map<String, Boolean> methods = new HashMap<>();
|
||||
methods.put("POST", true);
|
||||
methods.put("PUT", true);
|
||||
methods.put("PATCH", true);
|
||||
Boolean hasBody = methods.get(method);
|
||||
if (hasBody != null) {
|
||||
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
|
||||
conn.setDoOutput(true);
|
||||
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
|
||||
out.writeBytes(bodyParamStr);
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
|
||||
// 定义 BufferedReader输入流来读取URL的响应
|
||||
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||
String line;
|
||||
String result = "";
|
||||
while ((line = in.readLine()) != null) {
|
||||
result += line;
|
||||
}
|
||||
|
||||
|
||||
log.info("ThirdIdCard get idcardNumber:{},realName:{},result:{}", idcardNumber,realName, result);
|
||||
|
||||
ThirdIdCardResp thirdIdCardResp = JSONObject.parseObject(result, ThirdIdCardResp.class);
|
||||
return thirdIdCardResp;
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
} catch (Exception e2) {
|
||||
e2.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
||||
@ -42,6 +42,7 @@ public class IdCardOCRVerificationController {
|
||||
@Autowired
|
||||
private TIdCardOCRVerificationHandle tIdCardOCRVerificationHandle;
|
||||
|
||||
|
||||
// http://localhost:8888/idcard/verification?idCardNumber=430529199209255030&realName=瞿贻晓
|
||||
// https://api.1024api.com/api-interface/bank/card/2/verify?bankCardNumber=6214855713516769&realName=瞿贻晓
|
||||
@EbAuthentication(tencent = ApiConstants.TENCENT_AUTH)
|
||||
@ -55,6 +56,20 @@ public class IdCardOCRVerificationController {
|
||||
if (apiR.isSuccess()) {
|
||||
IdCardOCRVerificationResponse idCardOCRVerificationResponse = apiR.getData();
|
||||
if (idCardOCRVerificationResponse != null) {
|
||||
/***
|
||||
* 认证结果码,收费情况如下。
|
||||
* - 收费结果码:
|
||||
* 0: 姓名和身份证号一致。
|
||||
* -1: 姓名和身份证号不一致。
|
||||
* - 不收费结果码:
|
||||
* -2: 非法身份证号(长度、校验位等不正确)。
|
||||
* -3: 非法姓名(长度、格式等不正确)。
|
||||
* -4: 证件库服务异常。
|
||||
* -5: 证件库中无此身份证记录。
|
||||
* -6: 权威比对系统升级中,请稍后再试。
|
||||
* -7: 认证次数超过当日限制。
|
||||
* 示例值:0
|
||||
*/
|
||||
if ("0".equals(idCardOCRVerificationResponse.getResult())) {
|
||||
idCardOCRVerificationResp.setCode("1"); // 姓名和身份证号一致。
|
||||
|
||||
|
||||
@ -1,147 +1,23 @@
|
||||
package com.api.test;
|
||||
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.heyu.api.tencent.resp.ThirdIdCardResp;
|
||||
import com.heyu.api.utils.ThirdUtils;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.Key;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
class Demo {
|
||||
public static String calcAuthorization(String secretId, String secretKey, String datetime)
|
||||
throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
|
||||
String signStr = "x-date: " + datetime;
|
||||
Mac mac = Mac.getInstance("HmacSHA1");
|
||||
Key sKey = new SecretKeySpec(secretKey.getBytes("UTF-8"), mac.getAlgorithm());
|
||||
mac.init(sKey);
|
||||
byte[] hash = mac.doFinal(signStr.getBytes("UTF-8"));
|
||||
//String sig = new Base64Encoder().encode(hash);
|
||||
|
||||
|
||||
Base64.Encoder encoder = Base64.getMimeEncoder();
|
||||
String sig= encoder.encodeToString(hash);
|
||||
|
||||
|
||||
|
||||
String auth = "{\"id\":\"" + secretId + "\", \"x-date\":\"" + datetime + "\", \"signature\":\"" + sig + "\"}";
|
||||
return auth;
|
||||
}
|
||||
|
||||
public static String urlencode(Map<?, ?> map) throws UnsupportedEncodingException {
|
||||
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(), "UTF-8"),
|
||||
URLEncoder.encode(entry.getValue().toString(), "UTF-8")
|
||||
));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {
|
||||
//云市场分配的密钥Id
|
||||
String secretId = "32GyIZXijjQqtaTx";
|
||||
//云市场分配的密钥Key
|
||||
String secretKey = "8drHlKmmphKWNxEidbRDMbV9zA3WX8Cn";
|
||||
|
||||
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 auth = calcAuthorization(secretId, secretKey, datetime);
|
||||
// 请求方法
|
||||
String method = "POST";
|
||||
// 请求头
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
Map<String, String> headers = new HashMap<String, String>();
|
||||
headers.put("request-id", uuid);
|
||||
headers.put("Authorization", auth);
|
||||
|
||||
// 查询参数
|
||||
Map<String, String> queryParams = new HashMap<String, String>();
|
||||
|
||||
// body参数
|
||||
Map<String, String> bodyParams = new HashMap<String, String>();
|
||||
//bodyParams.put("cardNo","430529199209255030");
|
||||
//bodyParams.put("realName","瞿贻晓");
|
||||
|
||||
//bodyParams.put("cardNo","360428198505100418");
|
||||
//bodyParams.put("realName","吴林");
|
||||
|
||||
|
||||
bodyParams.put("cardNo","360311199303080027");
|
||||
bodyParams.put("realName","胡凯敏");
|
||||
String bodyParamStr = urlencode(bodyParams);
|
||||
ThirdIdCardResp thirdIdCardResp = ThirdUtils.getIdCard("4305291992092","瞿晓");
|
||||
|
||||
// url参数拼接
|
||||
String url = "https://ap-beijing.cloudmarket-apigw.com/service-18c38npd/idcard/VerifyIdcardv2";
|
||||
if (!queryParams.isEmpty()) {
|
||||
url += "?" + urlencode(queryParams);
|
||||
}
|
||||
|
||||
BufferedReader in = null;
|
||||
try {
|
||||
URL realUrl = new URL(url);
|
||||
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
|
||||
conn.setConnectTimeout(5000);
|
||||
conn.setReadTimeout(5000);
|
||||
conn.setRequestMethod(method);
|
||||
|
||||
// request headers
|
||||
for (Map.Entry<String, String> entry : headers.entrySet()) {
|
||||
conn.setRequestProperty(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
// request body
|
||||
Map<String, Boolean> methods = new HashMap<>();
|
||||
methods.put("POST", true);
|
||||
methods.put("PUT", true);
|
||||
methods.put("PATCH", true);
|
||||
Boolean hasBody = methods.get(method);
|
||||
if (hasBody != null) {
|
||||
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
|
||||
conn.setDoOutput(true);
|
||||
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
|
||||
out.writeBytes(bodyParamStr);
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
|
||||
// 定义 BufferedReader输入流来读取URL的响应
|
||||
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||
String line;
|
||||
String result = "";
|
||||
while ((line = in.readLine()) != null) {
|
||||
result += line;
|
||||
}
|
||||
|
||||
System.out.println(result);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e);
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
} catch (Exception e2) {
|
||||
e2.printStackTrace();
|
||||
}
|
||||
}
|
||||
System.out.println(JSON.toJSONString(thirdIdCardResp));
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user