| | |
| | | package com.ruoyi.framework.listener; |
| | | |
| | | import cn.dev33.satoken.config.SaTokenConfig; |
| | | import cn.dev33.satoken.listener.SaTokenListener; |
| | | import cn.dev33.satoken.stp.SaLoginModel; |
| | | import cn.dev33.satoken.stp.StpUtil; |
| | |
| | | import com.ruoyi.common.constant.Constants; |
| | | import com.ruoyi.common.core.domain.dto.UserOnlineDTO; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.core.service.UserService; |
| | | import com.ruoyi.common.enums.UserType; |
| | | import com.ruoyi.common.utils.*; |
| | | import com.ruoyi.common.utils.LoginUtils; |
| | | import com.ruoyi.common.utils.ServletUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.ip.AddressUtils; |
| | | import com.ruoyi.common.utils.redis.RedisUtils; |
| | | import com.ruoyi.common.utils.spring.SpringUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * 用户行为 侦听器的实现 |
| | |
| | | @Component |
| | | @Slf4j |
| | | public class UserActionListener implements SaTokenListener { |
| | | |
| | | @Autowired |
| | | private SaTokenConfig saTokenConfig; |
| | | |
| | | /** |
| | | * 每次登录时触发 |
| | |
| | | if (userType == UserType.SYS_USER) { |
| | | UserAgent userAgent = UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent")); |
| | | String ip = ServletUtils.getClientIP(); |
| | | SysUser user = SecurityUtils.getUser(); |
| | | SysUser user = SpringUtils.getBean(UserService.class).selectUserById(LoginUtils.getUserId()); |
| | | String tokenValue = StpUtil.getTokenValue(); |
| | | UserOnlineDTO userOnlineDTO = new UserOnlineDTO() |
| | | .setIpaddr(ip) |
| | | .setLoginLocation(AddressUtils.getRealAddressByIP(ip)) |
| | | .setBrowser(userAgent.getBrowser().getName()) |
| | | .setOs(userAgent.getOs().getName()) |
| | | .setLoginTime(System.currentTimeMillis()) |
| | | .setTokenId(tokenValue) |
| | | .setUserName(user.getUserName()); |
| | | .setIpaddr(ip) |
| | | .setLoginLocation(AddressUtils.getRealAddressByIP(ip)) |
| | | .setBrowser(userAgent.getBrowser().getName()) |
| | | .setOs(userAgent.getOs().getName()) |
| | | .setLoginTime(System.currentTimeMillis()) |
| | | .setTokenId(tokenValue) |
| | | .setUserName(user.getUserName()); |
| | | if (StringUtils.isNotNull(user.getDept())) { |
| | | userOnlineDTO.setDeptName(user.getDept().getDeptName()); |
| | | } |
| | | RedisUtils.setCacheObject(Constants.ONLINE_TOKEN_KEY + tokenValue, userOnlineDTO); |
| | | RedisUtils.setCacheObject(Constants.ONLINE_TOKEN_KEY + tokenValue, userOnlineDTO, saTokenConfig.getTimeout(), TimeUnit.SECONDS); |
| | | log.info("user doLogin, useId:{}, token:{}", loginId, tokenValue); |
| | | } else if (userType == UserType.APP_USER) { |
| | | // app端 自行根据业务编写 |