SysLoginService.java 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. @Autowired
  24. private RemoteLogService remoteLogService;
  25. @Autowired
  26. private RemoteUserService remoteUserService;
  27. /**
  28. * 登录
  29. */
  30. public LoginUser login(String username, String password) {
  31. // 用户名或密码为空 错误
  32. if (StringUtils.isAnyBlank(username, password)) {
  33. remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
  34. throw new BaseException("用户/密码必须填写");
  35. }
  36. // 密码如果不在指定范围内 错误
  37. if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
  38. || 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
  44. || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
  45. remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
  46. throw new BaseException("用户名不在指定范围");
  47. }
  48. // 查询用户信息
  49. R<LoginUser> userResult = remoteUserService.getUserInfo(username);
  50. if (R.FAIL == userResult.getCode()) {
  51. throw new BaseException(userResult.getMsg());
  52. }
  53. if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
  54. remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
  55. throw new BaseException("登录用户:" + username + " 不存在");
  56. }
  57. LoginUser userInfo = userResult.getData();
  58. SysUser user = userResult.getData().getSysUser();
  59. if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
  60. remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
  61. throw new BaseException("对不起,您的账号:" + username + " 已被删除");
  62. }
  63. if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
  64. remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
  65. throw new BaseException("对不起,您的账号:" + username + " 已停用");
  66. }
  67. if (!SecurityUtils.matchesPassword(password, user.getPassword())) {
  68. remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户密码错误");
  69. throw new BaseException("用户不存在/密码错误");
  70. }
  71. remoteLogService.saveLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
  72. return userInfo;
  73. }
  74. public AjaxResult updateUnionId(SysUser sysUser) {
  75. return remoteUserService.updateUnionId(sysUser);
  76. }
  77. public void logout(String loginName) {
  78. remoteLogService.saveLogininfor(loginName, Constants.LOGOUT, "退出成功");
  79. }
  80. }