250 lines
7.2 KiB
Java
250 lines
7.2 KiB
Java
|
|
package com.newyetai.common.validation.util;
|
|||
|
|
|
|||
|
|
import com.newyetai.common.constant.CommonConstants;
|
|||
|
|
import com.newyetai.common.exception.util.Assert;
|
|||
|
|
import org.apache.commons.lang3.StringUtils;
|
|||
|
|
|
|||
|
|
import javax.validation.ConstraintViolation;
|
|||
|
|
import javax.validation.Validation;
|
|||
|
|
import javax.validation.Validator;
|
|||
|
|
import java.util.Set;
|
|||
|
|
import java.util.regex.Pattern;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @author on 2019-11-05
|
|||
|
|
*/
|
|||
|
|
public class ValidateUtils {
|
|||
|
|
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 用户安全码校验正则
|
|||
|
|
*/
|
|||
|
|
public static final String SECURITY_CODE_PATTERN = "^[A-Za-z\\d!@#$%^&*]{6,32}$";
|
|||
|
|
|
|||
|
|
public static Pattern numberPattern = Pattern.compile("^[-\\+]?[\\d]*$");
|
|||
|
|
|
|||
|
|
public static Pattern mobilePattern = Pattern.compile("^[1][0-9]{10}$");
|
|||
|
|
|
|||
|
|
public static Pattern fixedTelephone = Pattern.compile("^[0]\\d{2,3}\\d{7,8}");
|
|||
|
|
|
|||
|
|
public static final Pattern USERNAME_PATTERN = Pattern.compile("^[a-zA-Z0-9]{4,32}$");
|
|||
|
|
public static final Pattern PASSWORD_PATTERN = Pattern.compile("[A-Za-z\\d!@#$%^&*]{6,32}$");
|
|||
|
|
/**
|
|||
|
|
* 正则 邮箱
|
|||
|
|
*/
|
|||
|
|
private static Pattern EMAIL_REG = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
|
|||
|
|
/**
|
|||
|
|
* 昵称校验
|
|||
|
|
*/
|
|||
|
|
private static Pattern NICK_NAME_REG = Pattern.compile("^[\\u4e00-\\u9fffa-zA-Z0-9]{2,32}$");
|
|||
|
|
|
|||
|
|
/** 姓名 **/
|
|||
|
|
private static final Pattern REAL_NAME_REG = Pattern.compile(
|
|||
|
|
"^(([a-zA-Z+\\.?\\·?a-zA-Z+]{2,30}$)|([\\u4e00-\\u9fff+\\·?\\u4e00-\\u9fff+]{2,30}$))");
|
|||
|
|
|
|||
|
|
public static Validator validator = Validation.buildDefaultValidatorFactory()
|
|||
|
|
.getValidator();
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 身份证号权重因子、对应验证码
|
|||
|
|
*/
|
|||
|
|
// 前十七位加权因子
|
|||
|
|
public static final int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
|
|||
|
|
// 这是除以11后,可能产生的11位余数对应的验证码
|
|||
|
|
public static final String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
|
|||
|
|
|
|||
|
|
// 定义判别用户身份证号的正则表达式(15位或者18位,最后一位可以为字母)
|
|||
|
|
// 假设18位身份证号码:41000119910101123X 410001 19910101 123X
|
|||
|
|
// ^开头
|
|||
|
|
// [1-9] 第一位1-9中的一个 4
|
|||
|
|
// \\d{5} 五位数字 10001(前六位省市县地区)
|
|||
|
|
// (18|19|20) 19(现阶段可能取值范围18xx-20xx年)
|
|||
|
|
// \\d{2} 91(年份)
|
|||
|
|
// ((0[1-9])|(10|11|12)) 01(月份)
|
|||
|
|
// (([0-2][1-9])|10|20|30|31)01(日期)
|
|||
|
|
// \\d{3} 三位数字 123(第十七位奇数代表男,偶数代表女)
|
|||
|
|
// [0-9Xx] 0123456789Xx其中的一个 X(第十八位为校验值)
|
|||
|
|
// $结尾
|
|||
|
|
|
|||
|
|
// 假设15位身份证号码:410001910101123 410001 910101 123
|
|||
|
|
// ^开头
|
|||
|
|
// [1-9] 第一位1-9中的一个 4
|
|||
|
|
// \\d{5} 五位数字 10001(前六位省市县地区)
|
|||
|
|
// \\d{2} 91(年份)
|
|||
|
|
// ((0[1-9])|(10|11|12)) 01(月份)
|
|||
|
|
// (([0-2][1-9])|10|20|30|31)01(日期)
|
|||
|
|
// \\d{3} 三位数字 123(第十五位奇数代表男,偶数代表女),15位身份证不含X
|
|||
|
|
// $结尾
|
|||
|
|
private static Pattern idCardPattern = Pattern.compile(
|
|||
|
|
"(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|"
|
|||
|
|
+ "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)");
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 手机号码校验
|
|||
|
|
*
|
|||
|
|
* @param mobile
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static boolean isPhone(String mobile) {
|
|||
|
|
if (StringUtils.isBlank(mobile)) {
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
return mobilePattern.matcher(mobile).matches();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 固定电话校验
|
|||
|
|
*
|
|||
|
|
* @param phone
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static boolean isFixedTelephone(String phone) {
|
|||
|
|
if (StringUtils.isBlank(phone)) {
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
phone = phone.replace("-", "");
|
|||
|
|
return fixedTelephone.matcher(phone).matches();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 身份证校验
|
|||
|
|
*
|
|||
|
|
* @param idCard
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public static boolean isIdCard(String idCard) {
|
|||
|
|
if (StringUtils.isBlank(idCard)) {
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
boolean matches = idCardPattern.matcher(idCard).matches();
|
|||
|
|
// 判断第18位校验值
|
|||
|
|
if (matches) {
|
|||
|
|
if (idCard.length() == 18) {
|
|||
|
|
try {
|
|||
|
|
char[] charArray = idCard.toCharArray();
|
|||
|
|
int sum = 0;
|
|||
|
|
for (int i = 0; i < idCardWi.length; i++) {
|
|||
|
|
int current = Integer.parseInt(String.valueOf(charArray[i]));
|
|||
|
|
int count = current * idCardWi[i];
|
|||
|
|
sum += count;
|
|||
|
|
}
|
|||
|
|
char idCardLast = charArray[17];
|
|||
|
|
int idCardMod = sum % 11;
|
|||
|
|
if (idCardY[idCardMod].toUpperCase().equals(String.valueOf(idCardLast).toUpperCase())) {
|
|||
|
|
return true;
|
|||
|
|
} else {
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
return matches;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 校验对象,校验未通过返回异常信息
|
|||
|
|
* @param object
|
|||
|
|
* @param groups
|
|||
|
|
* @return null 校验通过;否则 返回异常信息
|
|||
|
|
* @author sr on 2021-05-25
|
|||
|
|
*/
|
|||
|
|
public static String validate(Object object, Class<?>... groups) {
|
|||
|
|
Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
|
|||
|
|
if (constraintViolations.isEmpty()) {
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
StringBuilder errMsg = new StringBuilder();
|
|||
|
|
constraintViolations.forEach(constraintViolation -> errMsg
|
|||
|
|
.append(constraintViolation.getMessage()).append(CommonConstants.COMMA));
|
|||
|
|
return errMsg.toString();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 校验对象,校验未通过抛BusinessException
|
|||
|
|
* @param object
|
|||
|
|
* @param groups
|
|||
|
|
* @author sr on 2021-05-25
|
|||
|
|
*/
|
|||
|
|
public static void validateBiz(Object object, Class<?>... groups) {
|
|||
|
|
String errMsg = validate(object, groups);
|
|||
|
|
Assert.isNull(errMsg, errMsg);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 校验对象,校验未通过抛BusinessException
|
|||
|
|
* @param object
|
|||
|
|
* @param errMsg 自定义异常信息
|
|||
|
|
* @param groups
|
|||
|
|
* @author sr on 2021-06-10
|
|||
|
|
*/
|
|||
|
|
public static void validateBiz(Object object, String errMsg, Class<?>... groups) {
|
|||
|
|
String originErrMsg = validate(object, groups);
|
|||
|
|
Assert.isNull(originErrMsg, errMsg);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 账号是否合规
|
|||
|
|
* @param userName
|
|||
|
|
* @return
|
|||
|
|
* @author sr on 2021-07-07
|
|||
|
|
*/
|
|||
|
|
public static boolean matchUsername(String userName) {
|
|||
|
|
if (StringUtils.isBlank(userName)) {
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
return USERNAME_PATTERN.matcher(userName).matches();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 密码是否合规
|
|||
|
|
* @param password
|
|||
|
|
* @return
|
|||
|
|
* @author sr on 2021-07-07
|
|||
|
|
*/
|
|||
|
|
public static boolean matchPassword(String password) {
|
|||
|
|
if (StringUtils.isBlank(password)) {
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
return PASSWORD_PATTERN.matcher(password).matches();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 邮箱是否合规
|
|||
|
|
* @param email
|
|||
|
|
* @return
|
|||
|
|
* @author sr on 2021-07-07
|
|||
|
|
*/
|
|||
|
|
public static boolean checkEmail(String email) {
|
|||
|
|
if (StringUtils.isBlank(email)) {
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
return EMAIL_REG.matcher(email).matches();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static boolean checkNickName(String nickname) {
|
|||
|
|
if (StringUtils.isBlank(nickname)) {
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
return NICK_NAME_REG.matcher(nickname).matches();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 检查中文姓名
|
|||
|
|
*
|
|||
|
|
* @param realname 中文名
|
|||
|
|
* @return boolean
|
|||
|
|
*/
|
|||
|
|
public static boolean checkRealName(String realname) {
|
|||
|
|
if (StringUtils.isBlank(realname)) {
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
return REAL_NAME_REG.matcher(realname).matches();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|