mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
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
package com.panzhihua.common.utlis;
 
import java.util.Base64;
 
import com.alibaba.fastjson.JSONObject;
import com.panzhihua.common.exceptions.ServiceException;
 
import lombok.extern.slf4j.Slf4j;
 
/**
 * @program: springcloud_k8s_panzhihuazhihuishequ
 * @description: 实名认证工具
 * @author: huang.hongfa weixin hhf9596 qq 959656820
 * @create: 2021-01-08 09:45
 **/
@Slf4j
public class RealNameUtil {
    private static final String CLIENT_ID = "2092261934651932672";
    private static final String SECRET = "de1c0dbb-9197-4724-9cc2-aa94e30a072f";
    private static final String URL = "http://118.121.56.56:18080/users/auth/policeMatch";
 
    /**
     * 实名认证
     * 
     * @param idcard
     *            身份证
     * @param name
     *            名字
     * @return 认证结果 true false
     */
    public static boolean authentication(String idcard, String name) {
        boolean result = false;
        String headerkey = "Authorization";
        String headervalue = "Basic ";
        String basicStr = CLIENT_ID + ":" + SECRET + ":" + System.currentTimeMillis();
        String encodeToString = Base64.getEncoder().encodeToString(basicStr.getBytes());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("idNumber", idcard);
        jsonObject.put("realName", name);
        String param = jsonObject.toJSONString();
        log.info("请求公安实名认证接口,请求地址:" + URL);
        log.info("请求公安实名认证接口,请求参数:" + param);
        log.info("请求公安实名认证接口,请求头参数:" + headervalue + encodeToString);
        String postByJson = HttpClientUtil.sendPostByJson(URL, param, 2, headerkey, headervalue + encodeToString);
        log.info("请求公安实名认证接口,返回参数:" + postByJson);
        if (postByJson != null && StringUtils.isNotEmpty(postByJson)) {
            JSONObject obj = JSONObject.parseObject(postByJson);
            if (obj != null) {
                int code = JSONObject.parseObject(postByJson).getIntValue("code");
                if (0 == code) {
                    result = true;
                }
            }
        } else {
            throw new ServiceException("公安实名认证接口超时,请重试");
        }
        return result;
    }
 
    public static void main(String[] args) {
        boolean b = RealNameUtil.authentication("340823199112282557", "黄宏发");
        System.out.println(b);
    }
 
}