zl程序教程

您现在的位置是:首页 >  IT要闻

当前栏目

互联网用户账号信息真实性认证(阿里云身份信息实名认证API接口服务)

2023-04-18 17:00:52 时间

        2021年10月份国家互联网信息办公室公布《互联网用户账号名称信息管理规定(征求意见稿)》。内容包括,互联网用户账号服务平台应当按照“后台实名、前台自愿”的原则,要求互联网用户账号使用者在注册账号时提供真实身份信息。互联网用户账号使用者不提供真实身份信息或者虚假注册的,互联网用户账号服务平台不得为其提供服务。如何对用户提供的身份证信息进行真实性验证呢?

下面我们就介绍如何使用阿里云的实名认证服务:

 

1.注册阿里云账号并完成实名制认证(已有阿里云账号请略过):阿里云-上云就上阿里云 (aliyun.com)

2.购买测试套餐:https://market.aliyun.com/products/57126001/cmapi025518.html?#sku=yuncode1951800000icon-default.png?t=M3K6https://market.aliyun.com/products/57126001/cmapi025518.html?#sku=yuncode19518000003.获取您的秘钥(appdoce):https://market.console.aliyun.com/imageconsole/index.htm?spm=5176.product-detail.J_3982476530.15.2b6217c1jFWWLt#/?_k=ld57ewicon-default.png?t=M3K6https://market.console.aliyun.com/imageconsole/index.htm?spm=5176.product-detail.J_3982476530.15.2b6217c1jFWWLt#/?_k=ld57ew4.编写接口代码(本例已java为例,产品页面有其他代码实例):

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();
        }
    }

工具类:

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);
        }
    }
}

5.数据实例:

发送数据:

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

返回数据:

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

阿里云在国内拥有强大的技术沉淀,保证了接口的稳定服务,同时可以在线申请发票、合同等服务也为大家的工作节省大量时间,


希望大家搬砖快乐!