SysLoginService.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.boot.autoconfigure.condition.ConditionalOnProperty;
  23. import org.springframework.data.redis.core.BoundSetOperations;
  24. import org.springframework.stereotype.Component;
  25. import java.util.Date;
  26. /**
  27. * 登录校验方法
  28. *
  29. * @author xunmei
  30. */
  31. @Component
  32. @ConditionalOnProperty(name = "loginPlatform", matchIfMissing = true, havingValue = "system")
  33. public class SysLoginService implements LoginService {
  34. @Autowired
  35. private RemoteUserService remoteUserService;
  36. @Autowired
  37. private SysPasswordService passwordService;
  38. @Autowired
  39. private SysRecordLogService recordLogService;
  40. /**
  41. * 登录
  42. */
  43. @Override
  44. public LoginUser loginByPassword(String username, String password, String platformType) {
  45. // 用户名或密码为空 错误
  46. if (StringUtils.isAnyBlank(username, password)) {
  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. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户密码不在指定范围", platformType);
  54. throw new ServiceException("用户密码不在指定范围");
  55. }
  56. // 用户名不在指定范围内 错误
  57. if (username.length() < UserConstants.USERNAME_MIN_LENGTH
  58. || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
  59. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围", platformType);
  60. throw new ServiceException("用户名不在指定范围");
  61. }
  62. // IP黑名单校验
  63. String blackStr = Convert.toStr(RedisUtils.getCacheObject(CacheConstants.SYS_LOGIN_BLACKIPLIST));
  64. if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr())) {
  65. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "很遗憾,访问IP已被列入系统黑名单", platformType);
  66. throw new ServiceException("很遗憾,访问IP已被列入系统黑名单");
  67. }
  68. // 查询用户信息
  69. R<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
  70. if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
  71. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在", platformType);
  72. throw new ServiceException("登录用户:" + username + " 不存在");
  73. }
  74. if (R.FAIL == userResult.getCode()) {
  75. throw new ServiceException(userResult.getMsg());
  76. }
  77. LoginUser userInfo = userResult.getData();
  78. userInfo.setPlatformType(platformType);
  79. SysUser user = userResult.getData().getSysUser();
  80. if (UserStatus.DELETED.getCode().equals(user.getDeleted())) {
  81. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除", platformType);
  82. throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
  83. }
  84. if (UserStatus.DISABLE.getCode().equals(Integer.parseInt(user.getIsLock()))) {
  85. recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员", platformType);
  86. throw new ServiceException("对不起,您的账号:" + username + " 已停用");
  87. }
  88. passwordService.validate(user, password, platformType);
  89. recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功", platformType);
  90. return userInfo;
  91. }
  92. @Override
  93. public LoginUser loginByToken(String token, String loginType) throws Exception {
  94. throw new RuntimeException("系统不支持token登录");
  95. }
  96. @Override
  97. public void logout(String loginName, String platformType) {
  98. recordLogService.recordLogininfor(loginName, Constants.LOGOUT, "退出成功", platformType);
  99. }
  100. public void checkLogin(String authCode) {
  101. if (SecurityUtils.isApp()) {
  102. return;
  103. }
  104. String decrypt = AsymmetricEncryptionUtil.decrypt(authCode);
  105. if (ObjectUtil.isEmpty(decrypt) || null == decrypt) {
  106. throw new RuntimeException("登录信息失效");
  107. }
  108. //如果redis中存在此key,说明已经登录过了
  109. /* BoundSetOperations<String, Object> operations = redisService.getBoundSetOperations("loginAuth");
  110. if (Boolean.TRUE.equals(operations.isMember(decrypt))) {
  111. throw new RuntimeException("登录信息失效");
  112. }*/
  113. //如果不满足此规则,说明是伪造的
  114. String[] split = decrypt.split(":");
  115. if (split.length != 2) {
  116. throw new RuntimeException("登录信息失效");
  117. }
  118. //如果不满足以下规则,说明是伪造的
  119. String timeStamp = split[1];
  120. if (!NumberUtil.isNumber(timeStamp)) {
  121. throw new RuntimeException("登录信息失效");
  122. }
  123. //三分钟内有效
  124. if (System.currentTimeMillis() - Long.parseLong(timeStamp) > 300000) {
  125. throw new RuntimeException("登录信息失效");
  126. }
  127. /*operations.add(decrypt);
  128. operations.expireAt(DateUtil.endOfDay(new Date()));
  129. */
  130. }
  131. }