Air
2024-11-04 cb3b3801255082c53145bd752230339eae5d3e5a
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.nova.sankuai.domain.api.xiaobaiyouka;
 
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nova.sankuai.domain.entity.CustomerUser;
import com.nova.sankuai.security.HttpClientUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @Author Lilinhong
 * @Date 2022/7/26 9:56
 */
@Component
public class XiaoBaiYouKaUtil {
 
    @Value("${xiaobaiyouka.url}")
    private String url;
 
    @Value("${xiaobaiyouka.private-key}")
    private String privateKey;
 
    @Value("${xiaobaiyouka.channel-name}")
    private String channelName;
 
    private static final String KEY_ALGORITHM = "RSA";
    public static final String DEFAULT_ALGORITHM = "MD5withRSA";
    private static final String DEFAULT_ENCODING = "UTF-8";
 
    private static final Logger logger = LoggerFactory.getLogger("capitalLogger");
 
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
 
    public static String sign(String privateKey, String dataSrc) {
        return sign(privateKey, dataSrc, DEFAULT_ALGORITHM);
    }
 
    /**
     * 签名
     *
     * @param privateKey:私钥
     * @param dataSrc:源
     * @param algorithm:签名算法
     * @return 签名
     */
    public static String sign(String privateKey, String dataSrc, String algorithm) {
        try {
            PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
            KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
            PrivateKey myPrivateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
            Signature signature = Signature.getInstance(algorithm);
            signature.initSign(myPrivateKey);
            signature.update(dataSrc.getBytes(DEFAULT_ENCODING));
            byte[] signed = signature.sign();
            return Base64.encodeBase64String(signed);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 签名验证
     *
     * @param publicKey:公钥
     * @param dataSrc:源
     * @param sign:签名结果串
     * @return 验签结果
     */
    public static boolean checkSign(String publicKey, String dataSrc, String sign) throws Exception {
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
                Base64.decodeBase64(publicKey));
        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
        PublicKey myPublicKey = keyFactory.generatePublic(x509EncodedKeySpec);
        byte[] signed = Base64.decodeBase64(sign);
        Signature signature = Signature
                .getInstance(DEFAULT_ALGORITHM);
        signature.initVerify(myPublicKey);
        signature.update(dataSrc.getBytes(DEFAULT_ENCODING));
        return signature.verify(signed);
    }
 
    public XiaoBaiYouKaCheckUserResponse checkUser(CustomerUser customerUser) {
        // 请求体
        Map<String, Object> body = new HashMap<>(16);
        body.put("channel_name", channelName);
        body.put("mobile_md5", customerUser.getPhoneMd5());
        String timestamp = Long.toString(System.currentTimeMillis());
        body.put("timestamp", timestamp);
        String signString = "channelName=" + channelName + "&mobileNumber=" + customerUser.getPhoneMd5()
                + "&timestamp=" + timestamp;
        try {
            String sign = sign(privateKey, signString);
            body.put("sign", sign);
        } catch (Exception e) {
            logger.info("手机号为:" + customerUser.getPhone() + ",小白优卡撞库签名失败");
            e.printStackTrace();
            return null;
        }
        String paramString = null;
        logger.info("手机号为:" + customerUser.getPhone() + ",小白优卡撞库原始数据->:{}", body);
        try {
            paramString = OBJECT_MAPPER.writeValueAsString(body);
        } catch (JsonProcessingException e) {
            logger.info("手机号为:" + customerUser.getPhone() + ",小白优卡撞库入参序列化后数据错误");
            e.printStackTrace();
            return null;
        }
        String response = null;
        try {
            response = HttpClientUtil.getInstance().postJsonData(url + "/jointLogon/checkUser", paramString);
            logger.info("手机号为:" + customerUser.getPhone() + ",小白优卡撞库原始返回数据->:{}", response);
        } catch (IOException e) {
            logger.error("手机号为:" + customerUser.getPhone() + ",小白优卡撞库原始返回数据失败->:{}", response);
            e.printStackTrace();
            return null;
        }
        if (StringUtils.isEmpty(response)) {
            logger.error("手机号为:" + customerUser.getPhone() + ",小白优卡撞库返回数据失败为空");
            return null;
        }
        XiaoBaiYouKaCheckUserResponse xiaoBaiYouKaCheckUserResponse = null;
        try {
            xiaoBaiYouKaCheckUserResponse = JSON.parseObject(response, XiaoBaiYouKaCheckUserResponse.class);
        } catch (Exception e) {
            logger.error("手机号为:" + customerUser.getPhone() + ",小白优卡撞库原始返回数据转换失败->:{}", xiaoBaiYouKaCheckUserResponse);
            e.printStackTrace();
            return null;
        }
        return xiaoBaiYouKaCheckUserResponse;
    }
 
    public XiaoBaiYouKaRegisterResponse register(CustomerUser customerUser) {
        // 请求体
        Map<String, Object> body = new HashMap<>(16);
        body.put("channel_name", channelName);
        body.put("mobile", customerUser.getPhone());
        String timestamp = Long.toString(System.currentTimeMillis());
        body.put("timestamp", timestamp);
        String signString = "channelName=" + channelName + "&mobileNumber=" + customerUser.getPhone()
                + "&timestamp=" + timestamp;
        try {
            String sign = sign(privateKey, signString);
            body.put("sign", sign);
        } catch (Exception e) {
            logger.info("手机号为:" + customerUser.getPhone() + ",小白优卡联登签名失败");
            e.printStackTrace();
            return null;
        }
        String paramString = null;
        logger.info("手机号为:" + customerUser.getPhone() + ",小白优卡联登原始数据->:{}", body);
        try {
            paramString = OBJECT_MAPPER.writeValueAsString(body);
        } catch (JsonProcessingException e) {
            logger.info("手机号为:" + customerUser.getPhone() + ",小白优卡联登入参序列化后数据错误");
            e.printStackTrace();
            return null;
        }
        String response = null;
        try {
            response = HttpClientUtil.getInstance().postJsonData(url + "/jointLogon/register", paramString);
            logger.info("手机号为:" + customerUser.getPhone() + ",小白优卡联登原始返回数据->:{}", response);
        } catch (IOException e) {
            logger.error("手机号为:" + customerUser.getPhone() + ",小白优卡联登原始返回数据失败->:{}", response);
            e.printStackTrace();
            return null;
        }
        if (StringUtils.isEmpty(response)) {
            logger.error("手机号为:" + customerUser.getPhone() + ",小白优卡联登返回数据失败为空");
            return null;
        }
        XiaoBaiYouKaRegisterResponse xiaoBaiYouKaRegisterResponse = null;
        try {
            xiaoBaiYouKaRegisterResponse = JSON.parseObject(response, XiaoBaiYouKaRegisterResponse.class);
        } catch (Exception e) {
            logger.error("手机号为:" + customerUser.getPhone() + ",小白优卡联登原始返回数据转换失败->:{}", xiaoBaiYouKaRegisterResponse);
            e.printStackTrace();
            return null;
        }
        return xiaoBaiYouKaRegisterResponse;
    }
 
}