SysLoginService.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.ruoyi.system.service;
  2. import cn.dev33.satoken.stp.StpUtil;
  3. import com.ruoyi.common.constant.Constants;
  4. import com.ruoyi.common.core.domain.entity.SysUser;
  5. import com.ruoyi.common.core.service.LogininforService;
  6. import com.ruoyi.common.enums.DeviceType;
  7. import com.ruoyi.common.enums.UserStatus;
  8. import com.ruoyi.common.enums.UserType;
  9. import com.ruoyi.common.exception.ServiceException;
  10. import com.ruoyi.common.exception.user.CaptchaException;
  11. import com.ruoyi.common.exception.user.CaptchaExpireException;
  12. import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
  13. import com.ruoyi.common.utils.*;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  17. import org.springframework.stereotype.Service;
  18. import javax.servlet.http.HttpServletRequest;
  19. /**
  20. * 登录校验方法
  21. *
  22. * @author ruoyi
  23. */
  24. @Slf4j
  25. @Service
  26. public class SysLoginService {
  27. @Autowired
  28. private ISysUserService userService;
  29. @Autowired
  30. private ISysConfigService configService;
  31. @Autowired
  32. private LogininforService asyncService;
  33. /**
  34. * 登录验证
  35. *
  36. * @param username 用户名
  37. * @param password 密码
  38. * @param code 验证码
  39. * @param uuid 唯一标识
  40. * @return 结果
  41. */
  42. public String login(String username, String password, String code, String uuid) {
  43. HttpServletRequest request = ServletUtils.getRequest();
  44. boolean captchaOnOff = configService.selectCaptchaOnOff();
  45. // 验证码开关
  46. if (captchaOnOff) {
  47. validateCaptcha(username, code, uuid, request);
  48. }
  49. SysUser user = userService.selectUserByUserName(username);
  50. if (StringUtils.isNull(user)) {
  51. log.info("登录用户:{} 不存在.", username);
  52. throw new ServiceException("登录用户:" + username + " 不存在");
  53. } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
  54. log.info("登录用户:{} 已被删除.", username);
  55. throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
  56. } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
  57. log.info("登录用户:{} 已被停用.", username);
  58. throw new ServiceException("对不起,您的账号:" + username + " 已停用");
  59. }
  60. if (!SecurityUtils.matchesPassword(password, user.getPassword())) {
  61. asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match"), request);
  62. throw new UserPasswordNotMatchException();
  63. }
  64. asyncService.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"), request);
  65. recordLoginInfo(user.getUserId());
  66. // 生成token
  67. LoginUtils.loginByDevice(user.getUserId(), UserType.SYS_USER, DeviceType.PC);
  68. return StpUtil.getTokenValue();
  69. }
  70. /**
  71. * 校验验证码
  72. *
  73. * @param username 用户名
  74. * @param code 验证码
  75. * @param uuid 唯一标识
  76. * @return 结果
  77. */
  78. public void validateCaptcha(String username, String code, String uuid, HttpServletRequest request) {
  79. String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
  80. String captcha = RedisUtils.getCacheObject(verifyKey);
  81. RedisUtils.deleteObject(verifyKey);
  82. if (captcha == null) {
  83. asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"), request);
  84. throw new CaptchaExpireException();
  85. }
  86. if (!code.equalsIgnoreCase(captcha)) {
  87. asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"), request);
  88. throw new CaptchaException();
  89. }
  90. }
  91. /**
  92. * 记录登录信息
  93. *
  94. * @param userId 用户ID
  95. */
  96. public void recordLoginInfo(Long userId) {
  97. SysUser sysUser = new SysUser();
  98. sysUser.setUserId(userId);
  99. sysUser.setLoginIp(ServletUtils.getClientIP());
  100. sysUser.setLoginDate(DateUtils.getNowDate());
  101. userService.updateUserProfile(sysUser);
  102. }
  103. }