Search in sources :

Example 1 with UserOnlineDo

use of com.albedo.java.modules.sys.domain.UserOnlineDo in project albedo by somowhere.

the class LoginUtil method getUserOnline.

public static UserOnlineDo getUserOnline(Authentication authentication) {
    UserOnlineDo online = new UserOnlineDo();
    HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    HttpSession session = request.getSession(false);
    UserDetail user = SecurityUtil.getUser(authentication);
    online.setSessionId(String.valueOf(session.getId()));
    online.setDeptId(user.getDeptId());
    online.setDeptName(user.getDeptName());
    online.setUserId(user.getId());
    online.setUsername(user.getUsername());
    online.setStartTimestamp(LocalDateTimeUtil.of(session.getCreationTime()));
    online.setLastAccessTime(LocalDateTimeUtil.of(session.getLastAccessedTime()));
    online.setExpireTime((long) session.getMaxInactiveInterval());
    online.setIpAddress(WebUtil.getIp(request));
    online.setIpLocation(AddressUtil.getRegion(online.getIpAddress()));
    online.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT));
    UserAgent userAgent = UserAgentUtil.parse(online.getUserAgent());
    online.setBrowser(userAgent.getBrowser().getName());
    online.setOs(userAgent.getOs().getName());
    return online;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UserDetail(com.albedo.java.common.security.service.UserDetail) UserOnlineDo(com.albedo.java.modules.sys.domain.UserOnlineDo) HttpSession(javax.servlet.http.HttpSession) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) UserAgent(cn.hutool.http.useragent.UserAgent)

Example 2 with UserOnlineDo

use of com.albedo.java.modules.sys.domain.UserOnlineDo 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 3 with UserOnlineDo

use of com.albedo.java.modules.sys.domain.UserOnlineDo in project albedo by somowhere.

the class SysUserOnlineListener method saveSysUserOnline.

@Async
@Order
@EventListener(SysUserOnlineEvent.class)
public void saveSysUserOnline(SysUserOnlineEvent event) {
    UserOnlineDo userOnlineDo = (UserOnlineDo) event.getSource();
    userOnlineService.saveByEvent(userOnlineDo);
}
Also used : UserOnlineDo(com.albedo.java.modules.sys.domain.UserOnlineDo) Order(org.springframework.core.annotation.Order) Async(org.springframework.scheduling.annotation.Async) EventListener(org.springframework.context.event.EventListener)

Example 4 with UserOnlineDo

use of com.albedo.java.modules.sys.domain.UserOnlineDo in project albedo by somowhere.

the class UserOnlineResource method batchForceLogout.

@PreAuthorize("@pms.hasPermission('sys_userOnline_logout')")
@LogOperate(value = "在线用户强退")
@PutMapping("/batch-force-logout")
public Result<String> batchForceLogout(@RequestBody Set<String> ids, HttpServletRequest request) {
    for (String id : ids) {
        UserOnlineDo online = userOnlineService.getById(id);
        if (online == null) {
            return Result.buildFail("用户已下线");
        }
        SessionInformation sessionInformation = sessionRegistry.getSessionInformation(online.getSessionId());
        if (sessionInformation != null) {
            if (sessionInformation.getSessionId().equals(request.getSession(false).getId())) {
                return Result.buildFail("当前登陆用户无法强退");
            }
            sessionInformation.expireNow();
            redisTemplate.boundHashOps(RedisSessionRegistry.SESSIONIDS).put(online.getSessionId(), sessionInformation);
        }
        online.setStatus(OnlineStatus.OFFLINE);
        userOnlineService.updateById(online);
    }
    return Result.buildOk("操作成功");
}
Also used : SessionInformation(org.springframework.security.core.session.SessionInformation) UserOnlineDo(com.albedo.java.modules.sys.domain.UserOnlineDo) LogOperate(com.albedo.java.common.log.annotation.LogOperate) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 5 with UserOnlineDo

use of com.albedo.java.modules.sys.domain.UserOnlineDo in project albedo by somowhere.

the class UserOnlineServiceImpl method offlineBySessionId.

@Override
public void offlineBySessionId(String sessionId) {
    UserOnlineDo userOnlineDo = getById(sessionId);
    if (userOnlineDo != null) {
        userOnlineDo.setStatus(OnlineStatus.OFFLINE);
        repository.updateById(userOnlineDo);
    }
}
Also used : UserOnlineDo(com.albedo.java.modules.sys.domain.UserOnlineDo)

Aggregations

UserOnlineDo (com.albedo.java.modules.sys.domain.UserOnlineDo)8 SessionInformation (org.springframework.security.core.session.SessionInformation)4 LogOperate (com.albedo.java.common.log.annotation.LogOperate)2 SysUserOnlineEvent (com.albedo.java.common.security.event.SysUserOnlineEvent)2 UserDetail (com.albedo.java.common.security.service.UserDetail)2 EventListener (org.springframework.context.event.EventListener)2 Order (org.springframework.core.annotation.Order)2 Async (org.springframework.scheduling.annotation.Async)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 UserAgent (cn.hutool.http.useragent.UserAgent)1 LogLoginDo (com.albedo.java.modules.sys.domain.LogLoginDo)1 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpSession (javax.servlet.http.HttpSession)1 Authentication (org.springframework.security.core.Authentication)1 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)1