身份证实名认证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 fengjian

 * @date 创建时间:2015年9月14日 下午3:02:46

 */

public class idcard_103 {

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 verifys_url = "http://op.juhe.cn/idcard/verifys?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 uorderid = "";

// String sign = MD5(openid + APPKEY + idcard + realname + uorderid);

// int type = 2;// 验签版本

// Map<String, Object> params = new HashMap<>();

// params.put("realname", realname);

// params.put("idcard", idcard);

// params.put(uorderid, uorderid);

// params.put("sign", sign);


// ----------------------身份证实名查询(加密版)-----------------------------------------------------------------------

String realname = "张三";// 姓名

String idcard = "32072119970602561X";// 身份证

String openid = "";// 个人中心查询

String key = MD5(openid).substring(0, 16);

int type = 3;// 加密版本

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验签版,3加密版

* @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 = verifys_url;

break;

case 3:

url = queryEncry_url;

break;

}

try {

url = new StringBuffer(url).append("&").append(urlencode(params)).toString();

System.out.println(url);

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 input

* @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字符串


// 把数组每一字节换成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();

}


}


登录 后可发表评论

    最新发布
    聚合数据©版权所有