LoginService.java 4.0 KB

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