SysLoginService.java 5.5 KB

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