田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.vci.client.framework.systemConfig.stafforgmanage;
 
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
 
import com.vci.client.LogonApplication;
import com.vci.client.framework.delegate.RightManagementClientDelegate;
import com.vci.client.framework.systemConfig.object.CombinationObject;
import com.vci.client.framework.systemConfig.object.CombinationValueObject;
import com.vci.client.framework.systemConfig.object.PasswordStrategyObject;
import com.vci.client.ui.exception.VCIException;
import com.vci.client.ui.swing.VCIOptionPane;
 
public class CheckPassWordStrategy {
    public boolean passwordStrategyCheck(String id,String password,Component dialog){
        PasswordStrategyObject pwdStgObj;
        try {
            pwdStgObj = new RightManagementClientDelegate().fetchPasswordStrategyByUserId(id);
            if (pwdStgObj != null){
                //CombinationObject[] combinationObjs = new RightManagementClientDelegate().fetchCombinationsByPstId(pwdStgObj.getId());
//                String[] combinations = new RightManagementClientDelegate().fetchCombinationsByPstIdNew(pwdStgObj.getId());
                int pasLen = pwdStgObj.getPasswordLen(); //最小长度
                int pasMaxLen = pwdStgObj.getPasswordMaxLen();//最大长度
                int requiredType = pwdStgObj.getRequiredType(); // 必填种类
                int containsTypes = pwdStgObj.getCharTypes();// 包含字符类型
                
                String names= "";
 
                int strategy = 0;
                if ((containsTypes & 0x01) == 0x01) {
                    names += "数字,";
                } 
                if ((containsTypes & 0x02) == 0x02) {
                    names += "小写字母,";
                } 
                if ((containsTypes & 0x04) == 0x04) {
                    names += "大写字母,";
                } 
                if ((containsTypes & 0x08) == 0x08) {
                    names += "符号,";
                }
                
                names = names.substring(0, names.length() - 1);
 
                if(password.length() < pasLen){
                    VCIOptionPane.showMessageDialog(dialog,"密码长度不能小于"+pasLen+",且密码必须包含‘"+names+"’中的"+requiredType+"种组合! 请重新输入密码!-1");
                    return false;
                }
                if(password.length() > pasMaxLen){
                    VCIOptionPane.showMessageDialog(dialog,"密码长度不能大于"+pasMaxLen+",且密码必须包含‘"+names+"’中的"+requiredType+"种组合! 请重新输入密码!-2");
                    return false;
                }
                
                int actualTypes = 0;
                
                String symbol = "[ _`~!@#$%^&*()-+={[}]|\\'\":;,.<>/?";
                
                for (int i = 0 ; i < password.length() ;i ++){
                    char c = password.charAt(i);
                    if (Character.isDigit(c))
                        actualTypes |= 0x01;
                    else if (Character.isLowerCase(c))
                        actualTypes |= 0x02;
                    else if (Character.isUpperCase(c))
                        actualTypes |= 0x04;
                    else if (symbol.indexOf(c) > -1)
                        actualTypes |= 0x08;
                }
                if ((actualTypes & containsTypes) != actualTypes){
                    VCIOptionPane.showMessageDialog(dialog,"您输入的密码不正确,密码必须中必须含有‘"+names+"’中的"+requiredType+"种密码组合方式,\n" +
                            "或密码组合方式取值范围中不含有您输入的字符,请确认!-3");
                    return false;
                }
                
                int typeCount = 0;
                for (int i = 0; i < 4; i++) {
                    int type = (int)Math.pow(2, i);
                    if ((actualTypes & type) == type)
                        typeCount++;
                }
 
                if (typeCount < requiredType){
                    VCIOptionPane.showMessageDialog(dialog,"您输入的密码不正确,密码必须中必须含有‘"+names+"’中的"+requiredType+"种密码组合方式,\n" +
                            "或密码组合方式取值范围中不含有您输入的字符,请确认!-4");
                    return false;
                }
            }
            
        } catch (VCIException e) {
            VCIOptionPane.showError(LogonApplication.frame,"RMIPFramework", e);
            return false;
        }
        return true;
    }
}