dangsn
2024-12-26 4e9ff2ce6a830bb2340d7c8612c72eea0c5a553e
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package com.vci.client.logon.client;
 
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
import netscape.javascript.JSObject;
 
import com.vci.client.common.objects.ClientInfo;
import com.vci.client.common.objects.UserEntityObject;
import com.vci.client.common.objects.UserLogonObject;
import com.vci.client.common.objects.UserObject;
import com.vci.client.framework.delegate.RightManagementClientDelegate;
import com.vci.client.framework.systemConfig.object.PasswordStrategyObject;
import com.vci.client.framework.util.RightControlUtil;
import com.vci.client.logon.base.ChangePasswordDialog;
import com.vci.client.ui.exception.VCIException;
import com.vci.client.ui.locale.LocaleDisplay;
import com.vci.client.ui.swing.KPasswordField;
import com.vci.client.ui.swing.VCIOptionPane;
import com.vci.corba.common.VCIError;
 
public class LogonAppletThread extends Thread {
 
    private JSObject win = null;
    private String userName = null;
    private String password = null;
    private LogonPanel logonPanel;
    private UserObject lockedUserObj = null;
    private UserLogonObject userLogonObj = null;
    private int defaultWrongNum = 0;
    private int defaultLogonTime = 0;
    private int dafaultOverdueDay = 0;
    private int dafaultRemideDay = 0;
 
    public LogonAppletThread(JSObject win, String userName, String password,
            KPasswordField psText, LogonPanel logonPanel) {
        this.win = win;
        this.userName = userName;
        this.password = password;
        this.logonPanel = logonPanel;
    }
 
