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

如今随着互联网产业的多元化发展,尤其是互联网金融,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使用阿里云接口进行身份证实名认证的示例实现的文章就介绍到这了,更多相关阿里云身份证实名认证内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Java实现基于token认证的方法示例

    随着互联网的不断发展,技术的迭代也非常之快.我们的用户认证也从刚开始的用户名密码转变到基于cookie的session认证,然而到了今天,这种认证已经不能满足与我们的业务需求了(分布式,微服务).我们采用了另外一种认证方式:基于token的认证. 一.与cookie相比较的优势: 1.支持跨域访问,将token置于请求头中,而cookie是不支持跨域访问的: 2.无状态化,服务端无需存储token,只需要验证token信息是否正确即可,而session需要在服务端存储,一般是通过cookie中的

  • Java实现SSL双向认证的方法

    本文实例讲述了Java实现SSL双向认证的方法.分享给大家供大家参考,具体如下: 我们常见的SSL验证较多的只是验证我们的服务器是否是真实正确的,当然如果你访问的URL压根就错了,那谁也没有办法.这个就是所谓的SSL单向认证. 但是实际中,我们有可能还会验证客户端是否符合要求,也就是给我们每个用户颁发一个证书,比且每个数字证书都是唯一的,不公开的.这样就能通过这个数字证书保证当前访问我服务器的这个用户是经过服务器认可的,其他人不可访问. 双向认证 从第一个层面上 确保了服务器 与客户端 都是互相

  • Java使用OTP动态口令(每分钟变一次)进行登录认证

    GIT地址:https://github.com/suyin58/otp-demo 动态码截图: 在对外网开放的后台管理系统中,使用静态口令进行身份验证可能会存在如下问题: (1) 为了便于记忆,用户多选择有特征作为密码,所有静态口令相比动态口令而言,容易被猜测和破解: (2) 黑客可以从网上或电话线上截获静态密码,如果是非加密方式传输,用户认证信息可被轻易获取: (3) 内部工作人员可通过合法授权取得用户密码而非法使用: 静态口令根本上不能确定用户的身份,其结果是,个人可以轻松地伪造一个假身份

  • Java使用JDBC实现Oracle用户认证的方法详解

    本文实例讲述了Java使用JDBC实现Oracle用户认证的方法.分享给大家供大家参考,具体如下: 两天时间写的小品,以前的J2EE环境基本使用框架.现在使用JDBC配合Oracle存储过程模拟了一下用户注册和用户认证. 一.添加必须的jar包 需要JDBC连接Oracle的包和shiro-core依赖,添加shiro-core主要为了方便使用SHA-256散列算法. 二.编写JDBC连接 import java.sql.Connection; import java.sql.DriverMan

  • JavaWeb使用Session和Cookie实现登录认证

    后台管理页面往往需要登录才可以进行操作,这时就需要Seession来记录登录状态 要实现起来也是非常简单,只需要自定义一个HandlerInterceptor就行了 自定义的HandlerInterceptor也只有短短几行代码 public class LoginInterceptor implements HandlerInterceptor { @Override public void afterCompletion(HttpServletRequest request, HttpSer

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

    如今随着互联网产业的多元化发展,尤其是互联网金融,O2O,共享经济等新兴商业形式的兴起,企业对实名认证业务的数据形式和数据质量有了更高的需求.如今也衍生出身份证实名认证业务,通过接口将身份证号码.姓名上传至阿里云,再与全国公民身份信息系统进行匹配,判断信息的一致性. 在使用接口服务的方面我推荐使用技术实力强大的阿里云: 附上:阿里云最高¥2000云产品通用代金券 首先点击:[阿里云API接口]获取相应的订单后在控制台中可以得到您的appcode: 发送数据: bodys.put("idNo&qu

  • Java实现阿里云短信接口的示例

    阿里云短信服务接口 阿里云短信服务(Short Message Service)是阿里云为用户提供的一种通信服务的能力. 支持向国内和国际快速发送验证码.短信通知和推广短信,服务范围覆盖全球200多个国家和地区.国内短信支持三网合一专属通道,与工信部携号转网平台实时互联.电信级运维保障,实时监控自动切换,到达率高达99%.完美支撑双11期间20亿短信发送,6亿用户触达. 快速开发 ①开启短信服务 1)登陆阿里云服务平台 2)选择控制台 3)点击左上角下拉按钮选择短信服务 4)开通短信服务 ②实名

  • SpringBoot+阿里云OSS实现在线视频播放的示例

    阿里云 OSS 是一种云存储技术,你可以理解为云盘,我们的目标是将视频存储到云端,然后在前端读取并播放视频. OSS 首先登陆首页,创建一个存储桶:https://oss.console.aliyun.com 然后找到读写权限: 将读写权限设置为公共读即可: 在 RAM 中新建一个用户: 为其添加权限,选择 OSS 的权限: 然后点进去这个用户,找到 AccessKey: 创建之后记下来 secret ,因为他只出现一次,如果没记住也没事,可以重新创建新的 key. 下面开始编写服务端代码: P

  • springboot整合阿里云oss上传的方法示例

    OSS申请和配置 1. 注册登录 输入网址:https://www.aliyun.com/product/oss 如果没有账号点击免费注册,然后登录. 2.开通以及配置 点击立即开通 进入管理控制台 第一次使用会出现引导,按引导点击"我知道了",然后点击创建Bucket. 如果没有存储包或流量包点击购买. 点击确定,返回主页面,出现该页面,点击我知道了 将EndPoint记录下来,方便后期添加到我们项目的配置文件中 创建 AccessKeyID 和 AccessKeySecret 点击

  • React实现阿里云OSS上传文件的示例

    简介 阿里云 OSS 是 阿里云提供的海量.安全.低成本.高可靠的云存储服务,提供 99.9999999999%的数据可靠性(号称).能够使用 RESTful API 可以在互联网任何位置存储和访问,支持容量和处理能力弹性扩展. 基本术语 1.bucket :类似本地的一个文件夹 2.object : oss 存储数据的基本单元,类似本地的一个文件. 3.region:oss 存储的数据中心所在区域 4.Endpoint:oss 对外服务的访问域名,oss 以 http api 提供服务,不同

  • java中实现Comparable接口实现自定义排序的示例

    实例如下所示: class Student implements Comparable{ String name; int gpa; @Override public int compareTo(Object arg0) { // TODO Auto-generated method stub Student s = (Student)arg0; if(gpa == s.gpa) return name.compareTo(s.name); else if(gpa < s.gpa) return

  • python利用百度云接口实现车牌识别的示例

    一个小需求---实现车牌识别. 目前有两个想法 1. 调云在线的接口或者使用SDK做开发(配置环境和编译第三方库很麻烦,当然使用python可以避免这些问题) 2. 自己实现车牌识别算法(复杂) 一开始准备使用百度云文字识别C++ SDK来做,发现需要准备curl.jsoncpp和OpenCV,并且curl和jsoncpp需要自己编译,很麻烦,所以换用了python来做,真的是顺畅简单. 1. 安装python环境(我用python3.7) python官网下载地址:https://www.py

  • 浅谈使用java实现阿里云消息队列简单封装

    一.前言 最近公司有使用阿里云消息队列的需求,为了更加方便使用,本人用了几天时间将消息队列封装成api调用方式以方便内部系统的调用,现在已经完成,特此记录其中过程和使用到的相关技术,与君共勉. 现在阿里云提供了两种消息服务:mns服务和ons服务,其中我认为mns是简化版的ons,而且mns的消息消费需要自定义轮询策略的,相比之下,ons的发布与订阅模式功能更加强大(比如相对于mns,ons提供了消息追踪.日志.监控等功能),其api使用起来更加方便,而且听闻阿里内部以后不再对mns进行新的开发

  • JAVA演示阿里云图像识别API,印刷文字识别-营业执照识别

    最近有由于需要,我开始接触阿里云的云市场的印刷文字识别-营业执照识别这里我加上了官网的申请说明,只要你有阿里云账号就可以用,前500次是免费的,API说明很简陋,只能做个简单参考. 一.API介绍 JAVA示例: public static void main(String[] args) { String host = "https://dm-58.data.aliyun.com"; String path = "/rest/160601/ocr/ocr_business_

  • laravel框架使用阿里云短信发送消息操作示例

    本文实例讲述了laravel框架使用阿里云短信发送消息操作.分享给大家供大家参考,具体如下: 最新需要用到发送短信的功能,所以就在网上搜索一些写好的扩展. 扩展地址: https://github.com/MissMyCat/aliyun-sms 通过composer安装: composer require mrgoon/aliyun-sms dev-master 在 config/app.php 中 providers 加入: Mrgoon\AliSms\ServiceProvider::cl

随机推荐