SysLoginService.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package com.ruoyi.system.service;
  2. import cn.dev33.satoken.stp.StpUtil;
  3. import cn.hutool.core.util.ObjectUtil;
  4. import com.ruoyi.common.constant.Constants;
  5. import com.ruoyi.common.core.domain.entity.SysUser;
  6. import com.ruoyi.common.core.domain.model.LoginUser;
  7. import com.ruoyi.common.core.service.LogininforService;
  8. import com.ruoyi.common.enums.DeviceType;
  9. import com.ruoyi.common.enums.UserStatus;
  10. import com.ruoyi.common.enums.UserType;
  11. import com.ruoyi.common.exception.ServiceException;
  12. import com.ruoyi.common.exception.user.CaptchaException;
  13. import com.ruoyi.common.exception.user.CaptchaExpireException;
  14. import com.ruoyi.common.exception.user.UserException;
  15. import com.ruoyi.common.utils.*;
  16. import lombok.extern.slf4j.Slf4j;
  17. import lombok.RequiredArgsConstructor;
  18. import org.springframework.stereotype.Service;
  19. import javax.servlet.http.HttpServletRequest;
  20. import java.util.concurrent.TimeUnit;
  21. /**
  22. * 登录校验方法
  23. *
  24. * @author Lion Li
  25. */
  26. @RequiredArgsConstructor
  27. @Slf4j
  28. @Service
  29. public class SysLoginService {
  30. private final ISysUserService userService;
  31. private final ISysConfigService configService;
  32. private final LogininforService asyncService;
  33. private final SysPermissionService permissionService;
  34. /**
  35. * 登录验证
  36. *
  37. * @param username 用户名
  38. * @param password 密码
  39. * @param code 验证码
  40. * @param uuid 唯一标识
  41. * @return 结果
  42. */
  43. public String login(String username, String password, String code, String uuid) {
  44. HttpServletRequest request = ServletUtils.getRequest();
  45. boolean captchaOnOff = configService.selectCaptchaOnOff();
  46. // 验证码开关
  47. if (captchaOnOff) {
  48. validateCaptcha(username, code, uuid, request);
  49. }
  50. // 获取用户登录错误次数(可自定义限制策略 例如: key + username + ip)
  51. Integer errorNumber = RedisUtils.getCacheObject(Constants.LOGIN_ERROR + username);
  52. // 锁定时间内登录 则踢出
  53. if (ObjectUtil.isNotNull(errorNumber) && errorNumber.equals(Constants.LOGIN_ERROR_NUMBER)) {
  54. asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME), request);
  55. throw new UserException("user.password.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME);
  56. }
  57. SysUser user = userService.selectUserByUserName(username);
  58. if (StringUtils.isNull(user)) {
  59. log.info("登录用户:{} 不存在.", username);
  60. throw new UserException("user.not.exists", username);
  61. } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
  62. log.info("登录用户:{} 已被删除.", username);
  63. throw new UserException("user.password.delete", username);
  64. } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
  65. log.info("登录用户:{} 已被停用.", username);
  66. throw new UserException("user.blocked", username);
  67. }
  68. if (!SecurityUtils.matchesPassword(password, user.getPassword())) {
  69. // 是否第一次
  70. errorNumber = ObjectUtil.isNull(errorNumber) ? 1 : errorNumber + 1;
  71. // 达到规定错误次数 则锁定登录
  72. if (errorNumber.equals(Constants.LOGIN_ERROR_NUMBER)) {
  73. RedisUtils.setCacheObject(Constants.LOGIN_ERROR + username, errorNumber, Constants.LOGIN_ERROR_LIMIT_TIME, TimeUnit.MINUTES);
  74. asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME), request);
  75. throw new UserException("user.password.retry.limit.exceed", Constants.LOGIN_ERROR_LIMIT_TIME);
  76. } else {
  77. // 未达到规定错误次数 则递增
  78. RedisUtils.setCacheObject(Constants.LOGIN_ERROR + username, errorNumber);
  79. asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.retry.limit.count", errorNumber), request);
  80. throw new UserException("user.password.retry.limit.count", errorNumber);
  81. }
  82. }
  83. // 登录成功 清空错误次数
  84. RedisUtils.deleteObject(Constants.LOGIN_ERROR + username);
  85. asyncService.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"), request);
  86. recordLoginInfo(user.getUserId(), username);
  87. LoginUser loginUser = new LoginUser();
  88. loginUser.setUserId(user.getUserId());
  89. loginUser.setDeptId(user.getDeptId());
  90. loginUser.setUsername(user.getUserName());
  91. loginUser.setMenuPermission(permissionService.getMenuPermission(user));
  92. loginUser.setRolePermission(permissionService.getRolePermission(user));
  93. // 生成token
  94. LoginUtils.loginByDevice(loginUser, UserType.SYS_USER, DeviceType.PC);
  95. return StpUtil.getTokenValue();
  96. }
  97. /**
  98. * 校验验证码
  99. *
  100. * @param username 用户名
  101. * @param code 验证码
  102. * @param uuid 唯一标识
  103. * @return 结果
  104. */
  105. public void validateCaptcha(String username, String code, String uuid, HttpServletRequest request) {
  106. String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
  107. String captcha = RedisUtils.getCacheObject(verifyKey);
  108. RedisUtils.deleteObject(verifyKey);
  109. if (captcha == null) {
  110. asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"), request);
  111. throw new CaptchaExpireException();
  112. }
  113. if (!code.equalsIgnoreCase(captcha)) {
  114. asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"), request);
  115. throw new CaptchaException();
  116. }
  117. }
  118. /**
  119. * 记录登录信息
  120. *
  121. * @param userId 用户ID
  122. */
  123. public void recordLoginInfo(Long userId, String username) {
  124. SysUser sysUser = new SysUser();
  125. sysUser.setUserId(userId);
  126. sysUser.setLoginIp(ServletUtils.getClientIP());
  127. sysUser.setLoginDate(DateUtils.getNowDate());
  128. sysUser.setUpdateBy(username);
  129. userService.updateUserProfile(sysUser);
  130. }
  131. }