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); } }