SysLoginService.java 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package com.ruoyi.auth.service;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Component;
  4. import com.ruoyi.common.core.constant.Constants;
  5. import com.ruoyi.common.core.constant.UserConstants;
  6. import com.ruoyi.common.core.domain.R;
  7. import com.ruoyi.common.core.enums.UserStatus;
  8. import com.ruoyi.common.core.exception.BaseException;
  9. import com.ruoyi.common.core.utils.SecurityUtils;
  10. import com.ruoyi.common.core.utils.StringUtils;
  11. import com.ruoyi.system.api.RemoteLogService;
  12. import com.ruoyi.system.api.RemoteUserService;
  13. import com.ruoyi.system.api.domain.SysUser;
  14. import com.ruoyi.system.api.model.LoginUser;
  15. /**
  16. * 登录校验方法
  17. *
  18. * @author ruoyi
  19. */
  20. @Component
  21. public class SysLoginService
  22. {
  23. @Autowired
  24. private RemoteLogService remoteLogService;
  25. @Autowired
  26. private RemoteUserService remoteUserService;
  27. /**
  28. * 登录
  29. */
  30. public LoginUser login(String username, String password)
  31. {
  32. // 用户名或密码为空 错误
  33. if (StringUtils.isAnyBlank(username, password))
  34. {
  35. remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
  36. throw new BaseException("用户/密码必须填写");
  37. }
  38. // 密码如果不在指定范围内 错误
  39. if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
  40. || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
  41. {
  42. remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户密码不在指定范围");
  43. throw new BaseException("用户密码不在指定范围");
  44. }
  45. // 用户名不在指定范围内 错误
  46. if (username.length() < UserConstants.USERNAME_MIN_LENGTH
  47. || username.length() > UserConstants.USERNAME_MAX_LENGTH)
  48. {
  49. remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
  50. throw new BaseException("用户名不在指定范围");
  51. }
  52. // 查询用户信息
  53. R<LoginUser> userResult = remoteUserService.getUserInfo(username);
  54. if (R.FAIL == userResult.getCode())
  55. {
  56. throw new BaseException(userResult.getMsg());
  57. }
  58. if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
  59. {
  60. remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
  61. throw new BaseException("登录用户:" + username + " 不存在");
  62. }
  63. LoginUser userInfo = userResult.getData();
  64. SysUser user = userResult.getData().getSysUser();
  65. if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
  66. {
  67. remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
  68. throw new BaseException("对不起,您的账号:" + username + " 已被删除");
  69. }
  70. if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
  71. {
  72. remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
  73. throw new BaseException("对不起,您的账号:" + username + " 已停用");
  74. }
  75. if (!SecurityUtils.matchesPassword(password, user.getPassword()))
  76. {
  77. remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户密码错误");
  78. throw new BaseException("用户不存在/密码错误");
  79. }
  80. remoteLogService.saveLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
  81. return userInfo;
  82. }
  83. public void logout(String loginName)
  84. {
  85. remoteLogService.saveLogininfor(loginName, Constants.LOGOUT, "退出成功");
  86. }
  87. }