1
luodangjia
2025-01-22 895ed97195660b0196b61f5da626ac6d18e2068d
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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.cloudauth20221125.models.EntElementVerifyRequest;
import com.aliyun.cloudauth20221125.models.EntElementVerifyResponse;
import com.aliyun.cloudauth20221125.models.EntElementVerifyResponseBody;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import static com.aliyun.teautil.Common.assertAsString;
 
/**
 * @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);
    }
 
    /**
     * <b>description</b> :
     * <p>使用AK&amp;SK初始化账号Client</p>
     * @return Client
     *
     * @throws Exception
     */
    public static com.aliyun.cloudauth20221125.Client createBusinessLicenseClient() throws Exception {
        Config config = new Config()
                .setAccessKeyId("LTAI5tFSeci96NRF6p6UN69G")
                .setAccessKeySecret("25t5zmWkueoQd81Zka70uPKRPgaMiV");
        config.endpoint = "cloudauth.aliyuncs.com";
        return new com.aliyun.cloudauth20221125.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;
    }
 
    public static Boolean verifyBusinessLicense(String businessLicenseNo,String companyName,Long userId) {
        com.aliyun.cloudauth20221125.Client client = null;
        try {
            client = createBusinessLicenseClient();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        EntElementVerifyRequest entElementVerifyRequest = new EntElementVerifyRequest()
                .setLicenseNo(businessLicenseNo)
                .setEntName(companyName)
                .setSceneCode("000001")
                .setMerchantBizId(businessLicenseNo)
                .setMerchantUserId(String.valueOf(userId))
                .setInfoVerifyType("ENT_2META")
                .setUserAuthorization("1");
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            EntElementVerifyResponse entElementVerifyResponse = client.entElementVerifyWithOptions(entElementVerifyRequest, runtime);
            EntElementVerifyResponseBody body = entElementVerifyResponse.getBody();
            EntElementVerifyResponseBody.EntElementVerifyResponseBodyResult result = body.getResult();
            String bizCode = result.getBizCode();
            return "1".equals(bizCode);
        } catch (TeaException error) {
            // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            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"));
            assertAsString(error.message);
        }
        return false;
    }
 
    public static void main(String[] args) {
    }
 
}