|
@@ -6,15 +6,27 @@ package com.ruoyi.common.utils;
|
|
|
* @Describe:
|
|
|
*/
|
|
|
|
|
|
+import com.aliyun.cloudauth20190307.Client;
|
|
|
+import com.aliyun.cloudauth20190307.models.*;
|
|
|
+import com.aliyun.teaopenapi.models.Config;
|
|
|
+import com.aliyun.teautil.models.RuntimeOptions;
|
|
|
+
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
+
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import com.aliyun.cloudauth20190307.Client;
|
|
|
+import com.aliyun.cloudauth20190307.models.*;
|
|
|
+import com.aliyun.teaopenapi.models.Config;
|
|
|
+import com.aliyun.teautil.models.RuntimeOptions;
|
|
|
import com.ruoyi.common.core.domain.SysUserIdcardVo;
|
|
|
-import com.ruoyi.common.core.redis.RedisCache;
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
-import com.ruoyi.common.core.domain.IdCardVo;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.crypto.Cipher;
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
@@ -23,10 +35,7 @@ import java.io.UnsupportedEncodingException;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Paths;
|
|
|
-import java.util.Base64;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.*;
|
|
|
|
|
|
import static com.ruoyi.common.constant.CommonConstants.*;
|
|
|
|
|
@@ -184,19 +193,19 @@ public class IdCardUtil {
|
|
|
String decryptResult = parseResult(encryptResult);
|
|
|
JSONObject jsonObject = JSON.parseObject(decryptResult);
|
|
|
String wordsResult = jsonObject.getString("words_result");
|
|
|
- Map<String,Object> map = new HashMap<>(3);
|
|
|
- if (StringUtils.isNotEmpty(wordsResult)){
|
|
|
+ Map<String, Object> map = new HashMap<>(3);
|
|
|
+ if (StringUtils.isNotEmpty(wordsResult)) {
|
|
|
JSONObject jsonObjectWordsResult = JSON.parseObject(wordsResult);
|
|
|
- if (FRONT.equals(idCardSide)){
|
|
|
+ if (FRONT.equals(idCardSide)) {
|
|
|
String name = JSON.parseObject(jsonObjectWordsResult.getString("姓名")).getString("words");
|
|
|
String address = JSON.parseObject(jsonObjectWordsResult.getString("住址")).getString("words");
|
|
|
String idCard = JSON.parseObject(jsonObjectWordsResult.getString("公民身份号码")).getString("words");
|
|
|
- map.put("realName",name);
|
|
|
- map.put("address",address);
|
|
|
- map.put("idCard",idCard);
|
|
|
- }else if (BACK.equals(idCardSide)){
|
|
|
+ map.put("realName", name);
|
|
|
+ map.put("address", address);
|
|
|
+ map.put("idCard", idCard);
|
|
|
+ } else if (BACK.equals(idCardSide)) {
|
|
|
String date = JSON.parseObject(jsonObjectWordsResult.getString("失效日期")).getString("words");
|
|
|
- map.put("expirationDate",date);
|
|
|
+ map.put("expirationDate", date);
|
|
|
}
|
|
|
}
|
|
|
return AjaxResult.success(map);
|
|
@@ -521,8 +530,8 @@ public class IdCardUtil {
|
|
|
*/
|
|
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
String wordsResult = jsonObject.getString("words_result");
|
|
|
- Map<String,Object> map = new HashMap<>(9);
|
|
|
- if (StringUtils.isNotEmpty(wordsResult)){
|
|
|
+ Map<String, Object> map = new HashMap<>(9);
|
|
|
+ if (StringUtils.isNotEmpty(wordsResult)) {
|
|
|
JSONObject jsonObjectWordsResult = JSON.parseObject(wordsResult);
|
|
|
String creditCode = JSON.parseObject(jsonObjectWordsResult.getString("社会信用代码")).getString("words");
|
|
|
String enterpriseName = JSON.parseObject(jsonObjectWordsResult.getString("单位名称")).getString("words");
|
|
@@ -533,15 +542,15 @@ public class IdCardUtil {
|
|
|
String establishData = JSON.parseObject(jsonObjectWordsResult.getString("成立日期")).getString("words");
|
|
|
String businessTerm = JSON.parseObject(jsonObjectWordsResult.getString("有效期")).getString("words");
|
|
|
String businessScope = JSON.parseObject(jsonObjectWordsResult.getString("经营范围")).getString("words");
|
|
|
- map.put("creditCode",creditCode);
|
|
|
- map.put("enterpriseName",enterpriseName);
|
|
|
- map.put("enterpriseType",enterpriseType);
|
|
|
- map.put("enterpriseAddress",enterpriseAddress);
|
|
|
- map.put("legalName",legalName);
|
|
|
- map.put("registeredCapital",registeredCapital);
|
|
|
- map.put("establishData",establishData);
|
|
|
- map.put("businessTerm",businessTerm);
|
|
|
- map.put("businessScope",businessScope);
|
|
|
+ map.put("creditCode", creditCode);
|
|
|
+ map.put("enterpriseName", enterpriseName);
|
|
|
+ map.put("enterpriseType", enterpriseType);
|
|
|
+ map.put("enterpriseAddress", enterpriseAddress);
|
|
|
+ map.put("legalName", legalName);
|
|
|
+ map.put("registeredCapital", registeredCapital);
|
|
|
+ map.put("establishData", establishData);
|
|
|
+ map.put("businessTerm", businessTerm);
|
|
|
+ map.put("businessScope", businessScope);
|
|
|
}
|
|
|
return AjaxResult.success(map);
|
|
|
} catch (Exception e) {
|
|
@@ -641,4 +650,129 @@ public class IdCardUtil {
|
|
|
String accessToken = jsonObject.getString("access_token");
|
|
|
return accessToken;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 人脸识别阿里云金融级实人认证
|
|
|
+ *
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ public static AjaxResult ocrSampleAliYun(SysUserIdcardVo sysUserIdcardVo) {
|
|
|
+
|
|
|
+ return AjaxResult.success(initFaceVerify(sysUserIdcardVo));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 阿里云调用人脸识别
|
|
|
+ * 每次开始认证前通过本接口获取CertifyId,用来串联认证请求中的各个接口。
|
|
|
+ */
|
|
|
+ public static String initFaceVerify(SysUserIdcardVo sysUserIdcardVo) {
|
|
|
+ InitFaceVerifyRequest request = new InitFaceVerifyRequest();
|
|
|
+ // 请输入场景ID+L。1000009979场景id固定值
|
|
|
+ request.setSceneId(1000009979L);
|
|
|
+ // 设置商户请求的唯一标识。
|
|
|
+ request.setOuterOrderNo(UUID.randomUUID().toString());
|
|
|
+ // 认证方案。
|
|
|
+ request.setProductCode("ID_PRO");
|
|
|
+ // 模式。要进行活体检测的类型。取值:
|
|
|
+ //LIVENESS(默认):眨眼动作活体检测。
|
|
|
+ //
|
|
|
+ //PHOTINUS_LIVENESS:眨眼动作活体+炫彩活体双重检测。
|
|
|
+ //
|
|
|
+ //MULTI_ACTION:多动作活体检测。当前为眨眼+任意摇头检测。
|
|
|
+ request.setModel("PHOTINUS_LIVENESS");
|
|
|
+ //不同证件类型,取值均为IDENTITY_CARD
|
|
|
+ request.setCertType("IDENTITY_CARD");
|
|
|
+ //您的终端用户的真实姓名
|
|
|
+ request.setCertName(sysUserIdcardVo.getRealName());
|
|
|
+ //您的终端用户的证件号码。
|
|
|
+ request.setCertNo(sysUserIdcardVo.getIdCard());
|
|
|
+ // MetaInfo环境参数。
|
|
|
+ request.setMetaInfo(sysUserIdcardVo.getMetaInfo());
|
|
|
+ //request.setMobile("130xxxxxxxx");
|
|
|
+ //request.setIp("114.xxx.xxx.xxx");
|
|
|
+ //request.setUserId("12345xxxx");
|
|
|
+ //request.setCallbackUrl("https://www.aliyundoc.com");
|
|
|
+ //request.setCallbackToken("xxxxx");
|
|
|
+ // 如需开启个人信息加密传输。
|
|
|
+ //request.setEncryptType("SM2");
|
|
|
+ //request.setCertName("BCRD/7ZkNy7Q*****M1BMBezZe8GaYHrLwyJv558w==");
|
|
|
+ //request.setCertNo("BMjsstxK3S4b1YH*****Pet8ECObfxmLN92SLsNg==");
|
|
|
+
|
|
|
+ // 推荐,支持服务路由。
|
|
|
+ InitFaceVerifyResponse response = initFaceVerifyAutoRoute(request);
|
|
|
+
|
|
|
+ // 不支持服务自动路由。
|
|
|
+ //InitFaceVerifyResponse response = initFaceVerify("cloudauth.cn-shanghai.aliyuncs.com", request);
|
|
|
+
|
|
|
+ response.getBody().getRequestId();
|
|
|
+ String certifyId = response.getBody().getResultObject().getCertifyId();
|
|
|
+ System.out.println(response.getBody().getRequestId());
|
|
|
+ System.out.println(response.getBody().getCode());
|
|
|
+ System.out.println(response.getBody().getMessage());
|
|
|
+ System.out.println(response.getBody().getResultObject() == null ? null
|
|
|
+ : response.getBody().getResultObject().getCertifyId());
|
|
|
+
|
|
|
+ return certifyId;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static InitFaceVerifyResponse initFaceVerifyAutoRoute(InitFaceVerifyRequest request) {
|
|
|
+ // 第一个为主区域Endpoint,第二个为备区域Endpoint。
|
|
|
+ List<String> endpoints = Arrays.asList("cloudauth.cn-shanghai.aliyuncs.com", "cloudauth.cn-beijing.aliyuncs.com");
|
|
|
+ InitFaceVerifyResponse lastResponse = null;
|
|
|
+ for (int i = 0; i < endpoints.size(); i++) {
|
|
|
+ try {
|
|
|
+ InitFaceVerifyResponse response = initFaceVerify(endpoints.get(i), request);
|
|
|
+ lastResponse = response;
|
|
|
+
|
|
|
+ // 服务端错误,切换到下个区域调用。
|
|
|
+ if (response != null) {
|
|
|
+ if (500 == response.getStatusCode()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (response.getBody() != null) {
|
|
|
+ if ("500".equals(response.getBody().getCode())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 正常返回
|
|
|
+ return lastResponse;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ if (i == endpoints.size() - 1) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return lastResponse;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static InitFaceVerifyResponse initFaceVerify(String endpoint, InitFaceVerifyRequest request)
|
|
|
+ throws Exception {
|
|
|
+ // 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
|
|
|
+ // 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
|
|
|
+ // 本示例通过阿里云Credentials工具从环境变量中读取AccessKey,来实现API访问的身份验证。如何配置环境变量,请参见https://help.aliyun.com/document_detail/378657.html。
|
|
|
+ Config credentialConfig = new Config();
|
|
|
+ credentialConfig.setType("access_key");
|
|
|
+ //使用阿里云RAM用户的AK。最小的权限
|
|
|
+ credentialConfig.setAccessKeyId("LTAI5tCwkQRjuJYzbohaveEz");
|
|
|
+ credentialConfig.setAccessKeySecret("8VW0H8GL7hmm5td8cYxO49FcVLPzOj");
|
|
|
+ credentialConfig.setEndpoint(endpoint);
|
|
|
+ Client client = new Client(credentialConfig);
|
|
|
+ // 设置http代理。
|
|
|
+ //config.setHttpProxy("http://xx.xx.xx.xx:xxxx");
|
|
|
+ // 设置https代理。
|
|
|
+ //config.setHttpsProxy("https://xx.xx.xx.xx:xxxx");
|
|
|
+
|
|
|
+
|
|
|
+ // 创建RuntimeObject实例并设置运行参数。
|
|
|
+ RuntimeOptions runtime = new RuntimeOptions();
|
|
|
+ runtime.readTimeout = 10000;
|
|
|
+ runtime.connectTimeout = 10000;
|
|
|
+
|
|
|
+ return client.initFaceVerifyWithOptions(request, runtime);
|
|
|
+ }
|
|
|
}
|