登录
原创

基于聚合数据的身份证实名认证API接口调用示例-Java版

专栏聚合数据JUHE.CN
发布于 2020-10-14 阅读 10868
  • Java
  • API
原创

一、申请接口

通过https://www.juhe.cn/docs/api/id/103自助申请开通接口,获取API请求KEY

二、请求参数

名称 是否必填 说明
idcard 身份证号码
realname 姓名
orderid 传1时返回单号,默认不返回单号(加密版必返回单号)
key 在个人中心->我的数据,接口名称上方查看

三、Java示例代码

package com.jefferson.utils.interfaceDemo.juheDemo;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
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;
/**

 * @author jefferson
 */

public class idcard_103 {

    //设置超时时间为5秒
	public static RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(5000).build();
	// 配置您申请的KEY,在个人中心->我的数据,接口名称上方查看
	public static final String APPKEY = "";
       //明文查询地址
	public static String query_url = "http://op.juhe.cn/idcard/query?key=" + APPKEY;
       //加密查询地址
	public static String queryEncry_url = "http://op.juhe.cn/idcard/queryEncry?key=" + APPKEY;
   
      //主方法
	public static void main(String[] args) throws Exception {

		// ----------------------身份证实名查询-----------------------------------------------------------------------
		// String realname = "";// 姓名
		// String idcard = "";// 身份证
		// int type = 1;// 普通版,不需要加密
		// Map<String, Object> params = new HashMap<>();
		// params.put("realname", realname);
		// params.put("idcard", idcard);

		// ----------------------身份证实名查询(加密版)-----------------------------------------------------------------------
		String realname = "张三";// 姓名
		String idcard = "32072119970602561X";// 身份证
		String openid = "";// 个人中心查询
		String key = MD5(openid).substring(0, 16);//取前16位作为加密密钥
		int type = 2;// 加密版本
		realname = SecurityAESTool.encrypt(realname, key);//加密姓名
		idcard = SecurityAESTool.encrypt(idcard, key);//加密身份证
		Map<String, Object> params = new HashMap<>();//组合参数
		params.put("realname", realname);
		params.put("idcard", idcard);
                //请求接口
		String result = queryResult(params, type);
                //打印结果
		System.out.println(result);

	}

	/**

	 * 请求接口查询数据

	 * @param params 参数
	 * @param type 类型,1明文查询(默认),2加密版
	 * @return 结果
	 * @throws Exception
	 */

	public static String queryResult(Map<String, Object> params, int type) throws Exception {

		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String result = null;
		String url = query_url;
		switch (type) {
		      case 2:
			url = queryEncry_url;
			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();
	}
}

AES工具类,加密解密

package com.jefferson.utils.interfaceDemo.juheDemo;

import org.apache.commons.codec.binary.Base64;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class SecurityAESTool {

	/**
	 * AES加密
	 * 
	 * @param str
	 *            明文
	 * @param key
	 *            秘钥
	 * @return
	 */
	public static String encrypt(String str, String key) {
		byte[] crypted = null;
		try {
			SecretKeySpec skey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, skey);
			String enStr = str;
			crypted = cipher.doFinal(enStr.getBytes("UTF-8"));
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		String body = new String(Base64.encodeBase64(crypted));
		return body;
	}

	/**
	 * AES解密
	 * 
	 * @param input
	 * @param key
	 * @return
	 */
	public static String decrypt(String input, String key) {
		byte[] output = null;
		String body = null;
		if (input == null || key == null) {
			return null;
		}
		try {
			SecretKeySpec skey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE, skey);
			byte[] b = Base64.decodeBase64(input);
			// 解密
			output = cipher.doFinal(b);
			body = new String(output, "UTF-8");
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		return body;
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		String key = "1111111111111111";// 密钥
		String data = "123456"; // 明文

		String enStr = SecurityAESTool.encrypt(data, key);
		System.out.println(enStr);
		System.out.println(URLEncoder.encode(enStr, "UTF-8"));
	}
}

四、返回参数说明

名称 类型 说明
error_code int 返回码
reason string 返回说明
result jsonobject 返回结果集
res int 属result,匹配详情,1匹配,2不匹配
orderid string 属result,单号

返回示例:

{
    "reason": "成功",
    "result": {
        "realname": "***",/*真实姓名(加密版不返回)*/
        "idcard": "******************",/*身份证号码(加密版不返回)**/
        "orderid":"J103201911121607589548",/*单号(加密版必返回,明文版根据传入参数是否返回)*/[/color]
        "res": 1 /*1:匹配 2:不匹配*/
    },
    "error_code": 0
}

评论区

詹姆斯36岁了还能获得NBA总冠军,你呢?

2

0

0

举报