Search in sources :

Example 1 with SysUserOnlineEvent

use of com.albedo.java.common.security.event.SysUserOnlineEvent in project albedo by somowhere.

the class RedisSessionRegistry method registerNewSession.

@Override
public void registerNewSession(String sessionId, Object principal) {
    ArgumentAssert.notEmpty(sessionId, "SessionId required as per interface contract");
    ArgumentAssert.notNull(principal, "Principal required as per interface contract");
    ArgumentAssert.isTrue(principal instanceof UserDetail, "Principal required as UserDetail");
    if (log.isDebugEnabled()) {
        log.debug("Registering session " + sessionId + ", for principal " + principal);
    }
    UserDetail userDetail = (UserDetail) principal;
    if (getSessionInformation(sessionId) != null) {
        removeSessionInformation(sessionId);
    }
    SessionInformation sessionInformation = new CustomSessionInformation(userDetail.getId(), sessionId, new Date());
    redisTemplate.boundHashOps(getSessionIdsKey()).put(sessionId, sessionInformation);
    Set<String> sessionsUsedByPrincipal = getPrincipals(userDetail.getId());
    if (sessionsUsedByPrincipal == null) {
        sessionsUsedByPrincipal = new CopyOnWriteArraySet();
        Set<String> prevSessionsUsedByPrincipal = this.putIfAbsentPrincipals(userDetail.getId(), sessionsUsedByPrincipal);
        if (prevSessionsUsedByPrincipal != null) {
            sessionsUsedByPrincipal = prevSessionsUsedByPrincipal;
        }
    }
    sessionsUsedByPrincipal.add(sessionId);
    if (log.isTraceEnabled()) {
        log.trace("Sessions used by '" + principal + "' : " + sessionsUsedByPrincipal);
    }
    Authentication authentication = SecurityUtil.getAuthentication();
    if (authentication != null && authentication.isAuthenticated()) {
        UserOnlineDo userOnlineDo = userOnlineService.getById(sessionId);
        if (userOnlineDo == null) {
            userOnlineDo = LoginUtil.getUserOnline(authentication);
            SpringContextHolder.publishEvent(new SysUserOnlineEvent(userOnlineDo));
        }
    }
}
Also used : UserDetail(com.albedo.java.common.security.service.UserDetail) SessionInformation(org.springframework.security.core.session.SessionInformation) SysUserOnlineEvent(com.albedo.java.common.security.event.SysUserOnlineEvent) UserOnlineDo(com.albedo.java.modules.sys.domain.UserOnlineDo) Authentication(org.springframework.security.core.Authentication) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet)

Example 2 with SysUserOnlineEvent

use of com.albedo.java.common.security.event.SysUserOnlineEvent in project albedo by somowhere.

the class AjaxAuthenticationSuccessHandler method onAuthenticationSuccess.

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
    response.setStatus(HttpServletResponse.SC_OK);
    String useruame = request.getParameter("username");
    LoginUtil.isValidateCodeLogin(useruame, false, true);
    UserOnlineDo userOnlineDo = LoginUtil.getUserOnline(authentication);
    SpringContextHolder.publishEvent(new SysUserOnlineEvent(userOnlineDo));
    LogLoginDo logLoginDo = SysLogUtils.getSysLogLogin();
    logLoginDo.setParams(HttpUtil.toParams(request.getParameterMap()));
    logLoginDo.setUsername(useruame);
    logLoginDo.setTitle("用户登录");
    AsyncUtil.recordLogLogin(logLoginDo);
    WebUtil.renderJson(response, Result.buildOk("登录成功"));
}
Also used : SysUserOnlineEvent(com.albedo.java.common.security.event.SysUserOnlineEvent) LogLoginDo(com.albedo.java.modules.sys.domain.LogLoginDo) UserOnlineDo(com.albedo.java.modules.sys.domain.UserOnlineDo)

Aggregations

SysUserOnlineEvent (com.albedo.java.common.security.event.SysUserOnlineEvent)2 UserOnlineDo (com.albedo.java.modules.sys.domain.UserOnlineDo)2 UserDetail (com.albedo.java.common.security.service.UserDetail)1 LogLoginDo (com.albedo.java.modules.sys.domain.LogLoginDo)1 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)1 Authentication (org.springframework.security.core.Authentication)1 SessionInformation (org.springframework.security.core.session.SessionInformation)1