Java使用阿里云接口进行身份证实名认证的示例实现

 更新时间:2020年07月13日 15:04:14   作者:ruidongjun007  
这篇文章主要介绍了使用阿里云接口进行身份证实名认证的示例实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

如今随着互联网产业的多元化发展,尤其是互联网金融,O2O,共享经济等新兴商业形式的兴起,企业对实名认证业务的数据形式和数据质量有了更高的需求。如今也衍生出身份证实名认证业务,通过接口将身份证号码、姓名上传至阿里云,再与全国公民身份信息系统进行匹配,判断信息的一致性。

在使用接口服务的方面我推荐使用技术实力强大的阿里云;

附上:阿里云最高¥2000云产品通用代金券

首先点击:【阿里云API接口】获取相应的订单后在控制台中可以得到您的appcode;

发送数据:

bodys.put("idNo", "340421190210182345");
bodys.put("name", "张三");

返回数据:

{
 "name": "张三",
 "idNo": "340421190710145412",
 "respMessage": "身份证信息匹配",
 "respCode": "0000",
 "province": "安徽省",
 "city": "淮南市",
 "county": "凤台县",
 "birthday": "19071014",
 "sex": "M",
 "age": "111"
}

具体实现类:

public static void main(String[] args) {
    String host = "https://idenauthen.market.alicloudapi.com";
    String path = "/idenAuthentication";
    String method = "POST";
    String appcode = "你自己的AppCode";
    Map<String, String> headers = new HashMap<String, String>();
    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
    headers.put("Authorization", "APPCODE " + appcode);
    //根据API的要求,定义相对应的Content-Type
    headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    Map<String, String> querys = new HashMap<String, String>();
    Map<String, String> bodys = new HashMap<String, String>();
    bodys.put("idNo", "340421190210182345");
    bodys.put("name", "张三");
 
 
    try {
      /**
      * 重要提示如下:
      * HttpUtils请从
      * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
      * 下载
      *
      * 相应的依赖请参照
      * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
      */
      HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
      System.out.println(response.toString());
      //获取response的body
      //System.out.println(EntityUtils.toString(response.getEntity()));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

工具类HttpUtils:

package com.netgate.util.send;
 
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
 
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
 
public class HttpUtils {
  
  /**
   * get
   * 
   * @param host
   * @param path
   * @param method
   * @param headers
   * @param querys
   * @return
   * @throws Exception
   */
  public static HttpResponse doGet(String host, String path, String method, 
      Map<String, String> headers, 
      Map<String, String> querys)
      throws Exception {    
    HttpClient httpClient = wrapClient(host);
 
    HttpGet request = new HttpGet(buildUrl(host, path, querys));
    for (Map.Entry<String, String> e : headers.entrySet()) {
      request.addHeader(e.getKey(), e.getValue());
    }
    
    return httpClient.execute(request);
  }
  
  /**
   * post form
   * 
   * @param host
   * @param path
   * @param method
   * @param headers
   * @param querys
   * @param bodys
   * @return
   * @throws Exception
   */
  public static HttpResponse doPost(String host, String path, String method, 
      Map<String, String> headers, 
      Map<String, String> querys, 
      Map<String, String> bodys)
      throws Exception {    
    HttpClient httpClient = wrapClient(host);
 
    HttpPost request = new HttpPost(buildUrl(host, path, querys));
    for (Map.Entry<String, String> e : headers.entrySet()) {
      request.addHeader(e.getKey(), e.getValue());
    }
 
    if (bodys != null) {
      List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
 
      for (String key : bodys.keySet()) {
        nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
      }
      UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
      formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
      request.setEntity(formEntity);
    }
 
    return httpClient.execute(request);
  }  
  
  /**
   * Post String
   * 
   * @param host
   * @param path
   * @param method
   * @param headers
   * @param querys
   * @param body
   * @return
   * @throws Exception
   */
  public static HttpResponse doPost(String host, String path, String method, 
      Map<String, String> headers, 
      Map<String, String> querys, 
      String body)
      throws Exception {    
    HttpClient httpClient = wrapClient(host);
 
    HttpPost request = new HttpPost(buildUrl(host, path, querys));
    for (Map.Entry<String, String> e : headers.entrySet()) {
      request.addHeader(e.getKey(), e.getValue());
    }
 
    if (StringUtils.isNotBlank(body)) {
      request.setEntity(new StringEntity(body, "utf-8"));
    }
 
    return httpClient.execute(request);
  }
  
  /**
   * Post stream
   * 
   * @param host
   * @param path
   * @param method
   * @param headers
   * @param querys
   * @param body
   * @return
   * @throws Exception
   */
  public static HttpResponse doPost(String host, String path, String method, 
      Map<String, String> headers, 
      Map<String, String> querys, 
      byte[] body)
      throws Exception {    
    HttpClient httpClient = wrapClient(host);
 
    HttpPost request = new HttpPost(buildUrl(host, path, querys));
    for (Map.Entry<String, String> e : headers.entrySet()) {
      request.addHeader(e.getKey(), e.getValue());
    }
 
    if (body != null) {
      request.setEntity(new ByteArrayEntity(body));
    }
 
    return httpClient.execute(request);
  }
  
  /**
   * Put String
   * @param host
   * @param path
   * @param method
   * @param headers
   * @param querys
   * @param body
   * @return
   * @throws Exception
   */
  public static HttpResponse doPut(String host, String path, String method, 
      Map<String, String> headers, 
      Map<String, String> querys, 
      String body)
      throws Exception {    
    HttpClient httpClient = wrapClient(host);
 
    HttpPut request = new HttpPut(buildUrl(host, path, querys));
    for (Map.Entry<String, String> e : headers.entrySet()) {
      request.addHeader(e.getKey(), e.getValue());
    }
 
    if (StringUtils.isNotBlank(body)) {
      request.setEntity(new StringEntity(body, "utf-8"));
    }
 
    return httpClient.execute(request);
  }
  
  /**
   * Put stream
   * @param host
   * @param path
   * @param method
   * @param headers
   * @param querys
   * @param body
   * @return
   * @throws Exception
   */
  public static HttpResponse doPut(String host, String path, String method, 
      Map<String, String> headers, 
      Map<String, String> querys, 
      byte[] body)
      throws Exception {    
    HttpClient httpClient = wrapClient(host);
 
    HttpPut request = new HttpPut(buildUrl(host, path, querys));
    for (Map.Entry<String, String> e : headers.entrySet()) {
      request.addHeader(e.getKey(), e.getValue());
    }
 
    if (body != null) {
      request.setEntity(new ByteArrayEntity(body));
    }
 
    return httpClient.execute(request);
  }
  
  /**
   * Delete
   * 
   * @param host
   * @param path
   * @param method
   * @param headers
   * @param querys
   * @return
   * @throws Exception
   */
  public static HttpResponse doDelete(String host, String path, String method, 
      Map<String, String> headers, 
      Map<String, String> querys)
      throws Exception {    
    HttpClient httpClient = wrapClient(host);
 
    HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
    for (Map.Entry<String, String> e : headers.entrySet()) {
      request.addHeader(e.getKey(), e.getValue());
    }
    
    return httpClient.execute(request);
  }
  
  private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
    StringBuilder sbUrl = new StringBuilder();
    sbUrl.append(host);
    if (!StringUtils.isBlank(path)) {
      sbUrl.append(path);
    }
    if (null != querys) {
      StringBuilder sbQuery = new StringBuilder();
      for (Map.Entry<String, String> query : querys.entrySet()) {
        if (0 < sbQuery.length()) {
          sbQuery.append("&");
        }
        if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
          sbQuery.append(query.getValue());
        }
        if (!StringUtils.isBlank(query.getKey())) {
          sbQuery.append(query.getKey());
          if (!StringUtils.isBlank(query.getValue())) {
            sbQuery.append("=");
            sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
          }          
        }
      }
      if (0 < sbQuery.length()) {
        sbUrl.append("?").append(sbQuery);
      }
    }
    
    return sbUrl.toString();
  }
  
  private static HttpClient wrapClient(String host) {
    HttpClient httpClient = new DefaultHttpClient();
    if (host.startsWith("https://")) {
      sslClient(httpClient);
    }
    
    return httpClient;
  }
  
  private static void sslClient(HttpClient httpClient) {
    try {
      SSLContext ctx = SSLContext.getInstance("TLS");
      X509TrustManager tm = new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
          return null;
        }
        public void checkClientTrusted(X509Certificate[] xcs, String str) {
          
        }
        public void checkServerTrusted(X509Certificate[] xcs, String str) {
          
        }
      };
      ctx.init(null, new TrustManager[] { tm }, null);
      SSLSocketFactory ssf = new SSLSocketFactory(ctx);
      ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
      ClientConnectionManager ccm = httpClient.getConnectionManager();
      SchemeRegistry registry = ccm.getSchemeRegistry();
      registry.register(new Scheme("https", 443, ssf));
    } catch (KeyManagementException ex) {
      throw new RuntimeException(ex);
    } catch (NoSuchAlgorithmException ex) {
      throw new RuntimeException(ex);
    }
  }
}

