SysLoginService.java 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package com.boman.auth.service;
  2. import com.boman.domain.dto.AjaxResult;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Component;
  5. import com.boman.domain.constant.Constants;
  6. import com.boman.domain.constant.UserConstants;
  7. import com.boman.domain.dto.R;
  8. import com.boman.common.core.enums.UserStatus;
  9. import com.boman.common.core.exception.BaseException;
  10. import com.boman.common.core.utils.SecurityUtils;
  11. import com.boman.common.core.utils.StringUtils;
  12. import com.boman.system.api.RemoteLogService;
  13. import com.boman.system.api.RemoteUserService;
  14. import com.boman.domain.SysUser;
  15. import com.boman.system.api.model.LoginUser;
  16. /**
  17. * 登录校验方法
  18. *
  19. * @author ruoyi
  20. */
  21. @Component
  22. public class SysLoginService
  23. {
  24. @Autowired
  25. private RemoteLogService remoteLogService;
  26. @Autowired
  27. private RemoteUserService remoteUserService;
  28. /**
  29. * 登录
  30. */
  31. public LoginUser login(String username, String password) {
  32. // 用户名或密码为空 错误
  33. if (StringUtils.isAnyBlank(username, password)) {
  34. remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
  35. throw new BaseException("用户/密码必须填写");
  36. }
  37. // 密码如果不在指定范围内 错误
  38. if (password.length() < UserConstants.PASSWORD_MIN_LENGTH || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
  39. remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户密码不在指定范围");
  40. throw new BaseException("用户密码不在指定范围");
  41. }
  42. // 用户名不在指定范围内 错误
  43. if (username.length() < UserConstants.USERNAME_MIN_LENGTH || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
  44. remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
  45. throw new BaseException("用户名不在指定范围");
  46. }
  47. // 查询用户信息
  48. R<LoginUser> userResult = remoteUserService.getUserInfo(username);
  49. if (R.FAIL == userResult.getCode()) {
  50. throw new BaseException(userResult.getMsg());
  51. }
  52. if (StringUtils.isNull(userResult.getData())) {
  53. remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
  54. throw new BaseException("登录用户:" + username + " 不存在");
  55. }
  56. LoginUser userInfo = userResult.getData();
  57. SysUser user = userResult.getData().getSysUser();
  58. if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
  59. remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
  60. throw new BaseException("对不起,您的账号:" + username + " 已被删除");
  61. }
  62. if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
  63. remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
  64. throw new BaseException("对不起,您的账号:" + username + " 已停用");
  65. }
  66. if (!SecurityUtils.matchesPassword(password, user.getPassword())) {
  67. remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户密码错误");
  68. throw new BaseException("用户不存在/密码错误");
  69. }
  70. remoteLogService.saveLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
  71. return userInfo;
  72. }
  73. public AjaxResult updateUnionId(SysUser sysUser) {
  74. return remoteUserService.updateUnionId(sysUser);
  75. }
  76. public void logout(String loginName)
  77. {
  78. remoteLogService.saveLogininfor(loginName, Constants.LOGOUT, "退出成功");
  79. }
  80. }