    public void run() {
        try {
            lockedUserObj = this.getUserObjToLock();
            PasswordStrategyObject pwdStgObj = null;
        
            if ("".equals(lockedUserObj.getId())) {
                VCIOptionPane.showMessageDialog(this.logonPanel, "此用户名不存在!!!");
                this.logonPanel.clearLoadingIcon();
                logonPanel.logonButton.setEnabled(true);
                return;
            }
            
            /**校验用户名密码是否正确**/
            UserObject userObj = this.checkLogin();
            if(userObj == null) {
                VCIOptionPane.showError(this.logonPanel, "请检查用户名和密码是否正确!");
                this.logonPanel.clearLoadingIcon();
                logonPanel.logonButton.setEnabled(true);
                return;
            }
            
            if (userObj != null && userObj.getStatus() == 1) {
                VCIOptionPane.showMessageDialog(this.logonPanel,
                        "您的帐户已被停用,请联系管理员!");
                this.logonPanel.frame.dispose();
                return;
            }
            
            if(RightControlUtil.isAdminOrDeveloperOrRoot(userObj.getUserName())){
                //跳转到主界面
                String url = "./vcicode.jsp?userName=" + userName;
                System.out.println(url);
                String href = "window.location.href='" + url + "'";
                win.eval(href);
                
                return;
            }
 
            if(!(RightControlUtil.isAdminOrDeveloperOrRoot(userName))){
                pwdStgObj = new RightManagementClientDelegate()
                        .fetchPasswordStrategyByUserId(lockedUserObj.getId());
                if(!("".equals(pwdStgObj.getId()))){// 密码策略是否为空
                    defaultWrongNum = pwdStgObj.getRetryTime(); // 获取密码策略规定的错误登录次数
                    defaultLogonTime = pwdStgObj.getLockTime(); // 获取密码策略规定的锁定时间
                    dafaultOverdueDay = pwdStgObj.getOverdueDay(); // 获取密码
                    dafaultRemideDay = pwdStgObj.getRemideDay(); // 获取密码策略规定的密码快过期提醒时间
                }
            }
            userLogonObj = this.getUserLogonObj(lockedUserObj.getId());
 
            if (userLogonObj != null 
                    && !RightControlUtil.isAdmin(lockedUserObj.getUserName())
                    && !RightControlUtil.isDeveloper(lockedUserObj.getUserName())){
                int wrongNum = userLogonObj.getPlWrongNum();
                long lastLogonTime = userLogonObj.getPlLogonTime() + 1;
                long systemTime = 0;
                systemTime = new RightManagementClientDelegate()
                        .getSystemTime();
 
                int timeGap = compare_time(lastLogonTime, systemTime, true);
                if (wrongNum >= defaultWrongNum && timeGap < defaultLogonTime) {
                    //用户登陆失败时候记录相关日志信息
                    if (!RightControlUtil.isAdminOrDeveloperOrRoot(lockedUserObj.getUserName())){
                        String message = "登陆次数过多,登入系统失败!";
                        new RightManagementClientDelegate(getUserEntityObject(lockedUserObj)).savelogFail(message);
                    }
                    VCIOptionPane.showMessage(logonPanel,
                            "您的登录次数已经超过密码策略登录次数限制,用户已被锁定,\n 请等"
                                    + defaultLogonTime + "分钟之后再尝试登录,或找管理员解锁");
                    return;
                }
            }
 
            // 登录过程中需要做的验证,全部转移到SERVER端进行,
            // 如果验证失败将通过VCIERROR的形式返回
 
            updateLogonInfo(userObj.getId(), true);
            logonPanel.saveUserName();
            
            UserEntityObject userEntityObj = this.getUserEntityObject(userObj);
//            //跳转到主界面
//            String url = "./vcicode.jsp?userName=" + userName;
//            System.out.println(url);
//            String href = "window.location.href='" + url + "'";
//            win.eval(href);
//            
//            /**
//             * 记录登录日志  简单记录登入日志
//             */
//            if (!RightControlUtil.isAdminOrDeveloper(userObj.getUserName())){
//                String message = "登入";
//                UserEntityObject userEntityObj = this.getUserEntityObject(userObj);
//                userEntityObj.setModules(this.logonPanel.getClass().getName());
//                new RightManagementClientDelegate(userEntityObj).savelog(message);
//            }
 
            long pwdUpdateTime = userObj.getPwdUpdateTime();
            long systemTime = 0;
            systemTime = new RightManagementClientDelegate().getSystemTime();
 
            int date = compare_time(pwdUpdateTime, systemTime, false);
            if (pwdUpdateTime == 0 //"1970-01-01 00:00:00.0".equals(pwdUpdateTime.toString())
                    && !RightControlUtil.isAdmin(lockedUserObj.getUserName())
                    && !RightControlUtil.isDeveloper(lockedUserObj.getUserName())){
                VCIOptionPane.showMessageDialog(this.logonPanel,
                        "您的密码是管理员初始的密码,需要修改密码才能进行其它操作!");
                ChangePasswordDialog changePasswordDialog = new ChangePasswordDialog(
                        this.logonPanel.frame, true,userEntityObj);
                changePasswordDialog.setVisible(true);
            } else if (date >= dafaultOverdueDay && userObj.getUserType() >= 1) {
                VCIOptionPane.showMessageDialog(this.logonPanel,
                        "您的密码已经过期,请进行修改!");
                ChangePasswordDialog changePasswordDialog = new ChangePasswordDialog(
                        this.logonPanel.frame, true,userEntityObj);
                changePasswordDialog.setVisible(true);
            } else if ((dafaultOverdueDay > date)
                    && (dafaultOverdueDay - date <= dafaultRemideDay)
                    && userObj.getUserType() >= 1) {
                int pwValidity = dafaultOverdueDay - date;
                VCIOptionPane.showMessageDialog(this.logonPanel,
                        "您的密码有效期还有" + pwValidity + "天,请注意修改!");
            }
            
            //跳转到主界面
            String url = "./vcicode.jsp?userName=" + userName;
            System.out.println(url);
            String href = "window.location.href='" + url + "'";
            win.eval(href);
            
            /**
             * 记录登录日志  简单记录登入日志
             */
            if (!RightControlUtil.isAdminOrDeveloperOrRoot(userObj.getUserName())){
                String message = "登入";
                userEntityObj.setModules(this.logonPanel.getClass().getName());
                new RightManagementClientDelegate(userEntityObj).savelog(message);
            }
            
        } catch (Exception vex) {
            vex.printStackTrace();
            // add by xchao 2012.09.07
            // 在登录时,SERVER可能出现数据库方面的连接问题,从而处理抛出VCIError
            if(lockedUserObj == null && vex instanceof VCIError){
                VCIError verror = (VCIError)vex;
                String key = String.valueOf(verror.code);
                String message = LocaleDisplay.getI18nString(key, "RMIPFramework", logonPanel.getLocale());
                message += "\n" +
                        "请从以下几个方面进行排查:\n" +
                        "1、检查数据库服务器是否正常启动、运行\n" +
                        "2、检查服务器端系统是否正常启动、运行\n" +
                        "3、检查服务器端数据库连接、配置是否正确\n" +
                        "4、检查服务器端数据库连接池、会话(Session)连接是否正常" +
                        "";
                VCIOptionPane.showError(logonPanel, message);
                return;
            }
        }
    }
 