到此这篇关于Java使用阿里云接口进行身份证实名认证的示例实现的文章就介绍到这了,更多相关阿里云身份证实名认证内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • MyBatis连接数据库配置的基本步骤和机制

    MyBatis连接数据库配置的基本步骤和机制

    MyBatis 是一个流行的持久层框架,它通过使用XML或注解的方式将SQL语句、存储过程和Java方法进行绑定,从而避免了手写大量的JDBC代码和手动设置参数与结果集,本文给大家介绍了MyBatis连接数据库配置的基本步骤和机制,需要的朋友可以参考下
    2024-05-05
  • Java中Spring WebSocket详解

    Java中Spring WebSocket详解

    本篇文章主要通过代码给大家详细分析了Java中Spring WebSocket的用法,需要的读者们参考学习下吧。
    2017-12-12
  • 解决maven打包失败:程序包xxxx不存在问题

    解决maven打包失败:程序包xxxx不存在问题

    这篇文章主要介绍了解决maven打包失败:程序包xxxx不存在问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • mvn中dependencyManagement的使用详解

    mvn中dependencyManagement的使用详解

    这篇文章主要介绍了mvn中dependencyManagement的使用,子项目中只是声明使用此依赖即可,可不用指定版本(将使用父pom同一指定的版本),若指定了版本,将以子项目的版本号为主,需要的朋友可以参考下
    2022-08-08
  • Spring boot 整合 Redisson实现分布式锁并验证功能

    Spring boot 整合 Redisson实现分布式锁并验证功能

    这篇文章主要介绍了Spring boot 整合 Redisson实现分布式锁并验证,redisson是官方推荐的分布式锁实现方案,采用redis自身的原子命令和lua脚本来实现,需要的朋友可以参考下
    2022-05-05
  • java读取resources文件详解及实现代码

    java读取resources文件详解及实现代码

    这篇文章主要介绍了java读取resources文件详解及实现代码的相关资料,在开发项目的时候经常会遇到读取文件夹里面的内容,需要的朋友可以参考下
    2017-07-07
  • 如何使用Idea搭建全注解式开发的SpringMVC项目

    如何使用Idea搭建全注解式开发的SpringMVC项目

    这篇文章主要介绍了如何使用Idea搭建全注解式开发的SpringMVC项目,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03
  • Spring DevTools的介绍

    Spring DevTools的介绍

    今天小编就为大家分享一篇关于Spring DevTools的介绍,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • Springboot MultipartFile文件上传与下载的实现示例

    Springboot MultipartFile文件上传与下载的实现示例

    在Spring Boot项目中,可以使用MultipartFile类来处理文件上传和下载操作,本文就详细介绍了如何使用,具有一定的参考价值,感兴趣的可以了解一下
    2023-08-08
  • Spring配置多个数据源并实现动态切换示例

    Spring配置多个数据源并实现动态切换示例

    本篇文章主要介绍了Spring配置多个数据源并实现动态切换示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04

最新评论