SysLoginService.java 7.3 KB

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