|
@@ -10,25 +10,20 @@ import cn.hutool.http.HttpUtil;
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.aliyun.cloudauth20190307.Client;
|
|
|
-import com.aliyun.cloudauth20190307.models.InitFaceVerifyRequest;
|
|
|
-import com.aliyun.cloudauth20190307.models.InitFaceVerifyResponse;
|
|
|
-import com.aliyun.cloudauth20190307.models.InitFaceVerifyResponseBody;
|
|
|
+import com.aliyun.cloudauth20190307.models.*;
|
|
|
import com.aliyun.teaopenapi.models.Config;
|
|
|
import com.aliyun.teautil.models.RuntimeOptions;
|
|
|
+import com.ruoyi.common.config.RuoYiConfig;
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
import com.ruoyi.common.core.domain.SysUserIdcardVo;
|
|
|
|
|
|
import javax.crypto.Cipher;
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
import java.io.IOException;
|
|
|
-import java.io.UnsupportedEncodingException;
|
|
|
import java.net.URLEncoder;
|
|
|
-import java.nio.file.Files;
|
|
|
-import java.nio.file.Paths;
|
|
|
import java.util.*;
|
|
|
|
|
|
-import static com.ruoyi.common.constant.Constants.BACK;
|
|
|
-import static com.ruoyi.common.constant.Constants.FRONT;
|
|
|
+import static com.ruoyi.common.constant.Constants.*;
|
|
|
|
|
|
public class IdCardUtil {
|
|
|
/**
|
|
@@ -344,75 +339,6 @@ public class IdCardUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- /**
|
|
|
- * 人脸识别
|
|
|
- *
|
|
|
- * @throws IOException
|
|
|
- */
|
|
|
- public static AjaxResult sample(SysUserIdcardVo sysUserIdcardVo) {
|
|
|
- String image = sysUserIdcardVo.getImage();
|
|
|
- String idCardNumber = sysUserIdcardVo.getIdCard();
|
|
|
- String name = sysUserIdcardVo.getRealName();
|
|
|
- if (StringUtils.isEmpty(image) || StringUtils.isEmpty(idCardNumber) || StringUtils.isEmpty(name)) {
|
|
|
- return AjaxResult.error("参数不完整");
|
|
|
- }
|
|
|
- // image 可以通过 getFileContentAsBase64("C:\fakepath\1675840208844.png") 方法获取,如果Content-Type是application/x-www-form-urlencoded时,第二个参数传true
|
|
|
- HashMap<String, Object> paramMap = new HashMap<>();
|
|
|
- paramMap.put("access_token", getAccessToken("OmTDmo9hv4wll1ReYiRNJEfz", "vqnio9p7C17nbrkVar0MrHU4KOz2OXOr"));
|
|
|
- paramMap.put("image", getFileContentAsBase64(image, false));
|
|
|
- paramMap.put("image_type", "BASE64");
|
|
|
- paramMap.put("id_card_number", idCardNumber);
|
|
|
- paramMap.put("name", name);
|
|
|
- paramMap.put("spoofing_control", "NORMAL");
|
|
|
- paramMap.put("quality_control", "NORMAL");
|
|
|
- //返回值
|
|
|
- String post = HttpUtil.post("https://aip.baidubce.com/rest/2.0/face/v4/mingjing/verify", paramMap);
|
|
|
- /**
|
|
|
- * {
|
|
|
- * "log_id": 1370579072568000512,
|
|
|
- * "result": {
|
|
|
- * "score": 40.884,
|
|
|
- * "verify_status": 0
|
|
|
- * },
|
|
|
- * "dec_image": "/9j/4AAQSkZJRgABAgAAAQABAAD",
|
|
|
- * "risk_level": "3",
|
|
|
- * "risk_tag": [
|
|
|
- * "若判断为有风险,则会有风险标签json 数组告知风险类型,如:general_inject"
|
|
|
- * ]
|
|
|
- * }
|
|
|
- */
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(post);
|
|
|
- Map<String, Object> map = new HashMap<>();
|
|
|
- return AjaxResult.success(map);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取文件base64编码
|
|
|
- *
|
|
|
- * @param path 文件路径
|
|
|
- * @param urlEncode 如果Content-Type是application/x-www-form-urlencoded时,传true
|
|
|
- * @return base64编码信息,不带文件头
|
|
|
- * @throws IOException IO异常
|
|
|
- */
|
|
|
- static String getFileContentAsBase64(String path, boolean urlEncode) {
|
|
|
- byte[] b = new byte[0];
|
|
|
- try {
|
|
|
- b = Files.readAllBytes(Paths.get(path));
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- String base64 = Base64.getEncoder().encodeToString(b);
|
|
|
- if (urlEncode) {
|
|
|
- try {
|
|
|
- base64 = URLEncoder.encode(base64, "utf-8");
|
|
|
- } catch (UnsupportedEncodingException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- return base64;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 获取百度开放平台的AccessToken
|
|
|
* client_id: 必须参数,应用的API Key;
|
|
@@ -444,15 +370,18 @@ public class IdCardUtil {
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
public static AjaxResult ocrSampleAliYun(SysUserIdcardVo sysUserIdcardVo) {
|
|
|
-
|
|
|
- return AjaxResult.success(initFaceVerify(sysUserIdcardVo));
|
|
|
+ InitFaceVerifyResponseBody initFaceVerifyResponseBody = initFaceVerify(sysUserIdcardVo);
|
|
|
+ if (!initFaceVerifyResponseBody.getCode().equals(SUCCESS_CODE)) {
|
|
|
+ return AjaxResult.error(initFaceVerifyResponseBody.getMessage());
|
|
|
+ }
|
|
|
+ return AjaxResult.success("操作成功", initFaceVerifyResponseBody.getResultObject().getCertifyId());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 阿里云调用人脸识别
|
|
|
* 每次开始认证前通过本接口获取CertifyId,用来串联认证请求中的各个接口。
|
|
|
*/
|
|
|
- public static Object initFaceVerify(SysUserIdcardVo sysUserIdcardVo) {
|
|
|
+ public static InitFaceVerifyResponseBody initFaceVerify(SysUserIdcardVo sysUserIdcardVo) {
|
|
|
InitFaceVerifyRequest request = new InitFaceVerifyRequest();
|
|
|
// 请输入场景ID+L。1000009979场景id固定值
|
|
|
request.setSceneId(1000009979L);
|
|
@@ -494,29 +423,23 @@ public class IdCardUtil {
|
|
|
|
|
|
// 推荐,支持服务路由。
|
|
|
InitFaceVerifyResponse response = initFaceVerifyAutoRoute(request);
|
|
|
-
|
|
|
// 不支持服务自动路由。
|
|
|
//InitFaceVerifyResponse response = initFaceVerify("cloudauth.cn-shanghai.aliyuncs.com", request);
|
|
|
-
|
|
|
- response.getBody().getRequestId();
|
|
|
- String certifyId = response.getBody().getResultObject().getCertifyId();
|
|
|
-
|
|
|
InitFaceVerifyResponseBody.InitFaceVerifyResponseBodyResultObject resultObject = response.getBody().getResultObject();
|
|
|
-
|
|
|
System.out.println(response.getBody().getRequestId());
|
|
|
- System.out.println("人脸识别跳转URL"+resultObject.getCertifyUrl());
|
|
|
+ System.out.println("人脸识别跳转URL" + resultObject.getCertifyUrl());
|
|
|
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 resultObject;
|
|
|
+ return response.getBody();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 阿里云调用人脸识别H5
|
|
|
* 每次开始认证前通过本接口获取CertifyId,用来串联认证请求中的各个接口。
|
|
|
*/
|
|
|
- public static InitFaceVerifyResponseBody.InitFaceVerifyResponseBodyResultObject initFaceVerifyH5(SysUserIdcardVo sysUserIdcardVo) {
|
|
|
+ public static InitFaceVerifyResponseBody initFaceVerifyH5(SysUserIdcardVo sysUserIdcardVo) {
|
|
|
InitFaceVerifyRequest request = new InitFaceVerifyRequest();
|
|
|
// 请输入场景ID+L。1000009979场景id固定值
|
|
|
request.setSceneId(1000009979L);
|
|
@@ -559,19 +482,17 @@ public class IdCardUtil {
|
|
|
|
|
|
// 推荐,支持服务路由。
|
|
|
InitFaceVerifyResponse response = initFaceVerifyAutoRoute(request);
|
|
|
-
|
|
|
// 不支持服务自动路由。
|
|
|
//InitFaceVerifyResponse response = initFaceVerify("cloudauth.cn-shanghai.aliyuncs.com", request);
|
|
|
-
|
|
|
InitFaceVerifyResponseBody.InitFaceVerifyResponseBodyResultObject resultObject = response.getBody().getResultObject();
|
|
|
-
|
|
|
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 resultObject;
|
|
|
+ return response.getBody();
|
|
|
}
|
|
|
+
|
|
|
private static InitFaceVerifyResponse initFaceVerifyAutoRoute(InitFaceVerifyRequest request) {
|
|
|
// 第一个为主区域Endpoint,第二个为备区域Endpoint。
|
|
|
List<String> endpoints = Arrays.asList("cloudauth.cn-shanghai.aliyuncs.com", "cloudauth.cn-beijing.aliyuncs.com");
|
|
@@ -631,4 +552,164 @@ public class IdCardUtil {
|
|
|
|
|
|
return client.initFaceVerifyWithOptions(request, runtime);
|
|
|
}
|
|
|
+
|
|
|
+ /*获取认证结果返回值*/
|
|
|
+
|
|
|
+ /**
|
|
|
+ * {
|
|
|
+ * //是否为攻击:攻击为T,非攻击为F。
|
|
|
+ * "faceAttack": "T",
|
|
|
+ * // 是否有脸部遮挡:有脸部遮挡为T,否则为F。
|
|
|
+ * "faceOcclusion": "F",
|
|
|
+ * // 认证的照片信息。
|
|
|
+ * "facialPictureFront": {
|
|
|
+ * // 人脸攻击分。
|
|
|
+ * "faceAttackScore": 0.00008597839769208804,
|
|
|
+ * // 人脸和身份证照片比对分(仅当product code=ID_PLUS时,认证完成返回)。
|
|
|
+ * "idCardVerifyScore": 50.99797734769861,
|
|
|
+ * // 照片存放客户上海区域的OSS里,此为照片的bucket名。
|
|
|
+ * "ossBucketName": "cn-shanghai-aliyun-cloudauth-1260051251634779",
|
|
|
+ * // 照片存放客户上海区域的OSS里,此为照片的文件名。
|
|
|
+ * "ossObjectName": "verify/1260051251634779/03a081bd96328aedf83f635f39a50c57_0.jpeg",
|
|
|
+ * // 照片https或htttp地址,有效期15分钟,从查询时开始计时。
|
|
|
+ * "pictureUrl": "http://cn-shanghai-aliyun-cloudauth-1260051251634779.oss-cn-shanghai.aliyuncs.com/verify/1260051251634779/03a081bd96328aedf83f635f39a50c57_0.jpeg?Expires=1625371140&OSSAccessKeyId=STS.NTX1ngfr6Acg2Pmnn2RYM****&Signature=Hw5BF9WxJs6wI68IxKs41cxCU8****&security-token=CAISjgJ1q6Ft5B2yfSjIr5ftetTTi60X9qGMMHbcim5nXtZhu7GT1Dz2IH1PdXFgA%2Bgds%2Fswmm5U7vgalrkqEcEdHRGdN5YpsM8LrlzwO1h2TGRsq%2B5qsoasPETOITyZtZagToeUZdfZfejXGDKgvyRvwLz8WCy%2FVli%2BS%2FOggoJmadJlNWvRL0AxZrFsKxBltdUROFbIKP%2BpKWSKuGfLC1dysQcO4gEWq4bHm5fCskKD1Qenk7FO%2B9uuc6LJNZc8YM1NNP6ux%2FFze6b71ypd1gNH7q8ejtYfpGyW5oHHWwIMvErYbbaMrIV1WwZ9Z7knHaVAq%2BXwnOBkuuXYnIns0BdLMuZOSD7YQI2wYWEwgBcxt78agAF%2FHZbelNLYWtipAu5X5oG1t22SqIr1p3TMK5FrjDIVeWOppcxeRXtU%2BjR7hGcwd25scGhiraoxTXV3sxw%2F6dhFSswQ37O4j%2B%2FrCPtiGauqn2ZMdMj%2FvYwKb6KmpQqa%2BtR%2F9cxhS6hoIQRq0SUIfyXl5ZUiZbTjW22iwuX%2BPwzVCw%3D%3D",
|
|
|
+ * // 视频存放客户上海区域的OSS里,此为视频的文件名(仅当客户端设置视频参数时返回)。
|
|
|
+ * "ossVerifyVideoObjectName": "verify/1260051251634779/03a081bd96328aedf83f635****_verifyvideo_dcb7.mov",
|
|
|
+ * // 视频https或htttp地址,有效期15分钟,从查询时开始计时(仅当客户端设置视频参数时返回)。
|
|
|
+ * "verifyVideoUrl": "http://cn-shanghai-aliyun-cloudauth-1260051251634779.oss-cn-shanghai.aliyuncs.com/verify/1260051251634779/03a081bd96328aedf83f635f39a50c57__verifyvideo_dcb7.mov?Expires=1625371140&OSSAccessKeyId=STS.NTX1ngfr6Acg2Pmnn2RYM****&Signature=Hw5BF9WxJs6wI68IxKs41cxCU8****&security-token=CAISjgJ1q6Ft5B2yfSjIr5ftetTTi60X9qGMMHbcim5nXtZhu7GT1Dz2IH1PdXFgA%2Bgds%2Fswmm5U7vgalrkqEcEdHRGdN5YpsM8LrlzwO1h2TGRsq%2B5qsoasPETOITyZtZagToeUZdfZfejXGDKgvyRvwLz8WCy%2FVli%2BS%2FOggoJmadJlNWvRL0AxZrFsKxBltdUROFbIKP%2BpKWSKuGfLC1dysQcO4gEWq4bHm5fCskKD1Qenk7FO%2B9uuc6LJNZc8YM1NNP6ux%2FFze6b71ypd1gNH7q8ejtYfpGyW5oHHWwIMvErYbbaMrIV1WwZ9Z7knHaVAq%2BXwnOBkuuXYnIns0BdLMuZOSD7YQI2wYWEwgBcxt78agAF%2FHZbelNLYWtipAu5X5oG1t22SqIr1p3TMK5FrjDIVeWOppcxeRXtU%2BjR7hGcwd25scGhiraoxTXV3sxw%2F6dhFSswQ37O4j%2B%2FrCPtiGauqn2ZMdMj%2FvYwKb6KmpQqa%2BtR%2F9cxhS6hoIQRq0SUIfyXl5ZUiZbTjW22iwuX%2BPwzVCw%3D%3D",
|
|
|
+ * // 活体人脸质量分数。
|
|
|
+ * "qualityScore": 99.93476867675781,
|
|
|
+ * // 人脸和公安比对分数,阈值可参考下表详细说明。
|
|
|
+ * "verifyScore": 57.678396649466755
|
|
|
+ * },
|
|
|
+ * // 身份证OCR信息(仅当product code=ID_PLUS时,认证完成返回OCR识别的信息和图片)。
|
|
|
+ * "ocrIdCardInfo": {
|
|
|
+ * "address": "XXX省XX市XXX区XX街道X号",
|
|
|
+ * "authority": "XXX公安局XXX分局",
|
|
|
+ * "birth": "20010213",
|
|
|
+ * "certName": "张先生",
|
|
|
+ * "certNo": "4****************X",
|
|
|
+ * "endDate": "20450523",
|
|
|
+ * "nationality": "汉",
|
|
|
+ * "sex": "男",
|
|
|
+ * "startDate": "20250523"
|
|
|
+ * },
|
|
|
+ * "ocrPictureFront": {
|
|
|
+ * // 身份证OCR照片存放客户上海区域的OSS里,此为身份证OCR照片的Bucket名。
|
|
|
+ * "ossBucketName": "cn-shanghai-aliyun-cloudauth-1234567890",
|
|
|
+ * // 身份证OCR照片存放客户上海区域的OSS里,此为身份证OCR照片人脸面的文件名。
|
|
|
+ * "ossIdFaceObjectName": "verify/1234567890/f7ed1ef80ad1234fdsd95c66d83340cd_ocridface_b749.jpeg",
|
|
|
+ * // 身份证OCR照片人脸面的https或http地址,有效期15分钟,从查询时开始计时。
|
|
|
+ * "ossIdFaceUrl": "http://cn-shanghai-aliyun-cloudauth-1234567890.oss-cn-shanghai.aliyuncs.com/verify/1234567890/f7ed1ef80ad1234fdsd95c66d83340cd_ocridface_b749.jpeg?Expires=1625371165&OSSAccessKeyId=STS.NTGterbRtcVY7ykxixaQ6****&Signature=SqZoqBch%2BsdJefkabdaIJrPJWqtD%2FlmF****&security-token=CAISjdfgeJ1q6Ft5B2yfSjIr5fyP9%2FGj41V1JSyNV%2FanGktbd4aqInAmjz2IH1PdXFgA%2Bgds%2Fswmm5U7vgalrkqEcEdHRGdN5YpsM8LrlzwO30McmRsq%2B5qsoasPETOITyZtZagToeUZdfZfejXGDKgvyRvwLz8WCy%2FVli%2BS%2FOggoJmadJlNWvRL0AxZrFsKxBltdUROFbIKP%2BpKWSKuGfLC1dysQcO4gEWq4bHm5fCskKD1Qenk7FO%2B9uuc6LJNZc8YM1NNP6ux%2FFze6b71ypd1gNH7q8ejtYfpGyW5oHHWwIMvErYbbaMrIV1WwZ9Z7knHaVAq%2BXwnOBkuuXYnIns0BdLMuZOSD7YQI2wYWEwgBcxt78agAGe2a1NFO2XabhICqmUeN%2FLV25GuhaexEjjGL4sDC2VWjasWBmKzlXTQ6mHZoI9IhswopdGLLlWPC5ONnLNASWQEObAM28FAViS76clUdE8YVCMyU%2BuyFqLGeRNx4NjcPa%2FlOa9wsxjg4LwlfHKeXA3g0X%2BQQcRAqFc2j093K2r6A%3D%3D",
|
|
|
+ * // 身份证OCR照片存放客户上海区域的OSS里,此为身份证OCR照片国徽面的文件名。
|
|
|
+ * "ossIdNationalEmblemObjectName": "verify/1234567890/f7ed1ef80ad1234fdsd95c66d83340cd_ocridnationalemblem_a3hf.jpeg",
|
|
|
+ * // 身份证OCR照片国徽面的https或http地址,有效期15分钟,从查询时开始计时。
|
|
|
+ * "ossIdNationalEmblemUrl": "http://cn-shanghai-aliyun-cloudauth-1234567890.oss-cn-shanghai.aliyuncs.com/verify/1234567890/f7ed1ef80ad1234fdsd95c66d83340cd_ocridnationalemblem_a3hf.jpeg?Expires=1625371365&OSSAccessKeyId=STS.NTEz8oi35qJpxxX7no5gpGd****&Signature=wnHjNu3333ddW3zNMup1r4ovgrw6xJ0****&security-token=CAISjgJ1q6Ft5B2yfSjIr5fwMYLbhOwUxoibel7p0246OehcqLL%2BpTz2IH1PdXFgA%2Bgds%2Fswmm5U7vgalrkqEcEdHRGdN5YpsM8LrlzwO34PcmRsq%2B5qsoasPETOITyZtZagToeUZdfZfejXGDKgvyRvwLz8WCy%2FVli%2BS%2FOggoJmadJlNWvRL0AxZrFsKxBltdUROFbIKP%2BpKWSKuGfLC1dysQcO4gEWq4bHm5fCskKD1Qenk7FO%2B9uuc6LJNZc8YM1NNP6ux%2FFze6b71ypd1gNH7q8ejtYfpGyW5oHHWwIMvErYbbaMrIV1WwZ9Z7knHaVAq%2BXwnOBkuuXYnIns0BdLMuZOSD7YQI2wYWEwgBcxt78agAFThVhIcXH8zQiqbYVkTCHpZVRmDHH0mZ%2FMNuGikqSWdK3%2FhaSEvxmO1wyLH9E7mFzoKJErtxTCs453H6PYHO8H3pKOpKgZ0PfWTpJ8OEIJm26fKph%2F%2F3usSUBx5h0Ob17e2XB4BffkOJKQQiU3vXqoOZ6Vlyf7QpEJSvUMXXSKSg%3D%3D"
|
|
|
+ * }
|
|
|
+ * }
|
|
|
+ */
|
|
|
+ public static AjaxResult getDescribeFaceVerify(String certifyId) {
|
|
|
+ // 通过以下代码创建API请求并设置参数。
|
|
|
+ DescribeFaceVerifyRequest request = new DescribeFaceVerifyRequest();
|
|
|
+ // 请输入场景ID+L。
|
|
|
+ request.setSceneId(1000009979L);
|
|
|
+ //认证后返回的 实人认证唯一标识。
|
|
|
+ request.setCertifyId(certifyId);
|
|
|
+ // 推荐,支持服务路由。
|
|
|
+ DescribeFaceVerifyResponse response = describeFaceVerifyAutoRoute(request);
|
|
|
+ // 不支持服务自动路由。
|
|
|
+ //DescribeFaceVerifyResponse response = describeFaceVerify("cloudauth.cn-shanghai.aliyuncs.com", request);
|
|
|
+ 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().getPassed());
|
|
|
+ System.out.println(
|
|
|
+ response.getBody().getResultObject() == null ? null : response.getBody().getResultObject().getSubCode());
|
|
|
+ System.out.println(
|
|
|
+ response.getBody().getResultObject() == null ? null
|
|
|
+ : response.getBody().getResultObject().getIdentityInfo());
|
|
|
+ System.out.println(
|
|
|
+ response.getBody().getResultObject() == null ? null
|
|
|
+ : response.getBody().getResultObject().getDeviceToken());
|
|
|
+ System.out.println(
|
|
|
+ response.getBody().getResultObject() == null ? null
|
|
|
+ : response.getBody().getResultObject().getMaterialInfo());
|
|
|
+ if (!response.getBody().getCode().equals(SUCCESS_CODE)) {
|
|
|
+ return AjaxResult.error(response.getBody().getCode(), response.getBody().getMessage());
|
|
|
+ }
|
|
|
+ String materialInfo = response.getBody().getResultObject().getMaterialInfo();
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(materialInfo);
|
|
|
+ JSONObject facialPictureFront = JSONObject.parseObject(jsonObject.get("facialPictureFront").toString());
|
|
|
+ String pictureUrl = facialPictureFront.get("pictureUrl").toString();
|
|
|
+ //把图片存放在服务器上
|
|
|
+ // 上传文件路径
|
|
|
+ String filePath = RuoYiConfig.getUploadPath()+"/ocr";
|
|
|
+ HttpUtil.downloadFile(pictureUrl,filePath);
|
|
|
+ return AjaxResult.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取认证结果
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static DescribeFaceVerifyResponse describeFaceVerifyAutoRoute(DescribeFaceVerifyRequest request) {
|
|
|
+ // 第一个为主区域Endpoint,第二个为备区域Endpoint。
|
|
|
+ List<String> endpoints = Arrays.asList("cloudauth.cn-shanghai.aliyuncs.com", "cloudauth.cn-beijing.aliyuncs.com");
|
|
|
+ DescribeFaceVerifyResponse lastResponse = null;
|
|
|
+ for (int i = 0; i < endpoints.size(); i++) {
|
|
|
+ try {
|
|
|
+ DescribeFaceVerifyResponse response = describeFaceVerify(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) {
|
|
|
+ if (i == endpoints.size() - 1) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return lastResponse;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用单例模式优化性能
|
|
|
+ private static com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
|
|
|
+
|
|
|
+ private static DescribeFaceVerifyResponse describeFaceVerify(String endpoint, DescribeFaceVerifyRequest request)
|
|
|
+ throws Exception {
|
|
|
+ // 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
|
|
|
+ // 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
|
|
|
+ //本示例通过阿里云Credentials工具从环境变量中读取AccessKey,来实现API访问的身份验证。如何配置环境变量,请参见https://help.aliyun.com/document_detail/378657.html。
|
|
|
+
|
|
|
+ Config config = new Config();
|
|
|
+ config.setCredential(credentialClient);
|
|
|
+ config.setEndpoint(endpoint);
|
|
|
+ // 设置http代理。
|
|
|
+ //config.setHttpProxy("http://xx.xx.xx.xx:xxxx");
|
|
|
+ // 设置https代理。
|
|
|
+ //config.setHttpsProxy("http://xx.xx.xx.xx:xxxx");
|
|
|
+ Client client = new Client(config);
|
|
|
+
|
|
|
+ // 创建RuntimeObject实例并设置运行参数。
|
|
|
+ RuntimeOptions runtime = new RuntimeOptions();
|
|
|
+ runtime.readTimeout = 10000;
|
|
|
+ runtime.connectTimeout = 10000;
|
|
|
+
|
|
|
+ return client.describeFaceVerifyWithOptions(request, runtime);
|
|
|
+ }
|
|
|
}
|