package com.vci.client.logon.client; import java.awt.Frame; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Timer; import java.util.TimerTask; import org.apache.commons.lang3.StringUtils; import com.vci.client.ClientSession; import com.vci.client.LogonApplication; import com.vci.client.common.ConfigUtils; import com.vci.client.common.objects.UserObject; import com.vci.client.common.providers.ServiceProvider; import com.vci.client.framework.appConfig.object.AppConfigDetailObject; import com.vci.client.framework.delegate.AppConfigDetailClientDelegate; import com.vci.client.framework.delegate.RightManagementClientDelegate; import com.vci.client.framework.delegate.SystemCfgClientDelegate; import com.vci.client.logon.base.ChangePasswordDialog; import com.vci.client.logon.base.LogonHandler; import com.vci.client.logon.base.LogonResult; 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.client.ui.swing.components.VCIJOptionPane; import com.vci.common.resource.CommonProperties; import com.vci.corba.common.VCIError; import com.vci.corba.common.data.UserEntityInfo; import com.vci.mw.ClientContextVariable; public class LoginThread extends Thread { private Frame loginFrame = null; private String userName = null; private String password = null; private LogonPanel logonPanel; private UserObject lockedUserObj = null; public LoginThread(Frame frame, String userName, String password, KPasswordField psText, LogonPanel logonPanel) { this.loginFrame = frame; this.userName = userName; this.password = password; this.logonPanel = logonPanel; } private String getIPAddress() { String ip = "127.0.0.1"; try { ip = java.net.InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); } return ip; } RightManagementClientDelegate rmcd = new RightManagementClientDelegate(); public void run() { try { //应保密测评,只能三员登录,且指定的IP地址可访问。 SystemCfgClientDelegate conf = new SystemCfgClientDelegate(); String enable = conf.getConfigValue("evaluation.enable"); //是否开启登录名及IP控制 if("true".equals(enable)) { //判断登录名是否允许 String loginName = conf.getConfigValue("evaluation.loginUserName"); if(StringUtils.isBlank(loginName)){ VCIOptionPane.showError(loginFrame,"配置文件错误,没有配置evaluation.loginUserName"); return ; } String[] loginNameArr = loginName.split(","); ArrayList loginNameList = new ArrayList(loginNameArr.length); Collections.addAll(loginNameList, loginNameArr); //获取客户端访问机器的所有IP地址 List clientIpList = new ArrayList(); try { String hostName = java.net.InetAddress.getLocalHost().getHostName(); InetAddress[] addressArr = java.net.InetAddress.getAllByName(hostName); for(InetAddress inetAddress : addressArr){ clientIpList.add(inetAddress.getHostAddress()); } } catch (UnknownHostException e) { e.printStackTrace(); VCIOptionPane.showError(loginFrame,"未能获取到主机名"); return ; } if(!loginNameList.contains(userName)) { VCIOptionPane.showError(loginFrame,"当前登录用户非三员用户"); UserEntityInfo uei = new UserEntityInfo(); uei.ip = clientIpList.size()>0?clientIpList.get(0):"127.0.0.1"; uei.modules = "登录"; uei.userName = userName; ServiceProvider.getLogService().saveLoginLog(false, "登入失败:当前登录用户非三员用户" , uei); return ; } //判断访问IP地址是否允许 String ipKey = "evaluation." + userName + ".ip"; String ip = conf.getConfigValue(ipKey); if(StringUtils.isBlank(ip)){ VCIOptionPane.showError(loginFrame,"配置文件错误,没有配置" +ipKey); return ; } String[] ipArr = ip.split(","); ArrayList ipList = new ArrayList(ipArr.length); Collections.addAll(ipList, ipArr); boolean ipMatch = false; for(String clientIp : clientIpList){ if(ipList.contains(clientIp)){ ipMatch = true; break; } } if(!ipMatch) { VCIOptionPane.showError(loginFrame,"当前机器密级不符合密级要求"); return ; } } String clientIP = getIPAddress(); LogonResult chkRes = new LogonHandler().checkLogin(this.userName, this.password, clientIP); if(!chkRes.isSuccess()){ this.logonPanel.clearLoadingIcon(); this.logonPanel.logonButton.setEnabled(true); VCIOptionPane.showMessageDialog(this.loginFrame, chkRes.getMessage()); return; } //System.out.print("完成了登录"); UserObject userObj = chkRes.getLogonedUserObject(); logonPanel.saveUserName(); LogonFrame.userName = userObj.getUserName(); new LogonApplication(userObj); loginFrame.dispose(); this.checkMessage();// 信息检查 // 添加会话是否过期的检查 this.startTimeoutCheckThread(); // 其它提示消息 String otherMessage = chkRes.getOtherMessage(); if(!"".equals(otherMessage)){ VCIOptionPane.showMessageDialog(this.loginFrame, otherMessage); } // 根据条件确定是否需要弹出修改密码窗口 if(chkRes.isNeedChangePassword()){ ChangePasswordDialog changePasswordDialog = new ChangePasswordDialog( LogonApplication.frame, true, LogonApplication.getUserEntityObject()); changePasswordDialog.setVisible(true); } } 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", loginFrame.getLocale()); message += "\n" + "请从以下几个方面进行排查:\n" + "1、检查数据库服务器是否正常启动、运行\n" + "2、检查服务器端系统是否正常启动、运行\n" + "3、检查服务器端数据库连接、配置是否正确\n" + "4、检查服务器端数据库连接池、会话(Session)连接是否正常" + ""; VCIOptionPane.showError(loginFrame, message); return; } else if (vex instanceof VCIException) { VCIOptionPane.showError(loginFrame, "RMIPFramework", (VCIException) vex); } } finally{ this.logonPanel.clearLoadingIcon(); logonPanel.logonButton.setEnabled(true); } } private void checkMessage() { Thread t2 = new Thread() { public void run() { String timeCountstr = ""; try { timeCountstr = ClientSession.getFrameworkService().getConfigValue("extendservicecount"); } catch (VCIError e1) { e1.printStackTrace(); } if (timeCountstr == null || timeCountstr.length() == 0) { return; } int timeCount = Integer.parseInt(timeCountstr); if (timeCount < 1) { return; } String service = null; for (int i = 0; i < timeCount; i++) { try { service = ConfigUtils.getConfigValue("extendservice" + i); Class cl = Class.forName(service); Object obj = cl.newInstance(); cl.getDeclaredMethod("showMessage").invoke(obj); } catch (Exception e) { e.printStackTrace(); } } } }; t2.start(); } private void startTimeoutCheckThread() { try { // 是否启用会话过期检查 String isSessionTimeOutCheck = getAppConfigDetVal("sessionTimeOutCheck", "false"); if ("true".equals(isSessionTimeOutCheck)) { // 会话过期检查间隔,单位为秒 long timeOutInterval = Long.valueOf(getAppConfigDetVal("TimeOutInterval", "10")); long timeOutIntervalMillis = timeOutInterval * 1000; // 会话有效存活时间(单位为分钟) final long sessionAliveMax = Long.valueOf(getAppConfigDetVal("sessionAliveMax", "10")); final long sessionAliveMaxMillis = sessionAliveMax * 60 * 1000; final Timer tm = new Timer(); tm.schedule(new TimerTask() { boolean running = false; @Override public void run() { if (running) { return; } // 取客户端用户最后一次的请服务器端的时间 long clientLastRequestTS = Long.valueOf(ClientContextVariable.getClientLastRequestTS()); // 此处只能取客户端的时间对象,因为在客户端服务端的CORBA拦截器中也是取的客户端时间 long now = System.currentTimeMillis(); // 时间间隔 long span = now - clientLastRequestTS; // 比较时间之间的差值,如果大于 sessionAliveMax * 60 * 60, // 则视为会话过期,此时提示需要重新登录 if (span > sessionAliveMaxMillis) { running = true; tm.cancel(); relogin(ClientContextVariable.getFrame()); } } }, // 第一次执行时的延时时间 0, // 检查任务执行间隔 timeOutIntervalMillis); } } catch (Throwable e) { e.printStackTrace(); } } private void relogin(Frame frame) { VCIJOptionPane.showMessage(frame, "会话已过期,请重新登录!"); frame.setVisible(false); frame = null; writeLogouLog(); //modify by weidy@2018-11-22,会话超期后,此处会新建一个窗口,这样会显示多个窗口 LogonApplication.userEntityObject = null; LogonApplication.currentUserRoleRights = null; LogonApplication.show(new String[0]); //new LogonApplication(); } private void writeLogouLog() { String message = "登出"; LogonApplication.getUserEntityObject().setModules( LogonPanel.class.getName()); try { new RightManagementClientDelegate( LogonApplication.getUserEntityObject()).savelog(message); } catch (VCIException e) { VCIOptionPane.showError(LogonApplication.frame, "RMIPFramework", e); } } private String getAppConfigDetVal(String key, String defVal) throws VCIException { String res = ""; AppConfigDetailClientDelegate acdDel = new AppConfigDetailClientDelegate( LogonApplication.getUserEntityObject()); AppConfigDetailObject acdObj = acdDel.getAppConfigDetailByKey(key); if (acdObj != null && "".equals(acdObj.getId())) { res = defVal; } else { res = acdObj.getValue(); } return res; } }