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

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

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

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

 

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

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

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

 

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

OK,下面进入代码阶段。

首先我们添加pom依赖

        <!-- 阿里实名认证 -->
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.6</version>
		</dependency>
		
	    <dependency>
	        <groupId>com.aliyun</groupId>
	        <artifactId>aliyun-java-sdk-core</artifactId>
	        <version>4.5.5</version>
	    </dependency>

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

<!DOCTYPE html>
<html>
<head>
  <title>实名认证</title>
  <style>
    body {
      background-color: #f8f8f8;
      font-family: Arial, sans-serif;
    }

    .container {
      width: 450px;
      margin: 0 auto;
      padding: 20px;
      background-color: #ffffff;
      border-radius: 5px;
      box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
      text-align: center;
    }

    h2 {
      margin-top: 0;
      margin-bottom: 20px;
      color: #333333;
    }

    .form-group {
      margin-bottom: 20px;
    }

    .form-group label {
      display: block;
      margin-bottom: 5px;
      font-weight: bold;
      color: #555555;
    }

    .upload-div {
      position: relative;
      width: 85.60mm;
      height: 53.98mm;
      border: 2px dashed #aaaaaa;
      border-radius: 4px;
      overflow: hidden;
      cursor: pointer;
      background-repeat: no-repeat;
      background-position: center center;
      background-size: cover;
      margin-bottom: 10px;
    }

    .upload-div input[type="file"] {
      opacity: 0;
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
      cursor: pointer;
    }

    .upload-div label {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
      cursor: pointer;
      color: #aaaaaa;
      font-size: 14px;
      font-weight: bold;
    }

    /* 隐藏显示图片时的文字提示 */
    .upload-div.has-image label {
      display: none;
    }

    .preview-container {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 10px;
    }

    .preview-container img {
      width: 100%;
      height: 100%;
      border-radius: 4px;
      box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    }

    .submit-btn {
      display: inline-block;
      background-color: #4CAF50;
      color: white;
      padding: 10px 20px;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      font-size: 16px;
      text-decoration: none;
    }

    .submit-btn:hover {
      background-color: #45a049;
    }
  </style>
</head>
<body>
  <div class="container">
    <h2>实名认证</h2>
    <form enctype="multipart/form-data" >
      <div class="form-group" style="margin-left: 70px;">
        <div class="upload-div" id="frontPreview">
          <input type="file" id="frontImage" name="frontImage"
            onchange="previewImage(event, 'frontPreview', true)"> <label
            for="frontImage">选择身份证正面</label>
        </div>
      </div>
      <div class="form-group" style="margin-left: 70px;">
        <div class="upload-div" id="backPreview">
          <input type="file" id="backImage" name="backImage"
            onchange="previewImage(event, 'backPreview', false)"> <label
            for="backImage">选择身份证背面</label>
        </div>
      </div>
      <button type="button" class="submit-btn" onclick="processImage()">提交</button>
    </form>
  </div>
</body>
<script type="text/javascript">
	let selectedImage = null;
	function previewImage(event, previewId, flag) {
		var preview = document.getElementById(previewId);
		var image = event.target.files[0];
		var reader = new FileReader();
	
		reader.onload = function(e) {
			preview.style.backgroundImage = 'url(' + e.target.result + ')';
			preview.classList.add("has-image"); // 添加 class 来隐藏文字
		}
	
		reader.readAsDataURL(image);
		
		if (flag) {
			selectedImage = image;
			console.log(image);
		}
	}

	function processImage() {
	    if (!selectedImage) {
	        alert("请先选择图片");
	        return;
	    }
	
	    const formData = new FormData();
	    formData.append("file", selectedImage);
	
	    fetch("/ocr/idcard", {
	        method: "POST",
	        body: formData
	    })
	    .then(response => response.json())
	    .then(data => {
	        console.log(data);
	        // 处理后端返回的 OCR 结果
	        // 在这里您可以根据需要更新界面或显示识别结果
	    })
	    .catch(error => console.error("请求失败:", error));
	}
</script>
</html>

下面是前端界面的效果图

然后是后端页面,这是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<String, String> buildHeaders() {
        Map<String, String> 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<String, String> 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<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);
        }
    }
}

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

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值