| | |
| | | import cn.dev33.satoken.exception.NotLoginException; |
| | | import cn.dev33.satoken.stp.StpUtil; |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.dromara.common.core.constant.CacheConstants; |
| | | import org.dromara.common.core.constant.GlobalConstants; |
| | | import org.dromara.common.core.domain.R; |
| | | import org.dromara.common.core.domain.dto.UserOnlineDTO; |
| | | import org.dromara.common.core.utils.StreamUtils; |
| | |
| | | import org.dromara.common.redis.utils.RedisUtils; |
| | | import org.dromara.common.web.core.BaseController; |
| | | import org.dromara.system.domain.SysUserOnline; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 在线用户监控 |
| | |
| | | @GetMapping("/list") |
| | | public TableDataInfo<SysUserOnline> list(String ipaddr, String userName) { |
| | | // 获取所有未过期的 token |
| | | List<String> keys = StpUtil.searchTokenValue("", 0, -1, false); |
| | | Collection<String> keys = RedisUtils.keys(CacheConstants.ONLINE_TOKEN_KEY + "*"); |
| | | List<UserOnlineDTO> userOnlineDTOList = new ArrayList<>(); |
| | | for (String key : keys) { |
| | | String token = StringUtils.substringAfterLast(key, ":"); |
| | | // 如果已经过期则跳过 |
| | | if (StpUtil.stpLogic.getTokenActivityTimeoutByToken(token) < -1) { |
| | | if (StpUtil.stpLogic.getTokenActiveTimeoutByToken(token) < -1) { |
| | | continue; |
| | | } |
| | | userOnlineDTOList.add(RedisUtils.getCacheObject(CacheConstants.ONLINE_TOKEN_KEY + token)); |
| | |
| | | } |
| | | return R.ok(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前用户登录在线设备 |
| | | */ |
| | | @GetMapping() |
| | | public TableDataInfo<SysUserOnline> getInfo() { |
| | | // 获取指定账号 id 的 token 集合 |
| | | List<String> tokenIds = StpUtil.getTokenValueListByLoginId(StpUtil.getLoginIdAsString()); |
| | | List<UserOnlineDTO> userOnlineDTOList = tokenIds.stream() |
| | | .filter(token -> StpUtil.stpLogic.getTokenActiveTimeoutByToken(token) >= -1) |
| | | .map(token -> (UserOnlineDTO) RedisUtils.getCacheObject(CacheConstants.ONLINE_TOKEN_KEY + token)) |
| | | .collect(Collectors.toList()); |
| | | //复制和处理 SysUserOnline 对象列表 |
| | | Collections.reverse(userOnlineDTOList); |
| | | userOnlineDTOList.removeAll(Collections.singleton(null)); |
| | | List<SysUserOnline> userOnlineList = BeanUtil.copyToList(userOnlineDTOList, SysUserOnline.class); |
| | | return TableDataInfo.build(userOnlineList); |
| | | } |
| | | |
| | | /** |
| | | * 强退当前在线设备 |
| | | * |
| | | * @param tokenId token值 |
| | | */ |
| | | @Log(title = "在线设备", businessType = BusinessType.FORCE) |
| | | @DeleteMapping("/myself/{tokenId}") |
| | | public R<Void> remove(@PathVariable("tokenId") String tokenId) { |
| | | try { |
| | | // 获取指定账号 id 的 token 集合 |
| | | List<String> keys = StpUtil.getTokenValueListByLoginId(StpUtil.getLoginIdAsString()); |
| | | keys.stream() |
| | | .filter(key -> key.equals(tokenId)) |
| | | .findFirst() |
| | | .ifPresent(key -> StpUtil.kickoutByTokenValue(tokenId)); |
| | | } catch (NotLoginException ignored) { |
| | | } |
| | | return R.ok(); |
| | | } |
| | | |
| | | } |