    private UserObject checkLogin() throws VCIException {
        String desPassword = password;
        /*if(!(RightControlUtil.isAdminOrDeveloper(userName))){
            *//**
             * 将登录界面中输入的密码加密
             *//*
            ThreeDES des = new ThreeDES();// 实例化一个对像      
            des.getKey("daliantan0v0");// 生成密匙      
            desPassword = des.getEncString(password);// 加密字符串,返回String的密文
        }*/
        String ip = getIPAddress();
        ClientInfo client = new ClientInfo();
        client.setIp(ip);
        return new RightManagementClientDelegate().checkLogin(userName,
                desPassword, client);
    }
 
    /**
     * 根据登录的用户名获取将要被锁的账户
     * 
     * @return
     * @throws VCIError
     */
    private UserObject getUserObjToLock() throws VCIException {
        return new RightManagementClientDelegate().getUserObjToLock(userName);
    }
 
    /**
     * 根据用户获取账户登录信息
     * 
     * @param userId
     * @return
     * @throws VCIError
     */
    private UserLogonObject getUserLogonObj(String userId) throws VCIException {
        return new RightManagementClientDelegate().getUserLogonObj(userId);
    }
 
    /**
     * 更新用户的登录信息。成功登录,错误次数改为0,登录时间改为当前时间; 登录失败,错误次数加1,登录时间改为当前时间。
     * 
     * @param userId
     * @param flag
     * @throws VCIError
     */
    private void updateLogonInfo(String userId, boolean flag) throws VCIException {
        new RightManagementClientDelegate().updateLogonInfo(userId, flag);
        
    }
 
    private UserEntityObject getUserEntityObject(UserObject userObj){
        UserEntityObject obj = new UserEntityObject();
        obj.setUserName(userObj.getUserName());
        String ip = getIPAddress();
        obj.setIp(ip);
        obj.setModules("登录模块");
        return obj;
    }
    
    private String getIPAddress() {
        String ip = "127.0.0.1";
        try {
            ip = java.net.InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return ip;
    }
    /**
     * 求两个时间相隔多久
     * 
     * @param DATE1
     * @param DATE2
     * @param flag
     * @return
     */
    private int compare_time(long DATE1, long DATE2, boolean flag) {
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSS");
        Date dt1 = new Date(DATE1);
//        try {
//            dt1 = df.parse(DATE1);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
 
        long seconds = (DATE2 - dt1.getTime()) / 1000;
        long date = seconds / (24 * 60 * 60); // 相差的天数
        long hour = (seconds - date * 24 * 60 * 60) / (60 * 60);// 相差的小时数
        long minut = (seconds - date * 24 * 60 * 60 - hour * 60 * 60) / (60);// 相差的分钟数
        // long second = (seconds-date*24*60*60-hour*60*60-minut*60);//相差的秒数
 
        if (flag) {
            return (int) (minut); // 返回分钟数
        } else {
            return (int) (date); // 返回天数
        }
    }
}