private void loginUser(Authentication authentication) { if (authentication == null) return; if (!(authentication.getPrincipal() instanceof Person)) return; Person person = (Person) authentication.getPrincipal(); String username = person.getUsername(); String name = person.getName(); if (authentication.getDetails() instanceof WebAuthenticationDetails) { WebAuthenticationDetails details = (WebAuthenticationDetails) authentication.getDetails(); String ip = details.getRemoteAddress(); String sessionId = details.getSessionId(); if (onLineManager.getOnlineUser(sessionId) != null) return; onLineManager.loginUser(ip, sessionId, person); logManager.log(username, name, ip, "登录系统", ""); if (logger.isDebugEnabled()) logger.debug( "用户 {}[{}] 登录系统,登录IP:{},session:{}", new Object[] {name, username, ip, sessionId}); } }
public void onApplicationEvent(ApplicationEvent e) { if (e instanceof AuthenticationSuccessEvent) { // 登录成功后的事件处理 AuthenticationSuccessEvent event = (AuthenticationSuccessEvent) e; Authentication authentication = event.getAuthentication(); loginUser(authentication); } else if (e instanceof HttpSessionCreatedEvent) { HttpSession session = ((HttpSessionCreatedEvent) e).getSession(); OnLineInfo onlineInfo = onLineManager.getOnlineUser(session.getId()); if (onlineInfo == null) return; Person person = onlineInfo.getPerson(); String username = person.getUsername(); // // //把当前登录用户的CSS主题写入Session中 // String cssTheme = settingManager.getPersonSettingValue(username, MyConstants.CSS_THEME); // if (cssTheme == null) // cssTheme = ""; // // session.setAttribute(MyConstants.CSS_THEME, cssTheme); // // //把当前登录用户的在线消息定时接收时间间隔写入Session中 // int messageCheckInterval = settingManager.getPersonSettingIntValue(username, // MyConstants.MESSAGE_CHECK_INTERVAL); // session.setAttribute(MyConstants.MESSAGE_CHECK_INTERVAL, messageCheckInterval); // // //把当前登录用户的电子邮件定时接收时间间隔写入Session中 // int mailCheckInterval = settingManager.getPersonSettingIntValue(username, // MyConstants.MAIL_CHECK_INTERVAL); // session.setAttribute(MyConstants.MAIL_CHECK_INTERVAL, mailCheckInterval); } else if (e instanceof HttpSessionDestroyedEvent) { SecurityContext securityContext = ((HttpSessionDestroyedEvent) e).getSecurityContext(); if (securityContext == null) return; Authentication authentication = securityContext.getAuthentication(); if (authentication == null) return; if (authentication.getDetails() instanceof WebAuthenticationDetails) { WebAuthenticationDetails details = (WebAuthenticationDetails) authentication.getDetails(); String sessionId = details.getSessionId(); logoutUser(sessionId); } } }
/** * Internal conversion. This method will allow to save additional data. By default, it will save * the object as string * * @param data the data to convert * @return a map of String, String */ public Map<String, String> convertDataToStrings(Map<String, Object> data) { Map<String, String> results = new HashMap<>(); if (data != null) { for (String key : data.keySet()) { Object object = data.get(key); if (object instanceof WebAuthenticationDetails) { WebAuthenticationDetails authenticationDetails = (WebAuthenticationDetails) object; results.put("remoteAddress", authenticationDetails.getRemoteAddress()); results.put("sessionId", authenticationDetails.getSessionId()); } else if (object != null) { results.put(key, object.toString()); } else { results.put(key, "null"); } } } return results; }
public UserSessionBase processSuccessLogin( int authType, String userAgent, HttpServletRequest httpRequest) { boolean newSessionCreation = true; UserSessionBase userSession = null; XASecurityContext context = XAContextHolder.getSecurityContext(); if (context != null) { userSession = context.getUserSession(); } Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); WebAuthenticationDetails details = (WebAuthenticationDetails) authentication.getDetails(); String currentLoginId = authentication.getName(); if (userSession != null) { if (validateUserSession(userSession, currentLoginId)) { newSessionCreation = false; } } if (newSessionCreation) { // Need to build the UserSession XXPortalUser gjUser = daoManager.getXXPortalUser().findByLoginId(currentLoginId); if (gjUser == null) { logger.error("Error getting user for loginId=" + currentLoginId, new Exception()); return null; } XXAuthSession gjAuthSession = new XXAuthSession(); gjAuthSession.setLoginId(currentLoginId); gjAuthSession.setUserId(gjUser.getId()); gjAuthSession.setAuthTime(DateUtil.getUTCDate()); gjAuthSession.setAuthStatus(XXAuthSession.AUTH_STATUS_SUCCESS); gjAuthSession.setAuthType(authType); if (details != null) { gjAuthSession.setExtSessionId(details.getSessionId()); gjAuthSession.setRequestIP(details.getRemoteAddress()); } if (userAgent != null) { gjAuthSession.setRequestUserAgent(userAgent); } gjAuthSession.setDeviceType(httpUtil.getDeviceType(userAgent)); gjAuthSession = storeAuthSession(gjAuthSession); userSession = new UserSessionBase(); userSession.setXXPortalUser(gjUser); userSession.setXXAuthSession(gjAuthSession); resetUserSessionForProfiles(userSession); if (details != null) { logger.info( "Login Success: loginId=" + currentLoginId + ", sessionId=" + gjAuthSession.getId() + ", sessionId=" + details.getSessionId() + ", requestId=" + details.getRemoteAddress()); } else { logger.info( "Login Success: loginId=" + currentLoginId + ", sessionId=" + gjAuthSession.getId() + ", details is null"); } } return userSession; }