1
2
3
4
5 package gboat2.web.action;
6
7 import gboat2.base.bridge.GboatAppContext;
8 import gboat2.base.bridge.model.NaviResource;
9 import gboat2.base.bridge.model.UserSession;
10 import gboat2.base.bridge.util.json.JsonUtil;
11 import gboat2.base.core.web.BaseActionSupport;
12 import gboat2.base.core.web.JsonResult;
13 import gboat2.base.core.web.JsonResultSupport;
14 import gboat2.web.Constants;
15 import gboat2.web.business.IAuthorityBusiness;
16 import gboat2.web.business.IGroupBusiness;
17 import gboat2.web.business.ILoginBusiness;
18 import gboat2.web.business.IProfileBusiness;
19 import gboat2.web.business.IRoleBusiness;
20 import gboat2.web.business.IShortcutBusiness;
21 import gboat2.web.business.ISystemConfigBusiness;
22 import gboat2.web.business.IUserBusiness;
23 import gboat2.web.exception.ValidateException;
24 import gboat2.web.model.AuthorityResourceVO;
25 import gboat2.web.model.Group;
26 import gboat2.web.model.Profile;
27 import gboat2.web.model.Resource;
28 import gboat2.web.model.Role;
29 import gboat2.web.model.ShortcutResourceVO;
30 import gboat2.web.model.SystemConfig;
31 import gboat2.web.model.User;
32 import gboat2.web.model.UserGroupOrganMapper;
33 import gboat2.web.model.UserRoleGroupOrganVO;
34 import gboat2.web.service.ISessionService;
35 import gboat2.web.service.IUserAuthService;
36 import gboat2.web.service.PrivilegeException;
37 import gboat2.web.service.impl.UserAuthServiceImpl;
38
39 import java.io.IOException;
40 import java.net.URL;
41 import java.text.SimpleDateFormat;
42 import java.util.ArrayList;
43 import java.util.Date;
44 import java.util.Iterator;
45 import java.util.List;
46 import java.util.Locale;
47 import java.util.Map;
48
49 import net.sf.json.JSONObject;
50
51 import org.apache.commons.lang3.StringUtils;
52 import org.apache.struts2.convention.annotation.Result;
53 import org.apache.struts2.convention.annotation.ResultPath;
54 import org.apache.struts2.convention.annotation.Results;
55 import org.osgi.framework.BundleContext;
56 import org.osgi.framework.InvalidSyntaxException;
57 import org.osgi.framework.ServiceReference;
58 import org.springframework.beans.factory.annotation.Autowired;
59
60
61
62
63
64
65
66
67 @ResultPath("/content")
68 @Results(value = { @Result(name = "error", location = "login.jsp"),
69 @Result(name = "desktop", location = "/g-index!desktop.do", type = "redirect", params={"openUrl", "%{openUrl}", "openParams", "%{openParams}"}),
70 @Result(name = "normal", location = "/g-index!normal.do", type = "redirect", params={"openUrl", "%{openUrl}", "openParams", "%{openParams}"}),
71 @Result(name = "usersystem", location = "${namespace}"+"/g-index!index.do?mapperId=${mapperId}", type = "redirect"),
72 @Result(name = "logout", location = "/g-index.do", type = "redirect"), @Result(name = "no_user", location = "index.jsp") })
73 public class LoginAction extends BaseActionSupport {
74
75 private static final long serialVersionUID = 1L;
76
77
78 private String username;
79
80
81 private String password;
82
83
84 private String mapperId;
85
86
87 private String systemId;
88
89 private List<UserRoleGroupOrganVO> userStatus;
90
91
92 private String errorMessage = null;
93
94
95 private String extJSVer = "4.1";
96
97
98 @Autowired
99 private IUserBusiness userService;
100
101
102 @Autowired
103 private IShortcutBusiness shortcutService;
104
105
106 @Autowired
107 private ISessionService sessionService;
108
109
110 @Autowired
111 private IAuthorityBusiness authorityService;
112
113 @Autowired
114 private IProfileBusiness profileService;
115
116 @Autowired
117 private ISystemConfigBusiness systemConfigService;
118
119 @Autowired
120 private IGroupBusiness gropBusi;
121
122 @Autowired
123 private IRoleBusiness roleBusi;
124
125 @Autowired
126 private ILoginBusiness loginBusiness;
127
128 private String roleId;
129
130 private String groupId;
131
132 private String openUrl;
133 private String openParams;
134 private String namespace = "/gbmp";
135
136
137
138
139 public String login() {
140
141 String uri = request.getRequestURI();
142 String base = request.getContextPath();
143 String namespace = uri.replaceAll(base, "").replace("/login!login.do", "");
144 String indexUrl = namespace + "/g-index.do";
145 this.setNamespace(namespace);
146
147 SystemConfig system = systemConfigService.getSystemByIndexUrl(indexUrl);
148 if (null == system || StringUtils.isBlank(system.getSystemId())) {
149 system = new SystemConfig();
150 system.setSystemId(Constants.GBOAT2_SYSTEM_ID);
151 }
152 UserSession userSession = null;
153
154
155 if (Constants.SUPER.equals(username)) {
156 userSession = loginBusiness.superSession(null, system.getSystemId());
157 setSession(userSession);
158 return getPageAppMode(userSession);
159 }
160 this.setSystemId(system.getSystemId());
161
162 List<UserGroupOrganMapper> statusList = null;
163 if (StringUtils.isEmpty(mapperId)) {
164 if (StringUtils.isEmpty(username)) {
165 errorMessage = "登录失败,找不到用户身份!";
166 addActionError("登录失败,找不到用户身份!");
167 return "error";
168 }
169 statusList = getUserAuth().convertOuterAuthToInnerAuth(username);
170 UserGroupOrganMapper userGroupOrganMapper = statusList.get(0);
171 mapperId = userGroupOrganMapper.getMapperId();
172 }
173 UserGroupOrganMapper mapper = (UserGroupOrganMapper) userService.get(UserGroupOrganMapper.class, mapperId);
174
175 if (null == mapper) {
176 errorMessage = "登录失败,找不到用户身份!";
177 addActionError("登录失败,找不到用户身份!");
178 return "error";
179 }
180
181 User user = null;
182 if (StringUtils.isNotEmpty(username)) {
183 user = userService.getUserByLoginId(getUserAuth().convertOuterUserNameToInnerUserName(username));
184 } else {
185 user = userService.getUserByUserId(mapper.getUserId());
186 }
187 try {
188 userSession = sessionService.login(user, mapper);
189
190 userSession.setSystemId(system.getSystemId());
191 } catch (PrivilegeException e) {
192 errorMessage = e.getMessage();
193 addActionError(e.getMessage());
194 return "error";
195 }
196
197 userSession = sessionService.loadNavigationMenuToSession(userSession, mapper);
198
199 setSession(userSession);
200 return getPageAppMode(userSession);
201 }
202
203
204
205
206
207
208 public void rolesBeforeLogin() {
209
210
211
212
213
214
215 try {
216 getUserAuth().validateUser(username, password);
217
218
219 resetValidCompnent();
220 } catch (ValidateException e) {
221
222 outputFailureAndCheckValidCompnent(e.getMessage());
223 }
224
225
226 if (Constants.SUPER.equals(username)) {
227 GboatAppContext.output(JsonResult.SUCCESS);
228 return;
229 }
230
231
232 User user = userService.getUserByLoginId(username);
233 userStatus = userService.getMapperVOByUserId(user.getUserId());
234 outputRolesBeforeLogin(userStatus);
235 }
236
237
238
239
240 protected void outputRolesBeforeLogin(List<UserRoleGroupOrganVO> userStatus) {
241 List<UserRoleGroupOrganVO> zbdlUserStatus = null;
242 List<UserRoleGroupOrganVO> tbrUserStatus = null;
243 if (null != userStatus && userStatus.size() > 0) {
244 for(UserRoleGroupOrganVO status : userStatus){
245 if (null == status) {
246 continue;
247 }
248 if (status.getGroupCode().equals("zbdlg") && (zbdlUserStatus == null || zbdlUserStatus.size() <= 0)) {
249 zbdlUserStatus = new ArrayList<UserRoleGroupOrganVO>();
250 zbdlUserStatus.add(status);
251 } else if (status.getGroupCode().equals("tbrg") && (tbrUserStatus == null || tbrUserStatus.size() <= 0)) {
252 tbrUserStatus = new ArrayList<UserRoleGroupOrganVO>();
253 tbrUserStatus.add(status);
254 }
255 }
256 }
257 if (null != tbrUserStatus && tbrUserStatus.size() > 0) {
258 userStatus = tbrUserStatus;
259 } else if (null != zbdlUserStatus && zbdlUserStatus.size() > 0) {
260 userStatus = zbdlUserStatus;
261 }
262 GboatAppContext.output(JsonResult.createSuccess(userStatus));
263
264 }
265
266
267 private void resetValidCompnent() {
268 request.getSession().setAttribute("checkindex", null);
269 }
270
271
272 private void outputFailureAndCheckValidCompnent(String errMsg) {
273
274 String checkindex = (String) request.getSession().getAttribute("checkindex");
275 String checkCode = "false";
276 if (StringUtils.isEmpty(checkindex)) {
277 request.getSession().setAttribute("checkindex", "1");
278 } else {
279 int checkindexInt = Integer.parseInt(checkindex);
280 if (checkindexInt > 1) {
281 checkCode = "true";
282 }
283 request.getSession().setAttribute("checkindex", String.valueOf(++checkindexInt));
284 }
285 JSONObject failureJSONObj = JsonResultSupport.wrap(false, errMsg);
286 failureJSONObj.accumulate("showCheckCode", checkCode);
287 JsonResultSupport.output(failureJSONObj);
288 }
289
290
291 private boolean checkValidNumber() {
292
293 String loginId = (String) request.getSession().getAttribute("loginId");
294 if (StringUtils.isNotEmpty(loginId))
295 return true;
296 String checkindex = (String) request.getSession().getAttribute("checkindex");
297 String kaptchaKey = (String) request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
298
299 if (StringUtils.isNotEmpty(kaptchaKey) && StringUtils.isNotEmpty(checkindex) && Integer.parseInt(checkindex) > 2) {
300 String result = checkKaptcha();
301 if (StringUtils.isNotEmpty(result)) {
302 GboatAppContext.output(JsonResult.createFailure(result));
303 return false;
304 }
305 }
306 return true;
307 }
308
309
310
311
312 private String checkKaptcha() {
313 String kaptchaKey = (String) request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
314 String errMsg = null;
315 String kaptchafield = request.getParameter("kaptchafield");
316 if (StringUtils.isNotEmpty(kaptchaKey)) {
317 if (StringUtils.isEmpty(kaptchafield) || StringUtils.isEmpty(kaptchaKey)) {
318 errMsg = "请输入验证码!";
319 }
320 if (!kaptchaKey.equals(kaptchafield)) {
321 errMsg = "验证码输入错误,请重新输入!";
322 }
323 if (StringUtils.isNotEmpty(errMsg)) {
324 return errMsg;
325 }
326 }
327 return errMsg;
328 }
329
330
331
332
333 public void validateKaptcha() {
334 GboatAppContext.output(JsonResult.SUCCESS);
335
336
337
338
339
340
341
342
343 }
344
345
346
347
348 public void reLoginForTimeout() {
349 User user = userService.getUserByLoginId(username);
350
351 List<UserGroupOrganMapper> statusList = null;
352 if (null != user) {
353 statusList = userService.getURGOMapperByUserId(user.getUserId());
354 }
355 String errMsg = loginBusiness.validateUser(username, user, password, statusList);
356 if (StringUtils.isNotEmpty(errMsg)) {
357 JsonResultSupport.outputFailure(errMsg);
358 return;
359 }
360
361 UserSession session = null;
362 try {
363 if (Constants.SUPER.equals(username)) {
364 session = loginBusiness.superSession(null, systemId);
365 } else {
366 session = sessionService.login(user.getUserId(), groupId, roleId, systemId);
367 }
368 setSession(session);
369 JsonResultSupport.outputSuccess();
370 } catch (Exception e) {
371 JsonResultSupport.outputFailure(e.getMessage());
372 }
373 }
374
375
376
377
378 public String swapRole() {
379 UserSession session = GboatAppContext.getUserSession();
380 try {
381 session = sessionService.login(session.getUserId(), groupId, roleId, session.getSystemId());
382 } catch (PrivilegeException e) {
383 errorMessage = e.getMessage();
384 return "error";
385 }
386 setSession(session);
387 return getPageAppMode(session);
388 }
389
390
391
392
393 public String swapSystem() {
394 UserSession session = GboatAppContext.getUserSession();
395 try {
396 Group group = gropBusi.findGroupByGroupCode(session.getGroupCode());
397 Role role = roleBusi.findRoleByRoleCode(session.getRoleCode());
398 session = sessionService.login(session.getUserId(), group.getGroupId(), role.getRoleId(), systemId);
399 } catch (PrivilegeException e) {
400 errorMessage = e.getMessage();
401 return "error";
402 }
403 setSession(session);
404 return getPageAppMode(session);
405 }
406
407
408
409
410
411 protected String getPageAppMode(UserSession userSession) {
412 if(!Constants.SUPER.equals(username)){
413 return "usersystem";
414 }
415
416
417 Profile profile = profileService.getProfileByUserId(userSession.getUserId());
418 SystemConfig systemConfig = null;
419 if (StringUtils.isNotEmpty(userSession.getSystemId())) {
420 systemConfig = systemConfigService.findSysCfgById(userSession.getSystemId());
421 }
422 boolean isSysNormalMode = systemConfig != null && Constants.PAGE_APP_MODE_NORMAL.equals(systemConfig.getPageAppMode());
423 boolean iSystemConfigNotNull = systemConfig != null && !StringUtils.isEmpty(systemConfig.getPageAppMode());
424 boolean isProfileNotNull = profile != null && !StringUtils.isEmpty(profile.getPageAppMode());
425 boolean isNormalMode = profile == null || Constants.PAGE_APP_MODE_NORMAL.equals(profile.getPageAppMode());
426
427 if (isProfileNotNull) {
428 return this.redirectMode(userSession, isNormalMode);
429 }
430
431 if (iSystemConfigNotNull) {
432 return this.redirectMode(userSession, isSysNormalMode);
433 }
434 userSession.setAttribute(Constants.CURRENT_PAGE_APP_MODE_KEY, Constants.PAGE_APP_MODE_DESKTOP);
435 setSession(userSession);
436 return "desktop";
437 }
438
439
440
441
442
443
444
445 private String redirectMode(UserSession userSession, Boolean mode) {
446 if (mode) {
447 userSession.setAttribute(Constants.CURRENT_PAGE_APP_MODE_KEY, Constants.PAGE_APP_MODE_NORMAL);
448 setSession(userSession);
449 return "normal";
450 } else {
451 userSession.setAttribute(Constants.CURRENT_PAGE_APP_MODE_KEY, Constants.PAGE_APP_MODE_DESKTOP);
452 setSession(userSession);
453 return "desktop";
454 }
455 }
456
457
458
459
460
461 protected void setSession(UserSession userSession) {
462 if (null != userSession) {
463 request.getSession().setAttribute(UserSession.USER_SESSION_KEY, userSession);
464 String sessionId = request.getSession().getId();
465 double rdv = (double) (Math.random() * 10000000);
466 String key = sessionId + "_" + rdv;
467 userSession.setSsoSessionID(key);
468 }
469 }
470
471
472
473
474
475
476 public void showShotcutByUser() throws IOException {
477 UserSession session = GboatAppContext.getUserSession();
478 if (session.getLoginId().equals(Constants.SUPER)) {
479 URL url = this.getClass().getClassLoader().getResource(Constants.SUPER_SHORTCUT);
480 Object result = (url == null ? JsonResult.createFailure("获取超级管理员的功能菜单的数据定义失败!") : JsonUtil.fromURL(url));
481 GboatAppContext.output(result);
482 } else {
483 Group group = gropBusi.findGroupByGroupCode(session.getGroupCode());
484 Role role = roleBusi.findRoleByRoleCode(session.getRoleCode());
485 String authForId = sessionService.computeAuthForId(group.getGroupId(), role.getRoleId(), session.getSystemId());
486 List<AuthorityResourceVO> authoritys = authorityService.getAuthorityByStatus(authForId, session.getSystemId());
487
488 List<ShortcutResourceVO> shortResForSys = loginBusiness.showResourceShortcutByAuth(authoritys);
489 List<ShortcutResourceVO> shortResForUser = shortcutService.getShortcutResourceByUser(session, authoritys);
490 shortResForSys.addAll(shortResForUser);
491 shortResForSys.add(loginBusiness.addModule());
492 GboatAppContext.output(shortResForSys);
493 }
494 }
495
496 public String getDeniedPriority() {
497 Map<String, List<String>> map = sessionService.getDeniedPriority(request);
498 if (GboatAppContext.getUserSession().getLoginId().equals(Constants.SUPER)) {
499 JSONObject json = JsonResultSupport.wrap(new JSONObject());
500 JsonResultSupport.output(json);
501 } else if (null != map) {
502 JsonResultSupport.output(map);
503 } else {
504 JsonResultSupport.outputFailure();
505 }
506 return null;
507 }
508
509 public String logout() {
510 UserSession session = GboatAppContext.getUserSession();
511 request.getSession().invalidate();
512 String systemId = null;
513 if (session != null && !StringUtils.isEmpty(session.getSystemId())) {
514 systemId = session.getSystemId();
515 } else {
516 systemId = this.systemId;
517 }
518
519 if (!StringUtils.isEmpty(systemId)) {
520 SystemConfig system = (SystemConfig) systemConfigService.get(SystemConfig.class, systemId);
521 if (system != null) {
522 try {
523
524 response.sendRedirect(request.getContextPath() + system.getIndexUrl());
525
526 } catch (IOException e) {
527 }
528 }
529 } else {
530
531 return "logout";
532 }
533
534
535 return null;
536 }
537
538
539
540
541
542
543
544
545
546 protected boolean isValidateCA(String certStartDate, String certEndDate) {
547 if (StringUtils.isEmpty(certStartDate) || StringUtils.isEmpty(certEndDate))
548 return false;
549
550
551 SimpleDateFormat simpleDateFormatGMT = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss z", Locale.ENGLISH);
552 try {
553 String validateStartString = certStartDate.replace("(中国标准时间)", "");
554 validateStartString = validateStartString.replace("GMT+0800", "GMT+08:00");
555 String validateEndString = certEndDate.replace("(中国标准时间)", "");
556 validateEndString = validateEndString.replace("GMT+0800", "GMT+08:00");
557 Date validateStartDate = simpleDateFormatGMT.parse(validateStartString.trim());
558 Date validateEndDate = simpleDateFormatGMT.parse(validateEndString.trim());
559 Date today = new Date();
560 return (today.after(validateStartDate) && today.before(validateEndDate));
561 } catch (Exception e) {
562
563 SimpleDateFormat simpleDateFormatUTC = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'UTC'Z yyyy", Locale.ENGLISH);
564 try {
565 Date validateStartDate = simpleDateFormatUTC.parse(certStartDate.trim());
566 Date validateEndDate = simpleDateFormatUTC.parse(certEndDate.trim());
567 Date today = new Date();
568 return (today.after(validateStartDate) && today.before(validateEndDate));
569 } catch (Exception e2) {
570 return false;
571 }
572
573 }
574 }
575
576 public String getUsername() {
577 return username;
578 }
579
580 public void setUsername(String username) {
581 this.username = username;
582 }
583
584 public String getPassword() {
585 return password;
586 }
587
588 public void setPassword(String password) {
589 this.password = password;
590 }
591
592 public String getErrorMessage() {
593 return errorMessage;
594 }
595
596 public void setErrorMessage(String errorMessage) {
597 this.errorMessage = errorMessage;
598 }
599
600 public List<UserRoleGroupOrganVO> getUserStatus() {
601 return userStatus;
602 }
603
604 public void setUserStatus(List<UserRoleGroupOrganVO> userStatus) {
605 this.userStatus = userStatus;
606 }
607
608 public String getMapperId() {
609 return mapperId;
610 }
611
612 public void setMapperId(String mapperId) {
613 this.mapperId = mapperId;
614 }
615
616 public String getExtJSVer() {
617 return extJSVer;
618 }
619
620 public void setExtJSVer(String extJSVer) {
621 this.extJSVer = extJSVer;
622 }
623
624 public String getSystemId() {
625 return systemId;
626 }
627
628 public void setSystemId(String systemId) {
629 this.systemId = systemId;
630 }
631
632 public String getRoleId() {
633 return roleId;
634 }
635
636 public void setRoleId(String roleId) {
637 this.roleId = roleId;
638 }
639
640 public String getGroupId() {
641 return groupId;
642 }
643
644 public void setGroupId(String groupId) {
645 this.groupId = groupId;
646 }
647
648
649
650
651
652 private IUserAuthService getUserAuth() {
653
654 try {
655 BundleContext bundleContext = this.context;
656 ServiceReference[] sfs = bundleContext.getServiceReferences(IUserAuthService.class.getName(), null);
657 for (ServiceReference sf : sfs) {
658 IUserAuthService service = (IUserAuthService) bundleContext.getService(sf);
659 String serviceImplName = service.toString();
660 serviceImplName = serviceImplName.substring(0, serviceImplName.indexOf("@"));
661 if (serviceImplName.equals(getDefaultUserAuthServiceImpl()))
662 return service;
663 }
664 } catch (InvalidSyntaxException e) {
665 e.printStackTrace();
666 }
667
668 return null;
669 }
670
671
672
673
674 protected String getDefaultUserAuthServiceImpl() {
675 return UserAuthServiceImpl.class.getName();
676 }
677
678 public String getOpenUrl() {
679 return openUrl;
680 }
681
682 public void setOpenUrl(String openUrl) {
683 this.openUrl = openUrl;
684 }
685
686 public String getOpenParams() {
687 return openParams;
688 }
689
690 public void setOpenParams(String openParams) {
691 this.openParams = openParams;
692 }
693
694 public String getNamespace() {
695 return namespace;
696 }
697
698 public void setNamespace(String namespace) {
699 this.namespace = namespace;
700 }
701
702 }