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;
}
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));
}
}
}
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);
}
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("操作成功");
}
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);
}
}
Aggregations