LoginService.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package com.ruoyi.framework.shiro.service;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Component;
  4. import org.springframework.util.StringUtils;
  5. import com.ruoyi.common.constant.CommonConstant;
  6. import com.ruoyi.common.constant.ShiroConstants;
  7. import com.ruoyi.common.constant.UserConstants;
  8. import com.ruoyi.common.exception.user.CaptchaException;
  9. import com.ruoyi.common.exception.user.UserBlockedException;
  10. import com.ruoyi.common.exception.user.UserNotExistsException;
  11. import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
  12. import com.ruoyi.common.utils.DateUtils;
  13. import com.ruoyi.common.utils.MessageUtils;
  14. import com.ruoyi.common.utils.ServletUtils;
  15. import com.ruoyi.common.utils.SystemLogUtils;
  16. import com.ruoyi.common.utils.security.ShiroUtils;
  17. import com.ruoyi.project.system.user.domain.User;
  18. import com.ruoyi.project.system.user.service.IUserService;
  19. /**
  20. * 登录校验方法
  21. *
  22. * @author ruoyi
  23. */
  24. @Component
  25. public class LoginService
  26. {
  27. @Autowired
  28. private PasswordService passwordService;
  29. @Autowired
  30. private IUserService userService;
  31. /**
  32. * 登录
  33. */
  34. public User login(String username, String password)
  35. {
  36. // 验证码校验
  37. if (!StringUtils.isEmpty(ServletUtils.getStrAttribute(ShiroConstants.CURRENT_CAPTCHA)))
  38. {
  39. SystemLogUtils.log(username, CommonConstant.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"));
  40. throw new CaptchaException();
  41. }
  42. // 用户名或密码为空 错误
  43. if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
  44. {
  45. SystemLogUtils.log(username, CommonConstant.LOGIN_FAIL, MessageUtils.message("not.null"));
  46. throw new UserNotExistsException();
  47. }
  48. // 密码如果不在指定范围内 错误
  49. if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
  50. || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
  51. {
  52. SystemLogUtils.log(username, CommonConstant.LOGIN_FAIL, MessageUtils.message("user.password.not.match"));
  53. throw new UserPasswordNotMatchException();
  54. }
  55. // 用户名不在指定范围内 错误
  56. if (username.length() < UserConstants.USERNAME_MIN_LENGTH
  57. || username.length() > UserConstants.USERNAME_MAX_LENGTH)
  58. {
  59. SystemLogUtils.log(username, CommonConstant.LOGIN_FAIL, MessageUtils.message("user.password.not.match"));
  60. throw new UserPasswordNotMatchException();
  61. }
  62. // 查询用户信息
  63. User user = userService.selectUserByLoginName(username);
  64. if (user == null && maybeMobilePhoneNumber(username))
  65. {
  66. user = userService.selectUserByPhoneNumber(username);
  67. }
  68. if (user == null && maybeEmail(username))
  69. {
  70. user = userService.selectUserByEmail(username);
  71. }
  72. if (user == null)
  73. {
  74. SystemLogUtils.log(username, CommonConstant.LOGIN_FAIL, MessageUtils.message("user.not.exists"));
  75. throw new UserNotExistsException();
  76. }
  77. passwordService.validate(user, password);
  78. if (UserConstants.USER_BLOCKED == user.getStatus())
  79. {
  80. SystemLogUtils.log(username, CommonConstant.LOGIN_FAIL, MessageUtils.message("user.blocked", user.getRefuseDes()));
  81. throw new UserBlockedException(user.getRefuseDes());
  82. }
  83. SystemLogUtils.log(username, CommonConstant.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
  84. recordLoginInfo(user);
  85. return user;
  86. }
  87. private boolean maybeEmail(String username)
  88. {
  89. if (!username.matches(UserConstants.EMAIL_PATTERN))
  90. {
  91. return false;
  92. }
  93. return true;
  94. }
  95. private boolean maybeMobilePhoneNumber(String username)
  96. {
  97. if (!username.matches(UserConstants.MOBILE_PHONE_NUMBER_PATTERN))
  98. {
  99. return false;
  100. }
  101. return true;
  102. }
  103. /**
  104. * 记录登录信息
  105. */
  106. public void recordLoginInfo(User user)
  107. {
  108. user.setLoginIp(ShiroUtils.getIp());
  109. user.setLoginDate(DateUtils.getNowDate());
  110. userService.updateUser(user);
  111. }
  112. }