springboot实现阿里云实名认证,全步骤(包含OCR文字识别API、身份证实名认证API)

这是一个简单的身份证实名认证,先通过自己上传身份证正反面,然后后台使用阿里云的OCR文字识别,识别后再使用阿里云的身份证实名认证将内容再次查询核验返回到前端。

首先我们先进入到阿里云的云市场界面然后搜索身份证OCR文字识别。

进第一个,最好是跟我一样的,因为选不一样的代码会有一些差异。 

 

 进来后可以免费试用,一般都是一个月的有效期,然后是100次的免费使用。

购买之后再去购买身份证实名认证的API,搜索身份证实名认证选下面这个进去购买即可。

然后点击右上角买家中心进入管理控制台。

 

 可以看到我这里是购买了两个,一个是ocr文字识别的,一个是身份证实名认证的,待会会用到这两个的AppCode。

OK,下面进入代码阶段。

首先我们添加pom依赖

        commons-langcommons-lang2.6com.aliyunaliyun-java-sdk-core4.5.5

然后是前端页面,我这边是一个很简单的表单页面,只有添加图片然后进行提交。



实名认证

实名认证

选择身份证正面 选择身份证背面

下面是前端界面的效果图

然后是后端页面,这是config层,里面是一些OCR和身份证认证的配置,从阿里云那边的控制台将AppCode复制过来。

我两个AppCode都是一样的所以这里是直接用一个常量来表示了。

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;import org.apache.commons.codec.binary.Base64;
import org.springframework.web.multipart.MultipartFile;public class AliIDConfig {public static final String ALI_API_HOST = "https://cardnumber.market.alicloudapi.com";public static final String ALI_API_PATH = "/rest/160601/ocr/ocr_idcard.json";public static final String ALI_API_APPCODE = "输入你的AppCode";public static final String ALI_IDCHECK_HOST = "https://sxidcheck.market.alicloudapi.com";public static final String ALI_IDCHECK_PATH = "/idcard/check";public static Map buildHeaders() {Map headers = new HashMap<>();headers.put("Authorization", "APPCODE " + ALI_API_APPCODE);headers.put("Content-Type", "application/json; charset=UTF-8");return headers;}public static String imgBase64(MultipartFile file) {String imgBase64 = "";try {byte[] content = file.getBytes();imgBase64 = new String(Base64.encodeBase64(content));} catch (IOException e) {e.printStackTrace();}return imgBase64;}
}

接下来是Controller层

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cly.happyfood.config.AliIDConfig;
import com.cly.happyfood.util.HttpUtils;import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;import java.util.HashMap;
import java.util.Map;@RestController
public class AliIDController {@PostMapping(value = "/ocr/idcard", consumes = "multipart/form-data")public JSONObject ocrIdCard(@RequestParam("file") MultipartFile file) {JSONObject result = new JSONObject();if (file.isEmpty()) {result.put("error", "请选择上传的文件");return result;}String imgBase64 = AliIDConfig.imgBase64(file);// configure配置JSONObject configObj = new JSONObject();configObj.put("side", "face");// 创建请求对象JSONObject requestObj = new JSONObject();requestObj.put("image", imgBase64);if (configObj.size() > 0) {requestObj.put("configure", configObj);}try {HttpResponse response =  HttpUtils.doPost(AliIDConfig.ALI_API_HOST,AliIDConfig.ALI_API_PATH,"POST",AliIDConfig.buildHeaders(),new HashMap<>(),requestObj.toJSONString() // 使用toJSONString()将JSONObject转换为字符串);int stat = response.getStatusLine().getStatusCode();if (stat != 200) {result.put("error", "Http code: " + stat);result.put("message", EntityUtils.toString(response.getEntity()));} else {String res = EntityUtils.toString(response.getEntity());result = JSON.parseObject(res);}} catch (Exception e) {result.put("error", "OCR API调用失败");result.put("message", e.getMessage());// 记录错误而不仅仅打印堆栈跟踪e.printStackTrace();}System.out.println(result);return checkIdcard(result.get("num"), result.get("name"));}public JSONObject checkIdcard(Object idcard, Object name) {JSONObject result = new JSONObject();Map querys = new HashMap<>();querys.put("idCard", idcard.toString());querys.put("name", name.toString());try {HttpResponse response = HttpUtils.doPost(AliIDConfig.ALI_IDCHECK_HOST,AliIDConfig.ALI_IDCHECK_PATH,"POST",AliIDConfig.buildHeaders(),querys,new HashMap<>() // No need to pass an empty map for bodys);int stat = response.getStatusLine().getStatusCode();if (stat == 200) {String res = EntityUtils.toString(response.getEntity());result = JSON.parseObject(res);} else {result.put("error", "Http code: " + stat);result.put("message", EntityUtils.toString(response.getEntity()));}} catch (Exception e) {result.put("error", "实名认证检测失败");result.put("message", e.getMessage());// 记录错误而不仅仅打印堆栈跟踪e.printStackTrace();}System.out.println(result);return result;}
}

还有阿里官方的一个HttpUtils

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;@SuppressWarnings("deprecation")
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 headers, Map querys)throws Exception {    	HttpClient httpClient = wrapClient(host);HttpGet request = new HttpGet(buildUrl(host, path, querys));for (Map.Entry 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 headers, Map querys, Map bodys)throws Exception {    	HttpClient httpClient = wrapClient(host);HttpPost request = new HttpPost(buildUrl(host, path, querys));for (Map.Entry e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}if (bodys != null) {List nameValuePairList = new ArrayList();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 headers, Map querys, String body)throws Exception {    	HttpClient httpClient = wrapClient(host);HttpPost request = new HttpPost(buildUrl(host, path, querys));for (Map.Entry 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 headers, Map querys, byte[] body)throws Exception {    	HttpClient httpClient = wrapClient(host);HttpPost request = new HttpPost(buildUrl(host, path, querys));for (Map.Entry 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 headers, Map querys, String body)throws Exception {    	HttpClient httpClient = wrapClient(host);HttpPut request = new HttpPut(buildUrl(host, path, querys));for (Map.Entry 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 headers, Map querys, byte[] body)throws Exception {    	HttpClient httpClient = wrapClient(host);HttpPut request = new HttpPut(buildUrl(host, path, querys));for (Map.Entry 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 headers, Map querys)throws Exception {    	HttpClient httpClient = wrapClient(host);HttpDelete request = new HttpDelete(buildUrl(host, path, querys));for (Map.Entry e : headers.entrySet()) {request.addHeader(e.getKey(), e.getValue());}return httpClient.execute(request);}private static String buildUrl(String host, String path, Map 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 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);}}
}

这样就实现了身份证的一个实名认证功能,后续可以再发送请求添加数据库什么的,还有身份证照片的话我是只处理了正面(偷懒),所以背面的照片其实放不放上去都无所谓。


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部