SysLoginService.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.SecurityConstants;
  6. import com.ruoyi.common.core.constant.UserConstants;
  7. import com.ruoyi.common.core.domain.R;
  8. import com.ruoyi.common.core.enums.UserStatus;
  9. import com.ruoyi.common.core.exception.ServiceException;
  10. import com.ruoyi.common.core.utils.StringUtils;
  11. import com.ruoyi.common.security.utils.SecurityUtils;
  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 RemoteUserService remoteUserService;
  25. @Autowired
  26. private SysPasswordService passwordService;
  27. @Autowired
  28. private SysRecordLogService recordLogService;
  29. /**
  30. * 登录
  31. */
  32. public LoginUser login(String username, String password)
  33. {
  34. // 用户名或密码为空 错误
  35. if (StringUtils.isAnyBlank(username, password))
  36. {
  37. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
  38. throw new ServiceException("用户/密码必须填写");
  39. }
  40. // 密码如果不在指定范围内 错误
  41. if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
  42. || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
  43. {
  44. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户密码不在指定范围");
  45. throw new ServiceException("用户密码不在指定范围");
  46. }
  47. // 用户名不在指定范围内 错误
  48. if (username.length() < UserConstants.USERNAME_MIN_LENGTH
  49. || username.length() > UserConstants.USERNAME_MAX_LENGTH)
  50. {
  51. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
  52. throw new ServiceException("用户名不在指定范围");
  53. }
  54. // 查询用户信息
  55. R<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
  56. if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
  57. {
  58. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
  59. throw new ServiceException("登录用户:" + username + " 不存在");
  60. }
  61. if (R.FAIL == userResult.getCode())
  62. {
  63. throw new ServiceException(userResult.getMsg());
  64. }
  65. LoginUser userInfo = userResult.getData();
  66. SysUser user = userResult.getData().getSysUser();
  67. if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
  68. {
  69. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
  70. throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
  71. }
  72. if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
  73. {
  74. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
  75. throw new ServiceException("对不起,您的账号:" + username + " 已停用");
  76. }
  77. passwordService.validate(user, password);
  78. recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
  79. return userInfo;
  80. }
  81. public void logout(String loginName)
  82. {
  83. recordLogService.recordLogininfor(loginName, Constants.LOGOUT, "退出成功");
  84. }
  85. /**
  86. * 注册
  87. */
  88. public void register(String username, String password)
  89. {
  90. // 用户名或密码为空 错误
  91. if (StringUtils.isAnyBlank(username, password))
  92. {
  93. throw new ServiceException("用户/密码必须填写");
  94. }
  95. if (username.length() < UserConstants.USERNAME_MIN_LENGTH
  96. || username.length() > UserConstants.USERNAME_MAX_LENGTH)
  97. {
  98. throw new ServiceException("账户长度必须在2到20个字符之间");
  99. }
  100. if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
  101. || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
  102. {
  103. throw new ServiceException("密码长度必须在5到20个字符之间");
  104. }
  105. // 注册用户信息
  106. SysUser sysUser = new SysUser();
  107. sysUser.setUserName(username);
  108. sysUser.setNickName(username);
  109. sysUser.setPassword(SecurityUtils.encryptPassword(password));
  110. R<?> registerResult = remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER);
  111. if (R.FAIL == registerResult.getCode())
  112. {
  113. throw new ServiceException(registerResult.getMsg());
  114. }
  115. recordLogService.recordLogininfor(username, Constants.REGISTER, "注册成功");
  116. }
  117. }