SysLoginService.java 5.6 KB

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