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); // 返回天数 } } }