package com.ruoyi.company.utils;
|
|
import cn.hutool.crypto.digest.DigestUtil;
|
import com.alibaba.fastjson2.JSONObject;
|
import com.aliyun.cloudauth20190307.Client;
|
import com.aliyun.cloudauth20190307.models.Id2MetaVerifyResponse;
|
import com.aliyun.tea.TeaException;
|
import com.aliyun.teaopenapi.models.Config;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Component;
|
|
/**
|
* @author mitao
|
* @date 2025/1/21
|
*/
|
@Slf4j
|
@Component
|
public class AliyunCloudAuthUtil {
|
@Value("${aliyun.accessKeyId}")
|
private String accessKeyId;
|
@Value("${aliyun.accessKeySecret}")
|
private String accessKeySecret;
|
private Client createClient() throws Exception {
|
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
|
Config config = new com.aliyun.teaopenapi.models.Config()
|
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
.setAccessKeyId(accessKeyId)
|
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
.setAccessKeySecret(accessKeySecret);
|
// Endpoint 请参考 https://api.aliyun.com/product/Cloudauth
|
config.endpoint = "cloudauth.aliyuncs.com";
|
return new Client(config);
|
}
|
public Boolean verifyIdCard(String userName,String idCardNo) {
|
//姓名第一个字密文+ 姓名其他部分明文。
|
String firstCharacter = userName.substring(0, 1);
|
userName = DigestUtil.md5Hex(firstCharacter)+userName.substring(1);
|
log.info("姓名:"+userName);
|
//身份证号前6位(明文)+出生年月日(密文)+身份证号后4位(明文)。
|
idCardNo = idCardNo.substring(0,6)+ DigestUtil.md5Hex(idCardNo.substring(6, 14)) +idCardNo.substring(14);
|
log.info("身份证号:"+idCardNo);
|
Client client = null;
|
try {
|
client = createClient();
|
} catch (Exception e) {
|
throw new RuntimeException(e);
|
}
|
com.aliyun.cloudauth20190307.models.Id2MetaVerifyRequest id2MetaVerifyRequest = new com.aliyun.cloudauth20190307.models.Id2MetaVerifyRequest()
|
.setParamType("md5")
|
.setUserName(userName)
|
.setIdentifyNum(idCardNo);
|
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
try {
|
// 复制代码运行请自行打印 API 的返回值
|
Id2MetaVerifyResponse id2MetaVerifyResponse = client.id2MetaVerifyWithOptions(id2MetaVerifyRequest, runtime);
|
log.info(JSONObject.toJSONString(id2MetaVerifyResponse));
|
if (id2MetaVerifyResponse.getStatusCode().equals(200) && ("1").equals(id2MetaVerifyResponse.getBody().getResultObject().getBizCode())) {
|
return true;
|
}
|
} catch (TeaException error) {
|
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
// 错误 message
|
System.out.println(error.getMessage());
|
// 诊断地址
|
System.out.println(error.getData().get("Recommend"));
|
com.aliyun.teautil.Common.assertAsString(error.message);
|
} catch (Exception _error) {
|
TeaException error = new TeaException(_error.getMessage(), _error);
|
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
// 错误 message
|
System.out.println(error.getMessage());
|
}
|
return false;
|
}
|
}
|