SysLoginService.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package com.xunmei.auth.service;
  2. import cn.hutool.core.date.DateUtil;
  3. import cn.hutool.core.util.NumberUtil;
  4. import cn.hutool.core.util.ObjectUtil;
  5. import com.xunmei.common.core.constant.CacheConstants;
  6. import com.xunmei.common.core.constant.Constants;
  7. import com.xunmei.common.core.constant.SecurityConstants;
  8. import com.xunmei.common.core.constant.UserConstants;
  9. import com.xunmei.common.core.domain.R;
  10. import com.xunmei.common.core.enums.UserStatus;
  11. import com.xunmei.common.core.exception.ServiceException;
  12. import com.xunmei.common.core.text.Convert;
  13. import com.xunmei.common.core.utils.StringUtils;
  14. import com.xunmei.common.core.utils.ip.IpUtils;
  15. import com.xunmei.common.redis.utils.RedisUtils;
  16. import com.xunmei.common.security.utils.AsymmetricEncryptionUtil;
  17. import com.xunmei.common.security.utils.SecurityUtils;
  18. import com.xunmei.system.api.RemoteUserService;
  19. import com.xunmei.system.api.domain.SysUser;
  20. import com.xunmei.system.api.model.LoginUser;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.data.redis.core.BoundSetOperations;
  23. import org.springframework.stereotype.Component;
  24. import java.util.Date;
  25. /**
  26. * 登录校验方法
  27. *
  28. * @author xunmei
  29. */
  30. @Component
  31. public class SysLoginService
  32. {
  33. @Autowired
  34. private RemoteUserService remoteUserService;
  35. @Autowired
  36. private SysPasswordService passwordService;
  37. @Autowired
  38. private SysRecordLogService recordLogService;
  39. /**
  40. * 登录
  41. */
  42. public LoginUser login(String username, String password,String platformType)
  43. {
  44. // 用户名或密码为空 错误
  45. if (StringUtils.isAnyBlank(username, password))
  46. {
  47. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写",platformType);
  48. throw new ServiceException("用户/密码必须填写");
  49. }
  50. // 密码如果不在指定范围内 错误
  51. if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
  52. || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
  53. {
  54. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户密码不在指定范围",platformType);
  55. throw new ServiceException("用户密码不在指定范围");
  56. }
  57. // 用户名不在指定范围内 错误
  58. if (username.length() < UserConstants.USERNAME_MIN_LENGTH
  59. || username.length() > UserConstants.USERNAME_MAX_LENGTH)
  60. {
  61. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围",platformType);
  62. throw new ServiceException("用户名不在指定范围");
  63. }
  64. // IP黑名单校验
  65. String blackStr = Convert.toStr(RedisUtils.getCacheObject(CacheConstants.SYS_LOGIN_BLACKIPLIST));
  66. if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr()))
  67. {
  68. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "很遗憾,访问IP已被列入系统黑名单",platformType);
  69. throw new ServiceException("很遗憾,访问IP已被列入系统黑名单");
  70. }
  71. // 查询用户信息
  72. R<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
  73. if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
  74. {
  75. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在",platformType);
  76. throw new ServiceException("登录用户:" + username + " 不存在");
  77. }
  78. if (R.FAIL == userResult.getCode())
  79. {
  80. throw new ServiceException(userResult.getMsg());
  81. }
  82. LoginUser userInfo = userResult.getData();
  83. userInfo.setPlatformType(platformType);
  84. SysUser user = userResult.getData().getSysUser();
  85. if (UserStatus.DELETED.getCode().equals(user.getDeleted()))
  86. {
  87. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除",platformType);
  88. throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
  89. }
  90. if (UserStatus.DISABLE.getCode().equals(Integer.parseInt(user.getIsLock())))
  91. {
  92. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员",platformType);
  93. throw new ServiceException("对不起,您的账号:" + username + " 已停用");
  94. }
  95. passwordService.validate(user, password,platformType);
  96. recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功",platformType);
  97. return userInfo;
  98. }
  99. public void logout(String loginName,String platformType)
  100. {
  101. recordLogService.recordLogininfor(loginName, Constants.LOGOUT, "退出成功",platformType);
  102. }
  103. public void checkLogin(String authCode) {
  104. if (SecurityUtils.isApp()) {
  105. return;
  106. }
  107. String decrypt = AsymmetricEncryptionUtil.decrypt(authCode);
  108. if (ObjectUtil.isEmpty(decrypt) || null == decrypt) {
  109. throw new RuntimeException("登录信息失效");
  110. }
  111. //如果redis中存在此key,说明已经登录过了
  112. /* BoundSetOperations<String, Object> operations = redisService.getBoundSetOperations("loginAuth");
  113. if (Boolean.TRUE.equals(operations.isMember(decrypt))) {
  114. throw new RuntimeException("登录信息失效");
  115. }*/
  116. //如果不满足此规则,说明是伪造的
  117. String[] split = decrypt.split(":");
  118. if (split.length != 2) {
  119. throw new RuntimeException("登录信息失效");
  120. }
  121. //如果不满足以下规则,说明是伪造的
  122. String timeStamp = split[1];
  123. if (!NumberUtil.isNumber(timeStamp)) {
  124. throw new RuntimeException("登录信息失效");
  125. }
  126. //三分钟内有效
  127. if (System.currentTimeMillis() - Long.parseLong(timeStamp) > 300000) {
  128. throw new RuntimeException("登录信息失效");
  129. }
  130. /*operations.add(decrypt);
  131. operations.expireAt(DateUtil.endOfDay(new Date()));
  132. */
  133. }
  134. }