SysLoginService.java 5.9 KB

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