校验身份证,校验第一步:长度为15位或者18位,校验完成后,可以直接调用第三方的接口,校验姓名身份证是否一致
/**
* 身份证件校验
*
* @param param{姓名,手机,证件号}
* @return
* @throws IOException
* @throws ClientProtocolException
*/
public boolean ifValid(Map<String, Object> param) {
log.getLogger("info_s").info("ifValid-start({})", param);
if ((param.get("idName") == null || param.get("idMobileNumber") == null || param.get("idNumber") == null))
return false;
String idName = param.get("idName").toString();
String idNumber = param.get("idNumber").toString();
String idMobileNumber = param.get("idMobileNumber").toString();
if (!isChinese(idName) || idName.length() > 8)
return false;
param.put("name", removeEmoji(removeBlank(idName)));
param.put("idCard", removeEmoji(removeBlank(idNumber)));
param.put("mobileNumber", removeEmoji(removeBlank(idMobileNumber)));
// 调用身份证验证API
HttpClient httpClient = HttpClientBuilder.create().build();
ObjectMapper mapper = new ObjectMapper();
HttpGet httpGet = null;
HttpResponse response = null;
HttpEntity entity = null;
String responseString = null;
httpGet = new HttpGet("http://op.juhe.cn/idcard/query?key=55e47b9cfee9078b1d527aa2eccb85a9&idcard=" + param.get("idCard").toString() + "&realname=" + param.get("name").toString());
try {
response = httpClient.execute(httpGet);
entity = response.getEntity();
responseString = EntityUtils.toString(entity, "UTF-8");
log.getLogger("info_s").info("ifValid({}):{}", param, responseString);
JsonNode node = mapper.readTree(responseString);
if (0 != (node.get("error_code").asInt())) {
return false;
}
else {
if (1 == node.get("result").get("res").asInt()) {
return true;
}
else {
return false;
}
}
}
catch (ClientProtocolException e) {
log.getLogger("info_s").error("ifValid-fail({}),result({})", param, e.getMessage());
}
catch (IOException e) {
log.getLogger("info_s").error("ifValid-fail({}),result({})", param, e.getMessage());
}
log.getLogger("info_s").info("ifValid-end({}), result({})", param, true);
return true;
}