2024-07-03 16:11:21 +08:00

367 lines
14 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.zhgd.xmgl.util;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.regex.Pattern;
/**
* @program: wisdomSite
* @description: 身份证工具类
* @author: Mr.Peng
* @create: 2021-03-10 10:04
**/
@Component
@Slf4j
public class IdCardUtils {
/**
* 大陆地区地域编码最大值
**/
public static final int MAX_MAINLAND_AREACODE = 659004;
/**
* 大陆地区地域编码最小值
**/
public static final int MIN_MAINLAND_AREACODE = 110000;
/**
* 香港地域编码值
**/
public static final int HONGKONG_AREACODE = 810000; // 香港地域编码值
/**
* 台湾地域编码值
**/
public static final int TAIWAN_AREACODE = 710000;
/**
* 澳门地域编码值
**/
public static final int MACAO_AREACODE = 820000;
/**
* 数字正则
**/
public static final String regexNum = "^[0-9]*$";
/**
* 闰年生日正则
**/
public static final String regexBirthdayInLeapYear = "^((19[0-9]{2})|(200[0-9])|(201[0-5]))((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))$";
/**
* 平年生日正则
**/
public static final String regexBirthdayInCommonYear = "^((19[0-9]{2})|(200[0-9])|(201[0-5]))((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))$";
private static final HashSet<String> BLACK_SET = new HashSet<String>() {
private static final long serialVersionUID = 48136604486603324L;
{
add("111111111111111");
}
};
private static String tokenurl = "https://iam.cn-east-3.myhuaweicloud.com/v3/auth/tokens";
private static String username;
private static String domainname;
private static String password;
private static String projectid;
private static String endpoint;
/**
* 是否启用华为云账号功能
*/
private static Boolean enable;
/**
* 识别身份证信息
*
* @param imageurl 图片地址
* @param side true 正面(头像面) false 反面(国徽面)
* <p>
* 注意事项此处申请华为云必须在华为云上海一区开通身份证识别功能然后拷贝项目ID到此处进行识别
*/
public static String getIdCardInfo(String imageurl, boolean side) {
if (enable) {
// String JSONSTR = "{ \n \"auth\": { \n \"identity\": { \n \"methods\": [ \n \"password\" \n ], \n \"password\": { \n \"user\": { \n\"name\": \"" + username + "\", \n \"password\": \"" + password + "\", \n \"domain\": { \n \"name\": \"" + domainname + "\" \n } \n } \n} \n}, \n\"scope\": { \n \"project\": { \n\"name\": \"cn-east-3\" \n} \n} \n} \n}";
// String token = HttpRequest.postJson(tokenurl).body(String.valueOf(JSONUtil.parse(JSONSTR))).execute().header("X-Subject-Token");
// String url = "https://ocr.cn-east-3.myhuaweicloud.com/v2/" + projectid + "/ocr/id-card";
// Map<String, Object> param = new HashMap<>(16);
// param.put("url", imageurl);
// param.put("side", side ? "front" : "back");
// String result = HttpRequest.postJson(url).header("X-Auth-Token", token).body(String.valueOf(JSONUtil.parse(param))).execute().body();
// log.info(result);
// return result;
// String tokenurl = "https://iam.cn-east-3.myhuaweicloud.com/v3/auth/tokens";
// String JSONSTR = "{ \n \"auth\": { \n \"identity\": { \n \"methods\": [ \n \"password\" \n ], \n \"password\": { \n \"user\": { \n\"name\": \"" + username + "\", \n \"password\": \"" + password + "\", \n \"domain\": { \n \"name\": \"" + username + "\" \n } \n } \n} \n}, \n\"scope\": { \n \"project\": { \n\"name\": \"cn-east-3\" \n} \n} \n} \n}";
//获取token
String tokenUrl = "https://iam." + endpoint + ".myhuaweicloud.com/v3/auth/tokens";
String JSONSTR = getTokenStr(domainname, username, password, endpoint);
String tokenBody = JSONUtil.toJsonStr(JSONSTR);
log.info("token_url{}body{}", tokenUrl, tokenBody);
String token = HttpRequest.post(tokenUrl).body(tokenBody).execute().header("x-subject-token");
log.info("response-token{}", token);
//获取身份证信息
String cardUrl = "https://ocr." + endpoint + ".myhuaweicloud.com/v2/" + projectid + "/ocr/id-card";
Map<String, Object> param = new HashMap<>(16);
param.put("url", imageurl);
param.put("side", side ? "front" : "back");
String cardBoday = JSONUtil.toJsonStr(param);
log.info("token_url{}body{}", cardUrl, cardBoday);
String result = HttpRequest.post(cardUrl).header("x-auth-token", token).body(cardBoday).execute().body();
log.info(result);
return result;
}
return null;
}
public static String getTokenStr(String domainname, String username, String passwrd, String endpoint) {
JSONObject domain = new JSONObject();
domain.put("name", domainname);
JSONObject user = new JSONObject();
user.put("name", username);
user.put("password", passwrd);
user.put("domain", domain);
JSONObject password = new JSONObject();
password.put("user", user);
List<String> methods = new ArrayList();
methods.add("password");
JSONObject identity = new JSONObject();
identity.put("methods", methods);
identity.put("password", password);
JSONObject project = new JSONObject();
project.put("name", endpoint);
JSONObject scope = new JSONObject();
scope.put("project", project);
JSONObject json = new JSONObject();
json.put("identity", identity);
json.put("scope", scope);
JSONObject data = new JSONObject();
data.put("auth", json);
return data.toJSONString();
}
/**
* 通过身份证号码获取出生日期(birthday)、年龄(age)、性别(sex)
*
* @param idCardNo 身份证号码
* @return 返回的出生日期格式1993-05-07 性别格式1男2女
*/
public static Map<String, String> getBirthdayAgeSex(String idCardNo) {
String birthday = "";
String age = "";
String sexCode = "";
int year = Calendar.getInstance().get(Calendar.YEAR);
char[] number = idCardNo.toCharArray();
boolean flag = true;
if (number.length == 15) {
for (int x = 0; x < number.length; x++) {
if (!flag) {
return new HashMap<String, String>();
}
flag = Character.isDigit(number[x]);
}
} else if (number.length == 18) {
for (int x = 0; x < number.length - 1; x++) {
if (!flag) {
return new HashMap<String, String>();
}
flag = Character.isDigit(number[x]);
}
}
if (flag && idCardNo.length() == 15) {
birthday = "19" + idCardNo.substring(6, 8) + "-"
+ idCardNo.substring(8, 10) + "-"
+ idCardNo.substring(10, 12);
sexCode = Integer.parseInt(idCardNo.substring(idCardNo.length() - 3, idCardNo.length())) % 2 == 0 ? "2" : "1";
age = (year - Integer.parseInt("19" + idCardNo.substring(6, 8))) + "";
} else if (flag && idCardNo.length() == 18) {
birthday = idCardNo.substring(6, 10) + "-"
+ idCardNo.substring(10, 12) + "-"
+ idCardNo.substring(12, 14);
sexCode = Integer.parseInt(idCardNo.substring(idCardNo.length() - 4, idCardNo.length() - 1)) % 2 == 0 ? "2" : "1";
age = (year - Integer.parseInt(idCardNo.substring(6, 10))) + "";
}
Map<String, String> map = new HashMap<String, String>();
map.put("birthday", birthday);
map.put("age", age);
map.put("sex", sexCode);
return map;
}
/**
* <p>
* 身份证格式强校验
* </p>
* <p>
* 1、号码的结构 公民身份号码是特征组合码,由十七位数字本体码和一位校验码组成。排列顺序从左至右依次为:六位数字地址码,
* 八位数字出生日期码,三位数字顺序码和一位数字校验码。
* </p>
* <p>
* 2、地址码(前六位数)表示编码对象常住户口所在县(市、旗、区)的行政区划代码按GB/T2260的规定执行。
* </p>
* <p>
* 3、出生日期码第七位至十四位表示编码对象出生的年、月、日按GB/T7408的规定执行年、月、日代码之间不用分隔符。
* </p>
* <p>
* 4、顺序码第十五位至十七位表示在同一地址码所标识的区域范围内对同年、同月、同日出生的人编定的顺序号 顺序码的奇数分配给男性,偶数分配给女性。
* </p>
* <p>
* 5、校验码第十八位数
* 1十七位数字本体码加权求和公式 S = Sum(Ai * Wi), i = 0, ... , 16 先对前17位数字的权求和
* Ai:表示第i位置上的身份证号码数字值 Wi:表示第i位置上的加权因子 Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4
* 2 2计算模 Y = mod(S, 11) 3通过模得到对应的校验码 Y: 0 1 2 3 4 5 6 7 8 9 10 校验码: 1 0
* X 9 8 7 6 5 4 3 2
* </p>
*/
public static final boolean strongVerifyIdNumber(String idNumber) {
if (StringUtils.isBlank(idNumber)) {
return false;
}
idNumber = idNumber.trim();
if (BLACK_SET.contains(idNumber)) {
return false;
}
if (!checkIdNumberRegex(idNumber)) {
return false;
}
if (!checkIdNumberArea(idNumber.substring(0, 6))) {
return false;
}
idNumber = convertFifteenToEighteen(idNumber);
if (!checkBirthday(idNumber.substring(6, 14))) {
return false;
}
if (!checkIdNumberVerifyCode(idNumber)) {
return false;
}
return true;
}
/**
* 身份证正则校验
*/
private static boolean checkIdNumberRegex(String idNumber) {
return Pattern.matches("^([0-9]{17}[0-9Xx])|([0-9]{15})$", idNumber);
}
/**
* 身份证地区码检查
*/
private static boolean checkIdNumberArea(String idNumberArea) {
int areaCode = Integer.parseInt(idNumberArea);
if (areaCode == HONGKONG_AREACODE || areaCode == MACAO_AREACODE || areaCode == TAIWAN_AREACODE) {
return true;
}
if (areaCode <= MAX_MAINLAND_AREACODE && areaCode >= MIN_MAINLAND_AREACODE) {
return true;
}
return false;
}
/**
* 将15位身份证转换为18位
*/
private static String convertFifteenToEighteen(String idNumber) {
if (15 != idNumber.length()) {
return idNumber;
}
idNumber = idNumber.substring(0, 6) + "19" + idNumber.substring(6, 15);
idNumber = idNumber + getVerifyCode(idNumber);
return idNumber;
}
/**
* 根据身份证前17位计算身份证校验码
*/
private static String getVerifyCode(String idNumber) {
if (!Pattern.matches(regexNum, idNumber.substring(0, 17))) {
return null;
}
String[] ValCodeArr = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
String[] Wi = {"7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2"};
int sum = 0;
for (int i = 0; i < 17; i++) {
sum = sum + Integer.parseInt(String.valueOf(idNumber.charAt(i))) * Integer.parseInt(Wi[i]);
}
return ValCodeArr[sum % 11];
}
/**
* 身份证出生日期嘛检查
*/
private static boolean checkBirthday(String idNumberBirthdayStr) {
Integer year = null;
try {
year = Integer.valueOf(idNumberBirthdayStr.substring(0, 4));
} catch (Exception e) {
}
if (null == year) {
return false;
}
if (isLeapYear(year)) {
return Pattern.matches(regexBirthdayInLeapYear, idNumberBirthdayStr);
} else {
return Pattern.matches(regexBirthdayInCommonYear, idNumberBirthdayStr);
}
}
/**
* 判断是否为闰年
*/
private static boolean isLeapYear(int year) {
return (year % 400 == 0) || (year % 100 != 0 && year % 4 == 0);
}
/**
* 身份证校验码检查
*/
private static boolean checkIdNumberVerifyCode(String idNumber) {
return getVerifyCode(idNumber).equalsIgnoreCase(idNumber.substring(17));
}
public static void main(String[] args) {
System.out.println(strongVerifyIdNumber("121"));
System.out.println(strongVerifyIdNumber("420101198101019701"));
}
@Value("${hw-ocr-enable:false}")
public void setEnable(Boolean enable) {
IdCardUtils.enable = enable;
}
@Value("${hw-ocr-username}")
public void setUsername(String hwusername) {
username = hwusername;
}
@Value("${hw-ocr-domainname}")
public void setDomainname(String hwdomainname) {
domainname = hwdomainname;
}
@Value("${hw-ocr-password}")
public void setPassword(String hwpassword) {
password = hwpassword;
}
@Value("${hw-ocr-projectid}")
public void setProjectid(String hwprojectid) {
projectid = hwprojectid;
}
@Value("${hw-ocr-endpoint}")
public void setEndpoint(String hwendpoint) {
endpoint = hwendpoint;
}
}