mitao
2025-01-21 15d6d20c8f5e36f79b420cf6ebdcbbf41cb2cdc8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return false;
    }
}