dingxin 6 年之前
父節點
當前提交
3635ff642a

+ 14
- 0
CODE/smart-community/app-api/src/main/java/com/community/huiju/service/ITaUserService.java 查看文件

@@ -3,8 +3,10 @@ package com.community.huiju.service;
3 3
 import com.community.commom.constant.Constant;
4 4
 import com.community.commom.mode.ResponseBean;
5 5
 import com.community.commom.session.UserElement;
6
+import com.community.commom.utils.IdcardValidator;
6 7
 import com.community.huiju.exception.WisdomException;
7 8
 import com.community.huiju.model.TaUser;
9
+import org.apache.commons.lang.StringUtils;
8 10
 import org.springframework.web.multipart.MultipartFile;
9 11
 
10 12
 import javax.servlet.http.HttpSession;
@@ -29,6 +31,18 @@ public interface ITaUserService {
29 31
         }
30 32
     }
31 33
 
34
+    /**
35
+     * 校验身份证
36
+     * @param taUser
37
+     */
38
+    default void checkIdCart(TaUser taUser) {
39
+        if (StringUtils.isBlank(taUser.getIdCard())) {
40
+            throw new WisdomException("身份证号不能为空");
41
+        }
42
+        if (!IdcardValidator.isValidatedAllIdcard(taUser.getIdCard())) {
43
+            throw  new WisdomException("身份证号不合法,请修改");
44
+        }
45
+    }
32 46
     /**
33 47
      * 用户登录
34 48
      *

+ 11
- 0
CODE/smart-community/app-api/src/main/java/com/community/huiju/service/impl/TaUserServiceImpl.java 查看文件

@@ -215,6 +215,9 @@ public class TaUserServiceImpl implements ITaUserService {
215 215
             iCode.checkPhoneAndCode(user.getLoginName(), code);
216 216
         }
217 217
 
218
+        // 校验身份证
219
+        this.checkIdCart(user);
220
+
218 221
         // 开始注册
219 222
         user.setCreateDate(new Date());
220 223
         user.setUpdateDate(new Date());
@@ -417,6 +420,10 @@ public class TaUserServiceImpl implements ITaUserService {
417 420
 
418 421
         // 前台提交的数据
419 422
         TaUser user = JSONObject.parseObject(parameter, TaUser.class);
423
+
424
+        // 校验身份证
425
+        this.checkIdCart(user);
426
+
420 427
         // 数据库数据
421 428
         TaUser taUser = taUserMapper.selectByPrimaryKey(userElement.getId());
422 429
 
@@ -506,6 +513,10 @@ public class TaUserServiceImpl implements ITaUserService {
506 513
         String roleName = jsonObject.getString("roleName");
507 514
 
508 515
         TaUser user = jsonObject.toJavaObject(TaUser.class);
516
+
517
+        // 校验身份证
518
+        this.checkIdCart(user);
519
+
509 520
         // 调用注册接口
510 521
         response = register(parameter);
511 522
         // 返回值 code 为 1,表示已经注册

+ 4
- 0
CODE/smart-community/app-api/src/main/java/com/community/huiju/service/impl/WxLoginServiceImpl.java 查看文件

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
4 4
 import com.community.commom.mode.ResponseBean;
5 5
 import com.community.commom.session.UserElement;
6 6
 import com.community.commom.utils.BeanTools;
7
+import com.community.commom.utils.IdcardValidator;
7 8
 import com.community.huiju.common.code.ICode;
8 9
 import com.community.huiju.common.code.cache.AppkeyCache;
9 10
 import com.community.huiju.common.perproties.LoginCodePerproties;
@@ -217,6 +218,9 @@ public class WxLoginServiceImpl implements WxLoginServiceI {
217 218
 		JSONObject jsonObject = JSONObject.parseObject(parameter);
218 219
 		TaUser user = jsonObject.toJavaObject(TaUser.class);
219 220
 
221
+		// 验证身份证
222
+		iTaUserService.checkIdCart(user);
223
+
220 224
 		Map<String,Object> map = Maps.newHashMap();
221 225
 		map.put("loginName",user.getLoginName());
222 226
 		TaUser currentUser = taUserMapper.selectByLoginName(map);

+ 433
- 0
CODE/smart-community/community-common/src/main/java/com/community/commom/utils/IdcardValidator.java 查看文件

@@ -0,0 +1,433 @@
1
+package com.community.commom.utils;
2
+import java.text.ParseException;  
3
+import java.text.SimpleDateFormat;  
4
+import java.util.Calendar;  
5
+import java.util.Date;  
6
+import java.util.GregorianCalendar;  
7
+import java.util.HashMap;  
8
+import java.util.Map;  
9
+import java.util.regex.Pattern;  
10
+  
11
+/** 
12
+ * 
13
+ * 类说明:身份证合法性校验 
14
+ * 
15
+ * --15位身份证号码:第7、8位为出生年份(两位数),第9、10位为出生月份,第11、12位代表出生日期,第15位代表性别,奇数为男,偶数为女。 
16
+ * --18位身份证号码 
17
+ * :第7、8、9、10位为出生年份(四位数),第11、第12位为出生月份,第13、14位代表出生日期,第17位代表性别,奇数为男,偶数为女。 
18
+ * 
19
+ */  
20
+@SuppressWarnings({ "unchecked", "unused", "all" })  
21
+public class IdcardValidator {  
22
+  
23
+    /** 
24
+     * 省,直辖市代码表: { 11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古", 
25
+     * 21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏", 
26
+     * 33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南", 
27
+     * 42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆", 
28
+     * 51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃", 
29
+     * 63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"} 
30
+     */  
31
+    protected static String codeAndCity[][] = { { "11", "北京" }, { "12", "天津" },  
32
+            { "13", "河北" }, { "14", "山西" }, { "15", "内蒙古" }, { "21", "辽宁" },  
33
+            { "22", "吉林" }, { "23", "黑龙江" }, { "31", "上海" }, { "32", "江苏" },  
34
+            { "33", "浙江" }, { "34", "安徽" }, { "35", "福建" }, { "36", "江西" },  
35
+            { "37", "山东" }, { "41", "河南" }, { "42", "湖北" }, { "43", "湖南" },  
36
+            { "44", "广东" }, { "45", "广西" }, { "46", "海南" }, { "50", "重庆" },  
37
+            { "51", "四川" }, { "52", "贵州" }, { "53", "云南" }, { "54", "西藏" },  
38
+            { "61", "陕西" }, { "62", "甘肃" }, { "63", "青海" }, { "64", "宁夏" },  
39
+            { "65", "新疆" }, { "71", "台湾" }, { "81", "香港" }, { "82", "澳门" },  
40
+            { "91", "国外" } };  
41
+  
42
+    private static String cityCode[] = { "11", "12", "13", "14", "15", "21", "22",  
43
+            "23", "31", "32", "33", "34", "35", "36", "37", "41", "42", "43",  
44
+            "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63",  
45
+            "64", "65", "71", "81", "82", "91" };  
46
+  
47
+    // 每位加权因子  
48
+    private static int power[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };  
49
+  
50
+    // 第18位校检码  
51
+    private static String verifyCode[] = { "1", "0", "X", "9", "8", "7", "6", "5",  
52
+            "4", "3", "2" };  
53
+  
54
+    /** 
55
+     * 验证所有的身份证的合法性 
56
+     *  
57
+     * @param idcard 
58
+     * @return 
59
+     */  
60
+    public static boolean isValidatedAllIdcard(String idcard) {  
61
+        if (idcard.length() == 15) {  
62
+            idcard = convertIdcarBy15bit(idcard);  
63
+        }  
64
+        return isValidate18Idcard(idcard);  
65
+    }  
66
+  
67
+    /** 
68
+     * 
69
+     * 判断18位身份证的合法性 
70
+     * 
71
+     * 根据〖中华人民共和国国家标准GB11643-1999〗中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成。 
72
+     * 排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。 
73
+     * 
74
+     * 顺序码: 表示在同一地址码所标识的区域范围内,对同年、同月、同 日出生的人编定的顺序号,顺序码的奇数分配给男性,偶数分配 给女性。 
75
+     * 
76
+     * 
77
+     * 1.前1、2位数字表示:所在省份的代码; 2.第3、4位数字表示:所在城市的代码; 3.第5、6位数字表示:所在区县的代码; 
78
+     * 4.第7~14位数字表示:出生年、月、日; 5.第15、16位数字表示:所在地的派出所的代码; 
79
+     * 6.第17位数字表示性别:奇数表示男性,偶数表示女性; 
80
+     * 7.第18位数字是校检码:也有的说是个人信息码,一般是随计算机的随机产生,用来检验身份证的正确性。校检码可以是0~9的数字,有时也用x表示。 
81
+     *  
82
+     * 
83
+     * 第十八位数字(校验码)的计算方法为: 1.将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:7 9 10 5 8 4 
84
+     * 2 1 6 3 7 9 10 5 8 4 2 
85
+     * 
86
+     * 
87
+     * 2.将这17位数字和系数相乘的结果相加。 
88
+     *  
89
+     *  
90
+     * 3.用加出来和除以11,看余数是多少? 
91
+     * 
92
+     * 4.余数只可能有0 1 2 3 4 5 6 7 8 9 10这11个数字。其分别对应的最后一位身份证的号码为1 0 X 9 8 7 6 5 4 3 
93
+     * 2。 
94
+     * 
95
+     * 5.通过上面得知如果余数是2,就会在身份证的第18位数字上出现罗马数字的Ⅹ。如果余数是10,身份证的最后一位号码就是2。 
96
+     * 
97
+     *  
98
+     * @param idcard 
99
+     * @return 
100
+     */  
101
+    public static boolean isValidate18Idcard(String idcard) {  
102
+        // 非18位为假  
103
+        if (idcard.length() != 18) {  
104
+            return false;  
105
+        }  
106
+        // 获取前17位  
107
+        String idcard17 = idcard.substring(0, 17);  
108
+        // 获取第18位  
109
+        String idcard18Code = idcard.substring(17, 18);  
110
+        char c[] = null;  
111
+        String checkCode = "";  
112
+        // 是否都为数字  
113
+        if (isDigital(idcard17)) {  
114
+            c = idcard17.toCharArray();  
115
+        } else {  
116
+            return false;  
117
+        }  
118
+  
119
+        if (null != c) {  
120
+            int bit[] = new int[idcard17.length()];  
121
+  
122
+            bit = converCharToInt(c);  
123
+  
124
+            int sum17 = 0;  
125
+  
126
+            sum17 = getPowerSum(bit);  
127
+  
128
+            // 将和值与11取模得到余数进行校验码判断  
129
+            checkCode = getCheckCodeBySum(sum17);  
130
+            if (null == checkCode) {  
131
+                return false;  
132
+            }  
133
+            // 将身份证的第18位与算出来的校码进行匹配,不相等就为假  
134
+            if (!idcard18Code.equalsIgnoreCase(checkCode)) {  
135
+                return false;  
136
+            }  
137
+        }  
138
+        return true;  
139
+    }  
140
+  
141
+    /** 
142
+     * 验证15位身份证的合法性,该方法验证不准确,最好是将15转为18位后再判断,该类中已提供。 
143
+     *  
144
+     * @param idcard 
145
+     * @return 
146
+     */  
147
+    public boolean isValidate15Idcard(String idcard) {  
148
+        // 非15位为假  
149
+        if (idcard.length() != 15) {  
150
+            return false;  
151
+        }  
152
+  
153
+        // 是否全都为数字  
154
+        if (isDigital(idcard)) {  
155
+            String provinceid = idcard.substring(0, 2);  
156
+            String birthday = idcard.substring(6, 12);  
157
+            int year = Integer.parseInt(idcard.substring(6, 8));  
158
+            int month = Integer.parseInt(idcard.substring(8, 10));  
159
+            int day = Integer.parseInt(idcard.substring(10, 12));  
160
+  
161
+            // 判断是否为合法的省份  
162
+            boolean flag = false;  
163
+            for (String id : cityCode) {  
164
+                if (id.equals(provinceid)) {  
165
+                    flag = true;  
166
+                    break;  
167
+                }  
168
+            }  
169
+            if (!flag) {  
170
+                return false;  
171
+            }  
172
+            // 该身份证生出日期在当前日期之后时为假  
173
+            Date birthdate = null;  
174
+            try {  
175
+                birthdate = new SimpleDateFormat("yyMMdd").parse(birthday);  
176
+            } catch (ParseException e) {  
177
+                e.printStackTrace();  
178
+            }  
179
+            if (birthdate == null || new Date().before(birthdate)) {  
180
+                return false;  
181
+            }  
182
+  
183
+            // 判断是否为合法的年份  
184
+            GregorianCalendar curDay = new GregorianCalendar();  
185
+            int curYear = curDay.get(Calendar.YEAR);  
186
+            int year2bit = Integer.parseInt(String.valueOf(curYear)  
187
+                    .substring(2));  
188
+  
189
+            // 判断该年份的两位表示法,小于50的和大于当前年份的,为假  
190
+            if ((year < 50 && year > year2bit)) {  
191
+                return false;  
192
+            }  
193
+  
194
+            // 判断是否为合法的月份  
195
+            if (month < 1 || month > 12) {  
196
+                return false;  
197
+            }  
198
+  
199
+            // 判断是否为合法的日期  
200
+            boolean mflag = false;  
201
+            curDay.setTime(birthdate); // 将该身份证的出生日期赋于对象curDay  
202
+            switch (month) {  
203
+            case 1:  
204
+            case 3:  
205
+            case 5:  
206
+            case 7:  
207
+            case 8:  
208
+            case 10:  
209
+            case 12:  
210
+                mflag = (day >= 1 && day <= 31);  
211
+                break;  
212
+            case 2: // 公历的2月非闰年有28天,闰年的2月是29天。  
213
+                if (curDay.isLeapYear(curDay.get(Calendar.YEAR))) {  
214
+                    mflag = (day >= 1 && day <= 29);  
215
+                } else {  
216
+                    mflag = (day >= 1 && day <= 28);  
217
+                }  
218
+                break;  
219
+            case 4:  
220
+            case 6:  
221
+            case 9:  
222
+            case 11:  
223
+                mflag = (day >= 1 && day <= 30);  
224
+                break;  
225
+            }  
226
+            if (!mflag) {  
227
+                return false;  
228
+            }  
229
+        } else {  
230
+            return false;  
231
+        }  
232
+        return true;  
233
+    }  
234
+  
235
+    /** 
236
+     * 将15位的身份证转成18位身份证 
237
+     *  
238
+     * @param idcard 
239
+     * @return 
240
+     */  
241
+    public static String convertIdcarBy15bit(String idcard) {  
242
+        String idcard17 = null;  
243
+        // 非15位身份证  
244
+        if (idcard.length() != 15) {  
245
+            return null;  
246
+        }  
247
+  
248
+        if (isDigital(idcard)) {  
249
+            // 获取出生年月日  
250
+            String birthday = idcard.substring(6, 12);  
251
+            Date birthdate = null;  
252
+            try {  
253
+                birthdate = new SimpleDateFormat("yyMMdd").parse(birthday);  
254
+            } catch (ParseException e) {  
255
+                e.printStackTrace();  
256
+            }  
257
+            Calendar cday = Calendar.getInstance();  
258
+            cday.setTime(birthdate);  
259
+            String year = String.valueOf(cday.get(Calendar.YEAR));  
260
+  
261
+            idcard17 = idcard.substring(0, 6) + year + idcard.substring(8);  
262
+  
263
+            char c[] = idcard17.toCharArray();  
264
+            String checkCode = "";  
265
+  
266
+            if (null != c) {  
267
+                int bit[] = new int[idcard17.length()];  
268
+  
269
+                // 将字符数组转为整型数组  
270
+                bit = converCharToInt(c);  
271
+                int sum17 = 0;  
272
+                sum17 = getPowerSum(bit);  
273
+  
274
+                // 获取和值与11取模得到余数进行校验码  
275
+                checkCode = getCheckCodeBySum(sum17);  
276
+                // 获取不到校验位  
277
+                if (null == checkCode) {  
278
+                    return null;  
279
+                }  
280
+  
281
+                // 将前17位与第18位校验码拼接  
282
+                idcard17 += checkCode;  
283
+            }  
284
+        } else { // 身份证包含数字  
285
+            return null;  
286
+        }  
287
+        return idcard17;  
288
+    }  
289
+  
290
+    /** 
291
+     * 15位和18位身份证号码的基本数字和位数验校 
292
+     *  
293
+     * @param idcard 
294
+     * @return 
295
+     */  
296
+    public boolean isIdcard(String idcard) {  
297
+        return idcard == null || "".equals(idcard) ? false : Pattern.matches(  
298
+                "(^\\d{15}$)|(\\d{17}(?:\\d|x|X)$)", idcard);  
299
+    }  
300
+  
301
+    /** 
302
+     * 15位身份证号码的基本数字和位数验校 
303
+     *  
304
+     * @param idcard 
305
+     * @return 
306
+     */  
307
+    public boolean is15Idcard(String idcard) {  
308
+        return idcard == null || "".equals(idcard) ? false : Pattern.matches(  
309
+                "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$",  
310
+                idcard);  
311
+    }  
312
+  
313
+    /** 
314
+     * 18位身份证号码的基本数字和位数验校 
315
+     *  
316
+     * @param idcard 
317
+     * @return 
318
+     */  
319
+    public boolean is18Idcard(String idcard) {  
320
+        return Pattern  
321
+                .matches(  
322
+                        "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([\\d|x|X]{1})$",  
323
+                        idcard);  
324
+    }  
325
+  
326
+    /** 
327
+     * 数字验证 
328
+     *  
329
+     * @param str 
330
+     * @return 
331
+     */  
332
+    public static boolean isDigital(String str) {  
333
+        return str == null || "".equals(str) ? false : str.matches("^[0-9]*$");  
334
+    }  
335
+  
336
+    /** 
337
+     * 将身份证的每位和对应位的加权因子相乘之后,再得到和值 
338
+     *  
339
+     * @param bit 
340
+     * @return 
341
+     */  
342
+    public static int getPowerSum(int[] bit) {  
343
+  
344
+        int sum = 0;  
345
+  
346
+        if (power.length != bit.length) {  
347
+            return sum;  
348
+        }  
349
+  
350
+        for (int i = 0; i < bit.length; i++) {  
351
+            for (int j = 0; j < power.length; j++) {  
352
+                if (i == j) {  
353
+                    sum = sum + bit[i] * power[j];  
354
+                }  
355
+            }  
356
+        }  
357
+        return sum;  
358
+    }  
359
+  
360
+    /** 
361
+     * 将和值与11取模得到余数进行校验码判断 
362
+     *  
363
+     * @param checkCode 
364
+     * @param sum17 
365
+     * @return 校验位 
366
+     */  
367
+    public static String getCheckCodeBySum(int sum17) {  
368
+        String checkCode = null;  
369
+        switch (sum17 % 11) {  
370
+        case 10:  
371
+            checkCode = "2";  
372
+            break;  
373
+        case 9:  
374
+            checkCode = "3";  
375
+            break;  
376
+        case 8:  
377
+            checkCode = "4";  
378
+            break;  
379
+        case 7:  
380
+            checkCode = "5";  
381
+            break;  
382
+        case 6:  
383
+            checkCode = "6";  
384
+            break;  
385
+        case 5:  
386
+            checkCode = "7";  
387
+            break;  
388
+        case 4:  
389
+            checkCode = "8";  
390
+            break;  
391
+        case 3:  
392
+            checkCode = "9";  
393
+            break;  
394
+        case 2:  
395
+            checkCode = "x";  
396
+            break;  
397
+        case 1:  
398
+            checkCode = "0";  
399
+            break;  
400
+        case 0:  
401
+            checkCode = "1";  
402
+            break;  
403
+        }  
404
+        return checkCode;  
405
+    }  
406
+  
407
+    /** 
408
+     * 将字符数组转为整型数组 
409
+     *  
410
+     * @param c 
411
+     * @return 
412
+     * @throws NumberFormatException 
413
+     */  
414
+    public static int[] converCharToInt(char[] c) throws NumberFormatException {  
415
+        int[] a = new int[c.length];  
416
+        int k = 0;  
417
+        for (char temp : c) {  
418
+            a[k++] = Integer.parseInt(String.valueOf(temp));  
419
+        }  
420
+        return a;  
421
+    }  
422
+  
423
+    public static void main(String[] args) throws Exception {  
424
+          
425
+        String idcard15 = "142431199001145";//15位  
426
+        String idcard18 = "121212121212121212";//18位  
427
+        System.out.println(IdcardValidator.isValidatedAllIdcard(idcard15));  
428
+        System.out.println(IdcardValidator.isValidatedAllIdcard(idcard18));
429
+
430
+        String idCart = "420901199610081114";
431
+        System.out.println(IdcardValidator.isValidatedAllIdcard(idCart));
432
+    }  
433
+}