SysLoginService.java 4.1 KB

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