From 2d507c6afffdd616a8fa99f645d52e040b89e074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Fri, 17 Nov 2023 14:45:59 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20=E5=B0=86?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E8=AE=B0=E5=BD=95=E6=8A=BD=E5=8F=96=E5=88=B0?= =?UTF-8?q?=E7=9B=91=E5=90=AC=E5=99=A8=E7=BB=9F=E4=B8=80=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/listener/UserActionListener.java | 85 +++++++++++-------- .../auth/service/impl/EmailAuthStrategy.java | 4 - .../service/impl/PasswordAuthStrategy.java | 4 - .../auth/service/impl/SmsAuthStrategy.java | 4 - .../auth/service/impl/SocialAuthStrategy.java | 6 -- .../auth/service/impl/XcxAuthStrategy.java | 6 -- 6 files changed, 51 insertions(+), 58 deletions(-) diff --git a/ruoyi-auth/src/main/java/org/dromara/auth/listener/UserActionListener.java b/ruoyi-auth/src/main/java/org/dromara/auth/listener/UserActionListener.java index c13431c4..86df42c7 100644 --- a/ruoyi-auth/src/main/java/org/dromara/auth/listener/UserActionListener.java +++ b/ruoyi-auth/src/main/java/org/dromara/auth/listener/UserActionListener.java @@ -6,19 +6,26 @@ import cn.dev33.satoken.stp.SaLoginModel; import cn.hutool.core.util.ObjectUtil; import cn.hutool.http.useragent.UserAgent; import cn.hutool.http.useragent.UserAgentUtil; -import org.dromara.common.core.constant.CacheConstants; -import org.dromara.common.core.enums.UserType; -import org.dromara.common.core.utils.ServletUtils; -import org.dromara.common.core.utils.ip.AddressUtils; -import org.dromara.common.redis.utils.RedisUtils; -import org.dromara.common.satoken.utils.LoginHelper; -import org.dromara.system.api.domain.SysUserOnline; -import org.dromara.system.api.model.LoginUser; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.dubbo.config.annotation.DubboReference; +import org.dromara.common.core.constant.CacheConstants; +import org.dromara.common.core.constant.Constants; +import org.dromara.common.core.utils.MessageUtils; +import org.dromara.common.core.utils.ServletUtils; +import org.dromara.common.core.utils.SpringUtils; +import org.dromara.common.core.utils.ip.AddressUtils; +import org.dromara.common.log.event.LogininforEvent; +import org.dromara.common.redis.utils.RedisUtils; +import org.dromara.common.satoken.utils.LoginHelper; +import org.dromara.resource.api.RemoteMessageService; +import org.dromara.system.api.RemoteUserService; +import org.dromara.system.api.domain.SysUserOnline; +import org.dromara.system.api.model.LoginUser; import org.springframework.stereotype.Component; import java.time.Duration; +import java.util.concurrent.ScheduledExecutorService; /** * 用户行为 侦听器的实现 @@ -31,39 +38,49 @@ import java.time.Duration; public class UserActionListener implements SaTokenListener { private final SaTokenConfig tokenConfig; + private final ScheduledExecutorService scheduledExecutorService; + @DubboReference + private RemoteUserService remoteUserService; + @DubboReference + private RemoteMessageService remoteMessageService; /** * 每次登录时触发 */ @Override public void doLogin(String loginType, Object loginId, String tokenValue, SaLoginModel loginModel) { - UserType userType = UserType.getUserType(loginId.toString()); - if (userType == UserType.SYS_USER) { - UserAgent userAgent = UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent")); - String ip = ServletUtils.getClientIP(); - LoginUser user = LoginHelper.getLoginUser(); - SysUserOnline userOnline = new SysUserOnline(); - userOnline.setIpaddr(ip); - userOnline.setLoginLocation(AddressUtils.getRealAddressByIP(ip)); - userOnline.setBrowser(userAgent.getBrowser().getName()); - userOnline.setOs(userAgent.getOs().getName()); - userOnline.setLoginTime(System.currentTimeMillis()); - userOnline.setTokenId(tokenValue); - userOnline.setUserName(user.getUsername()); - userOnline.setClientKey(user.getClientKey()); - userOnline.setDeviceType(user.getDeviceType()); - if (ObjectUtil.isNotNull(user.getDeptName())) { - userOnline.setDeptName(user.getDeptName()); - } - if(tokenConfig.getTimeout() == -1) { - RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, userOnline); - } else { - RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, userOnline, Duration.ofSeconds(tokenConfig.getTimeout())); - } - log.info("user doLogin, useId:{}, token:{}", loginId, tokenValue); - } else if (userType == UserType.APP_USER) { - // app端 自行根据业务编写 + UserAgent userAgent = UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent")); + String ip = ServletUtils.getClientIP(); + LoginUser user = LoginHelper.getLoginUser(); + SysUserOnline userOnline = new SysUserOnline(); + userOnline.setIpaddr(ip); + userOnline.setLoginLocation(AddressUtils.getRealAddressByIP(ip)); + userOnline.setBrowser(userAgent.getBrowser().getName()); + userOnline.setOs(userAgent.getOs().getName()); + userOnline.setLoginTime(System.currentTimeMillis()); + userOnline.setTokenId(tokenValue); + userOnline.setUserName(user.getUsername()); + userOnline.setClientKey(user.getClientKey()); + userOnline.setDeviceType(user.getDeviceType()); + if (ObjectUtil.isNotNull(user.getDeptName())) { + userOnline.setDeptName(user.getDeptName()); } + if (tokenConfig.getTimeout() == -1) { + RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, userOnline); + } else { + RedisUtils.setCacheObject(CacheConstants.ONLINE_TOKEN_KEY + tokenValue, userOnline, Duration.ofSeconds(tokenConfig.getTimeout())); + } + // 记录登录日志 + LogininforEvent logininforEvent = new LogininforEvent(); + logininforEvent.setTenantId(user.getTenantId()); + logininforEvent.setUsername(user.getUsername()); + logininforEvent.setStatus(Constants.LOGIN_SUCCESS); + logininforEvent.setMessage(MessageUtils.message("user.login.success")); + logininforEvent.setRequest(ServletUtils.getRequest()); + SpringUtils.context().publishEvent(logininforEvent); + // 更新登录信息 + remoteUserService.recordLoginInfo(user.getUserId(), ServletUtils.getClientIP()); + log.info("user doLogin, useId:{}, token:{}", loginId, tokenValue); } /** diff --git a/ruoyi-auth/src/main/java/org/dromara/auth/service/impl/EmailAuthStrategy.java b/ruoyi-auth/src/main/java/org/dromara/auth/service/impl/EmailAuthStrategy.java index c4b66078..8790fe13 100644 --- a/ruoyi-auth/src/main/java/org/dromara/auth/service/impl/EmailAuthStrategy.java +++ b/ruoyi-auth/src/main/java/org/dromara/auth/service/impl/EmailAuthStrategy.java @@ -14,7 +14,6 @@ import org.dromara.common.core.constant.GlobalConstants; import org.dromara.common.core.enums.LoginType; import org.dromara.common.core.exception.user.CaptchaExpireException; import org.dromara.common.core.utils.MessageUtils; -import org.dromara.common.core.utils.ServletUtils; import org.dromara.common.core.utils.StringUtils; import org.dromara.common.core.utils.ValidatorUtils; import org.dromara.common.json.utils.JsonUtils; @@ -63,9 +62,6 @@ public class EmailAuthStrategy implements IAuthStrategy { // 生成token LoginHelper.login(loginUser, model); - loginService.recordLogininfor(loginUser.getTenantId(), loginUser.getUsername(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")); - remoteUserService.recordLoginInfo(loginUser.getUserId(), ServletUtils.getClientIP()); - LoginVo loginVo = new LoginVo(); loginVo.setAccessToken(StpUtil.getTokenValue()); loginVo.setExpireIn(StpUtil.getTokenTimeout()); diff --git a/ruoyi-auth/src/main/java/org/dromara/auth/service/impl/PasswordAuthStrategy.java b/ruoyi-auth/src/main/java/org/dromara/auth/service/impl/PasswordAuthStrategy.java index e5720a1b..5a5be629 100644 --- a/ruoyi-auth/src/main/java/org/dromara/auth/service/impl/PasswordAuthStrategy.java +++ b/ruoyi-auth/src/main/java/org/dromara/auth/service/impl/PasswordAuthStrategy.java @@ -17,7 +17,6 @@ import org.dromara.common.core.enums.LoginType; import org.dromara.common.core.exception.CaptchaException; import org.dromara.common.core.exception.user.CaptchaExpireException; import org.dromara.common.core.utils.MessageUtils; -import org.dromara.common.core.utils.ServletUtils; import org.dromara.common.core.utils.StringUtils; import org.dromara.common.core.utils.ValidatorUtils; import org.dromara.common.json.utils.JsonUtils; @@ -74,9 +73,6 @@ public class PasswordAuthStrategy implements IAuthStrategy { // 生成token LoginHelper.login(loginUser, model); - loginService.recordLogininfor(loginUser.getTenantId(), username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")); - remoteUserService.recordLoginInfo(loginUser.getUserId(), ServletUtils.getClientIP()); - LoginVo loginVo = new LoginVo(); loginVo.setAccessToken(StpUtil.getTokenValue()); loginVo.setExpireIn(StpUtil.getTokenTimeout()); diff --git a/ruoyi-auth/src/main/java/org/dromara/auth/service/impl/SmsAuthStrategy.java b/ruoyi-auth/src/main/java/org/dromara/auth/service/impl/SmsAuthStrategy.java index d2114ecd..d32b5aa1 100644 --- a/ruoyi-auth/src/main/java/org/dromara/auth/service/impl/SmsAuthStrategy.java +++ b/ruoyi-auth/src/main/java/org/dromara/auth/service/impl/SmsAuthStrategy.java @@ -14,7 +14,6 @@ import org.dromara.common.core.constant.GlobalConstants; import org.dromara.common.core.enums.LoginType; import org.dromara.common.core.exception.user.CaptchaExpireException; import org.dromara.common.core.utils.MessageUtils; -import org.dromara.common.core.utils.ServletUtils; import org.dromara.common.core.utils.StringUtils; import org.dromara.common.core.utils.ValidatorUtils; import org.dromara.common.json.utils.JsonUtils; @@ -63,9 +62,6 @@ public class SmsAuthStrategy implements IAuthStrategy { // 生成token LoginHelper.login(loginUser, model); - loginService.recordLogininfor(loginUser.getTenantId(), loginUser.getUsername(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")); - remoteUserService.recordLoginInfo(loginUser.getUserId(), ServletUtils.getClientIP()); - LoginVo loginVo = new LoginVo(); loginVo.setAccessToken(StpUtil.getTokenValue()); loginVo.setExpireIn(StpUtil.getTokenTimeout()); diff --git a/ruoyi-auth/src/main/java/org/dromara/auth/service/impl/SocialAuthStrategy.java b/ruoyi-auth/src/main/java/org/dromara/auth/service/impl/SocialAuthStrategy.java index d86392cc..f0173d4d 100644 --- a/ruoyi-auth/src/main/java/org/dromara/auth/service/impl/SocialAuthStrategy.java +++ b/ruoyi-auth/src/main/java/org/dromara/auth/service/impl/SocialAuthStrategy.java @@ -16,10 +16,7 @@ import org.dromara.auth.domain.vo.LoginVo; import org.dromara.auth.form.SocialLoginBody; import org.dromara.auth.service.IAuthStrategy; import org.dromara.auth.service.SysLoginService; -import org.dromara.common.core.constant.Constants; import org.dromara.common.core.exception.ServiceException; -import org.dromara.common.core.utils.MessageUtils; -import org.dromara.common.core.utils.ServletUtils; import org.dromara.common.core.utils.ValidatorUtils; import org.dromara.common.json.utils.JsonUtils; import org.dromara.common.satoken.utils.LoginHelper; @@ -102,9 +99,6 @@ public class SocialAuthStrategy implements IAuthStrategy { // 生成token LoginHelper.login(loginUser, model); - loginService.recordLogininfor(loginUser.getTenantId(), socialVo.getUserName(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")); - remoteUserService.recordLoginInfo(loginUser.getUserId(), ServletUtils.getClientIP()); - LoginVo loginVo = new LoginVo(); loginVo.setAccessToken(StpUtil.getTokenValue()); loginVo.setExpireIn(StpUtil.getTokenTimeout()); diff --git a/ruoyi-auth/src/main/java/org/dromara/auth/service/impl/XcxAuthStrategy.java b/ruoyi-auth/src/main/java/org/dromara/auth/service/impl/XcxAuthStrategy.java index 1d68c6d4..20ec4cc3 100644 --- a/ruoyi-auth/src/main/java/org/dromara/auth/service/impl/XcxAuthStrategy.java +++ b/ruoyi-auth/src/main/java/org/dromara/auth/service/impl/XcxAuthStrategy.java @@ -9,9 +9,6 @@ import org.dromara.auth.domain.vo.LoginVo; import org.dromara.auth.form.XcxLoginBody; import org.dromara.auth.service.IAuthStrategy; import org.dromara.auth.service.SysLoginService; -import org.dromara.common.core.constant.Constants; -import org.dromara.common.core.utils.MessageUtils; -import org.dromara.common.core.utils.ServletUtils; import org.dromara.common.core.utils.ValidatorUtils; import org.dromara.common.json.utils.JsonUtils; import org.dromara.common.satoken.utils.LoginHelper; @@ -61,9 +58,6 @@ public class XcxAuthStrategy implements IAuthStrategy { // 生成token LoginHelper.login(loginUser, model); - loginService.recordLogininfor(loginUser.getTenantId(), loginUser.getUsername(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")); - remoteUserService.recordLoginInfo(loginUser.getUserId(), ServletUtils.getClientIP()); - LoginVo loginVo = new LoginVo(); loginVo.setAccessToken(StpUtil.getTokenValue()); loginVo.setExpireIn(StpUtil.getTokenTimeout());