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
302
303
304
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<String> loginNameList = new ArrayList<String>(loginNameArr.length);  
                Collections.addAll(loginNameList, loginNameArr);
                //获取客户端访问机器的所有IP地址
                List<String> clientIpList = new ArrayList<String>();
                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<String> ipList = new ArrayList<String>(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;
    }
}