SysLoginService.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package com.zd.auth.service;
  2. import com.zd.common.core.exception.PreAuthorizeException;
  3. import com.zd.common.core.utils.IpUtils;
  4. import com.zd.model.constant.BaseConstants;
  5. import com.zd.model.constant.HttpStatus;
  6. import com.zd.model.constant.SecurityConstants;
  7. import com.zd.model.constant.UserConstants;
  8. import com.zd.model.domain.R;
  9. import com.zd.model.enums.UserStatus;
  10. import com.zd.model.entity.LoginUser;
  11. import com.zd.system.api.entity.SysLogininfor;
  12. import com.zd.model.entity.SysUser;
  13. import com.zd.system.api.feign.RemoteLogService;
  14. import com.zd.system.api.feign.RemoteUserService;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Component;
  17. import com.zd.common.core.exception.ServiceException;
  18. import com.zd.common.core.utils.SecurityUtils;
  19. import com.zd.common.core.utils.ServletUtils;
  20. import com.zd.common.core.utils.StringUtils;
  21. /**
  22. * 登录校验方法
  23. *
  24. * @author zd
  25. */
  26. @Component
  27. public class SysLoginService {
  28. @Autowired
  29. private RemoteLogService remoteLogService;
  30. @Autowired
  31. private RemoteUserService remoteUserService;
  32. /**
  33. * 登录
  34. */
  35. public LoginUser login(String username, Integer loginType, String password) {
  36. // 用户名或密码为空 错误
  37. if (StringUtils.isAnyBlank(username, password)) {
  38. // recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
  39. throw new ServiceException("用户/密码必须填写", 530);
  40. }
  41. // 密码如果不在指定范围内 错误
  42. if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
  43. || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
  44. // recordLogininfor(username, Constants.LOGIN_FAIL, "用户密码不在指定范围");
  45. throw new ServiceException("用户密码不在指定范围", 530);
  46. }
  47. // 用户名不在指定范围内 错误
  48. if (username.length() < UserConstants.USERNAME_MIN_LENGTH
  49. || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
  50. // recordLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
  51. throw new ServiceException("用户名不在指定范围", 530);
  52. }
  53. // 查询用户信息
  54. R<LoginUser> userResult = remoteUserService.getUserInfo(username, loginType, SecurityConstants.INNER);
  55. if (R.FAIL == userResult.getCode()) {
  56. throw new ServiceException(userResult.getMsg());
  57. }
  58. if (HttpStatus.FORBIDDEN == userResult.getCode()) {
  59. throw new PreAuthorizeException();
  60. }
  61. if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
  62. // recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
  63. throw new ServiceException("登录用户:" + username + " 不存在", 530);
  64. }
  65. LoginUser userInfo = userResult.getData();
  66. SysUser user = userResult.getData().getSysUser();
  67. if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
  68. // recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
  69. throw new ServiceException("对不起,您的账号:" + username + " 已被删除", 530);
  70. }
  71. if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
  72. // recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
  73. throw new ServiceException("对不起,您的账号:" + username + " 已停用", 530);
  74. }
  75. if (!SecurityUtils.matchesPassword(password, user.getPassword())) {
  76. // recordLogininfor(username, Constants.LOGIN_FAIL, "用户密码错误");
  77. throw new ServiceException("用户不存在/密码错误", 530);
  78. }
  79. recordLogininfor(user, BaseConstants.LOGIN_SUCCESS, "登录成功");
  80. return userInfo;
  81. }
  82. public void logout(SysUser user) {
  83. recordLogininfor(user, BaseConstants.LOGOUT, "退出成功");
  84. }
  85. /**
  86. * 注册
  87. */
  88. public void register(String username, String password) {
  89. // 用户名或密码为空 错误
  90. if (StringUtils.isAnyBlank(username, password)) {
  91. throw new ServiceException("用户/密码必须填写");
  92. }
  93. if (username.length() < UserConstants.USERNAME_MIN_LENGTH
  94. || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
  95. throw new ServiceException("账户长度必须在2到20个字符之间");
  96. }
  97. if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
  98. || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
  99. throw new ServiceException("密码长度必须在5到20个字符之间");
  100. }
  101. // 注册用户信息
  102. SysUser sysUser = new SysUser();
  103. sysUser.setUserName(username);
  104. sysUser.setNickName(username);
  105. sysUser.setPassword(SecurityUtils.encryptPassword(password));
  106. R<?> registerResult = remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER);
  107. if (R.FAIL == registerResult.getCode()) {
  108. throw new ServiceException(registerResult.getMsg());
  109. }
  110. // recordLogininfor(username, Constants.REGISTER, "注册成功");
  111. }
  112. /**
  113. * 记录登录信息
  114. *
  115. * @param user 用户名
  116. * @param status 状态
  117. * @param message 消息内容
  118. * @return
  119. */
  120. public void recordLogininfor(SysUser user, String status, String message) {
  121. SysLogininfor logininfor = new SysLogininfor();
  122. logininfor.setUserName(user.getUserName());
  123. logininfor.setIpaddr(IpUtils.getIpAddr(ServletUtils.getRequest()));
  124. logininfor.setMsg(message);
  125. // 日志状态
  126. if (StringUtils.equalsAny(status, BaseConstants.LOGIN_SUCCESS, BaseConstants.LOGOUT, BaseConstants.REGISTER)) {
  127. logininfor.setStatus("0");
  128. } else if (BaseConstants.LOGIN_FAIL.equals(status)) {
  129. logininfor.setStatus("1");
  130. }
  131. logininfor.setUserId(user.getUserId());
  132. logininfor.setDeptId(user.getDeptId());
  133. remoteLogService.saveLogininfor(logininfor, SecurityConstants.INNER);
  134. }
  135. }