From 2463035346382266e67d832f63fc523e5447ae9b Mon Sep 17 00:00:00 2001 From: du <1725534722@qq.com> Date: Fri, 8 Nov 2024 11:24:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AD=A3=E5=BC=8F=E7=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/service/SysLoginService.java | 72 ++++++------------- 1 file changed, 22 insertions(+), 50 deletions(-) diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java index d11523f..29c96c3 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java @@ -9,9 +9,7 @@ import com.ruoyi.common.core.domain.model.LoginUser; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.enums.UserStatus; import com.ruoyi.common.exception.ServiceException; -import com.ruoyi.common.exception.user.CaptchaException; -import com.ruoyi.common.exception.user.CaptchaExpireException; -import com.ruoyi.common.exception.user.UserPasswordNotMatchException; +import com.ruoyi.common.exception.user.*; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.MessageUtils; import com.ruoyi.common.utils.RsaUtils; @@ -84,14 +82,9 @@ public class SysLoginService { throw new RuntimeException(e); } // 验证码校验 - validateCaptcha(username, code, uuid); + validateCaptcha(username, code, uuid); // 登录前置校验 loginPreCheck(username, strP); - Map map = redisCache.getCacheObject(username + strP); - if (CollectionUtil.isNotEmpty(map)) { - map.remove("@type"); - return map; - } // 用户验证 Authentication authentication; try { @@ -110,32 +103,9 @@ public class SysLoginService { } finally { AuthenticationContextHolder.clearContext(); } -// AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"))); + AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"))); LoginUser loginUser = (LoginUser) authentication.getPrincipal(); - // recordLoginInfo(loginUser.getUserId()); - if (StringUtils.isNull(loginUser) || StringUtils.isNull(loginUser.getUserId())) { -// addRecord(username, Constants.LOGIN_FAIL, "登录用户不存在"); - throw new ServiceException("登录用户:" + username + " 不存在"); - } - loginUser.getUser().setPassword(password); - SysUser user = loginUser.getUser(); - // 判断用户类型 - if (!"admin".equals(username)) { - if (!userType.equals(user.getUserType())) { - throw new ServiceException("该用户类型错误"); - } - } - - if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) { -// addRecord(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除"); - throw new ServiceException("对不起,您的账号:" + username + " 已被删除"); - } - if (UserStatus.DISABLE.getCode().equals(user.getStatus())) { -// addRecord(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员"); - throw new ServiceException("对不起,您的账号:" + username + " 已停用"); - } -// sysPasswordService.validate(user); - + recordLoginInfo(loginUser.getUserId()); // 生成token return tokenService.createToken(loginUser); } @@ -173,30 +143,32 @@ public class SysLoginService { */ public void loginPreCheck(String username, String password) { // 用户名或密码为空 错误 - if (StringUtils.isAnyBlank(username, password)) { -// this.addRecord(username, Constants.LOGIN_FAIL, "用户/密码必须填写"); - throw new ServiceException("用户/密码必须填写"); + if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) + { + AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("not.null"))); + throw new UserNotExistsException(); } // 密码如果不在指定范围内 错误 if (password.length() < UserConstants.PASSWORD_MIN_LENGTH - || password.length() > UserConstants.PASSWORD_MAX_LENGTH) { -// this.addRecord(username, Constants.LOGIN_FAIL, "用户密码不在指定范围"); - throw new ServiceException("用户密码不在指定范围"); + || password.length() > UserConstants.PASSWORD_MAX_LENGTH) + { + AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match"))); + throw new UserPasswordNotMatchException(); } // 用户名不在指定范围内 错误 if (username.length() < UserConstants.USERNAME_MIN_LENGTH - || username.length() > UserConstants.USERNAME_MAX_LENGTH) { -// this.addRecord(username, Constants.LOGIN_FAIL, "用户名不在指定范围"); - throw new ServiceException("用户名不在指定范围"); + || username.length() > UserConstants.USERNAME_MAX_LENGTH) + { + AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match"))); + throw new UserPasswordNotMatchException(); } - // IP黑名单校验 -// String blackStr = configService.selectConfigByKey("sys.login.blackIPList"); -// if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr())) -// { -// AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("login.blocked"))); -// throw new BlackListException(); -// } + String blackStr = configService.selectConfigByKey("sys.login.blackIPList"); + if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr())) + { + AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("login.blocked"))); + throw new BlackListException(); + } } /**