mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add method for #1348
This commit is contained in:
parent
387845b869
commit
6110a20f0a
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
### 新特性
|
### 新特性
|
||||||
* 【core 】 DynaBean.create增加重载方法(pr#245@Gitee)
|
* 【core 】 DynaBean.create增加重载方法(pr#245@Gitee)
|
||||||
|
* 【core 】 IdcardUtil增加重载是否忽略大小写(issue#1348@Github)
|
||||||
|
|
||||||
### Bug修复
|
### Bug修复
|
||||||
|
|
||||||
|
@ -147,7 +147,7 @@ public class IdcardUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否有效身份证号
|
* 是否有效身份证号,忽略X的大小写
|
||||||
*
|
*
|
||||||
* @param idCard 身份证号,支持18位、15位和港澳台的10位
|
* @param idCard 身份证号,支持18位、15位和港澳台的10位
|
||||||
* @return 是否有效
|
* @return 是否有效
|
||||||
@ -198,9 +198,46 @@ public class IdcardUtil {
|
|||||||
* </ol>
|
* </ol>
|
||||||
*
|
*
|
||||||
* @param idcard 待验证的身份证
|
* @param idcard 待验证的身份证
|
||||||
* @return 是否有效的18位身份证
|
* @return 是否有效的18位身份证,忽略x的大小写
|
||||||
*/
|
*/
|
||||||
public static boolean isValidCard18(String idcard) {
|
public static boolean isValidCard18(String idcard) {
|
||||||
|
return isValidCard18(idcard, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 判断18位身份证的合法性
|
||||||
|
* </p>
|
||||||
|
* 根据〖中华人民共和国国家标准GB11643-1999〗中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成。<br>
|
||||||
|
* 排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。
|
||||||
|
* <p>
|
||||||
|
* 顺序码: 表示在同一地址码所标识的区域范围内,对同年、同月、同 日出生的人编定的顺序号,顺序码的奇数分配给男性,偶数分配 给女性。
|
||||||
|
* </p>
|
||||||
|
* <ol>
|
||||||
|
* <li>第1、2位数字表示:所在省份的代码</li>
|
||||||
|
* <li>第3、4位数字表示:所在城市的代码</li>
|
||||||
|
* <li>第5、6位数字表示:所在区县的代码</li>
|
||||||
|
* <li>第7~14位数字表示:出生年、月、日</li>
|
||||||
|
* <li>第15、16位数字表示:所在地的派出所的代码</li>
|
||||||
|
* <li>第17位数字表示性别:奇数表示男性,偶数表示女性</li>
|
||||||
|
* <li>第18位数字是校检码,用来检验身份证的正确性。校检码可以是0~9的数字,有时也用x表示</li>
|
||||||
|
* </ol>
|
||||||
|
* <p>
|
||||||
|
* 第十八位数字(校验码)的计算方法为:
|
||||||
|
* <ol>
|
||||||
|
* <li>将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2</li>
|
||||||
|
* <li>将这17位数字和系数相乘的结果相加</li>
|
||||||
|
* <li>用加出来和除以11,看余数是多少</li>
|
||||||
|
* <li>余数只可能有0 1 2 3 4 5 6 7 8 9 10这11个数字。其分别对应的最后一位身份证的号码为1 0 X 9 8 7 6 5 4 3 2</li>
|
||||||
|
* <li>通过上面得知如果余数是2,就会在身份证的第18位数字上出现罗马数字的Ⅹ。如果余数是10,身份证的最后一位号码就是2</li>
|
||||||
|
* </ol>
|
||||||
|
*
|
||||||
|
* @param idcard 待验证的身份证
|
||||||
|
* @param ignoreCase 是否忽略大小写。{@code true}则忽略X大小写,否则严格匹配大写。
|
||||||
|
* @return 是否有效的18位身份证
|
||||||
|
* @since 5.5.7
|
||||||
|
*/
|
||||||
|
public static boolean isValidCard18(String idcard, boolean ignoreCase) {
|
||||||
if (CHINA_ID_MAX_LENGTH != idcard.length()) {
|
if (CHINA_ID_MAX_LENGTH != idcard.length()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -217,13 +254,12 @@ public class IdcardUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 前17位
|
// 前17位
|
||||||
String code17 = idcard.substring(0, 17);
|
final String code17 = idcard.substring(0, 17);
|
||||||
// 第18位
|
|
||||||
char code18 = Character.toLowerCase(idcard.charAt(17));
|
|
||||||
if (ReUtil.isMatch(PatternPool.NUMBERS, code17)) {
|
if (ReUtil.isMatch(PatternPool.NUMBERS, code17)) {
|
||||||
// 获取校验位
|
// 获取校验位
|
||||||
char val = getCheckCode18(code17);
|
char val = getCheckCode18(code17);
|
||||||
return val == code18;
|
// 第18位
|
||||||
|
return CharUtil.equals(val, idcard.charAt(17), ignoreCase);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -586,7 +622,7 @@ public class IdcardUtil {
|
|||||||
case 3:
|
case 3:
|
||||||
return '9';
|
return '9';
|
||||||
case 2:
|
case 2:
|
||||||
return 'x';
|
return 'X';
|
||||||
case 1:
|
case 1:
|
||||||
return '0';
|
return '0';
|
||||||
case 0:
|
case 0:
|
||||||
|
@ -89,8 +89,20 @@ public class IdcardUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void isValidCard18Test(){
|
public void isValidCard18Test(){
|
||||||
final boolean isValidCard18 = IdcardUtil.isValidCard18("3301022011022000D6");
|
boolean isValidCard18 = IdcardUtil.isValidCard18("3301022011022000D6");
|
||||||
Assert.assertFalse(isValidCard18);
|
Assert.assertFalse(isValidCard18);
|
||||||
|
|
||||||
|
// 不忽略大小写情况下,X严格校验必须大写
|
||||||
|
isValidCard18 = IdcardUtil.isValidCard18("33010219200403064x", false);
|
||||||
|
Assert.assertFalse(isValidCard18);
|
||||||
|
isValidCard18 = IdcardUtil.isValidCard18("33010219200403064X", false);
|
||||||
|
Assert.assertTrue(isValidCard18);
|
||||||
|
|
||||||
|
// 非严格校验下大小写皆可
|
||||||
|
isValidCard18 = IdcardUtil.isValidCard18("33010219200403064x");
|
||||||
|
Assert.assertTrue(isValidCard18);
|
||||||
|
isValidCard18 = IdcardUtil.isValidCard18("33010219200403064X");
|
||||||
|
Assert.assertTrue(isValidCard18);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -1,19 +1,20 @@
|
|||||||
package cn.hutool.http.ssl;
|
package cn.hutool.http.ssl;
|
||||||
|
|
||||||
|
import java.security.KeyManagementException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
import static cn.hutool.http.ssl.SSLSocketFactoryBuilder.SSLv3;
|
import static cn.hutool.http.ssl.SSLSocketFactoryBuilder.SSLv3;
|
||||||
import static cn.hutool.http.ssl.SSLSocketFactoryBuilder.TLSv1;
|
import static cn.hutool.http.ssl.SSLSocketFactoryBuilder.TLSv1;
|
||||||
import static cn.hutool.http.ssl.SSLSocketFactoryBuilder.TLSv11;
|
import static cn.hutool.http.ssl.SSLSocketFactoryBuilder.TLSv11;
|
||||||
import static cn.hutool.http.ssl.SSLSocketFactoryBuilder.TLSv12;
|
import static cn.hutool.http.ssl.SSLSocketFactoryBuilder.TLSv12;
|
||||||
|
|
||||||
import java.security.KeyManagementException;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 兼容android低版本SSL连接
|
* 兼容android低版本SSL连接<br>
|
||||||
* 咱在测试HttpUrlConnection的时候
|
* 在测试HttpUrlConnection的时候,发现一部分手机无法连接[GithubPage]
|
||||||
* 发现一部分手机无法连接[GithubPage]
|
|
||||||
*
|
*
|
||||||
|
* <p>
|
||||||
* 最后发现原来是某些SSL协议没有开启
|
* 最后发现原来是某些SSL协议没有开启
|
||||||
|
*
|
||||||
* @author MikaGuraNTK
|
* @author MikaGuraNTK
|
||||||
*/
|
*/
|
||||||
public class AndroidSupportSSLFactory extends CustomProtocolsSSLFactory {
|
public class AndroidSupportSSLFactory extends CustomProtocolsSSLFactory {
|
||||||
|
@ -11,6 +11,7 @@ import cn.hutool.core.util.CharsetUtil;
|
|||||||
import cn.hutool.json.test.bean.Exam;
|
import cn.hutool.json.test.bean.Exam;
|
||||||
import cn.hutool.json.test.bean.JsonNode;
|
import cn.hutool.json.test.bean.JsonNode;
|
||||||
import cn.hutool.json.test.bean.KeyBean;
|
import cn.hutool.json.test.bean.KeyBean;
|
||||||
|
import lombok.Data;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -202,6 +203,14 @@ public class JSONArrayTest {
|
|||||||
Assert.assertEquals("-0", nodeList.get(3).getName());
|
Assert.assertEquals("-0", nodeList.get(3).getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getByPathTest(){
|
||||||
|
String jsonStr = "[{\"id\": \"1\",\"name\": \"a\"},{\"id\": \"2\",\"name\": \"b\"}]";
|
||||||
|
final JSONArray jsonArray = JSONUtil.parseArray(jsonStr);
|
||||||
|
Assert.assertEquals("b", jsonArray.getByPath("[1].name"));
|
||||||
|
Assert.assertEquals("b", JSONUtil.getByPath(jsonArray, "[1].name"));
|
||||||
|
}
|
||||||
|
|
||||||
private static Map<String, String> buildMap(String id, String parentId, String name) {
|
private static Map<String, String> buildMap(String id, String parentId, String name) {
|
||||||
Map<String, String> map = new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
map.put("id", id);
|
map.put("id", id);
|
||||||
@ -210,25 +219,9 @@ public class JSONArrayTest {
|
|||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
static class User {
|
static class User {
|
||||||
private Integer id;
|
private Integer id;
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
public Integer getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
public void setId(Integer id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "User [id=" + id + ", name=" + name + "]";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user