SysLoginService.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package com.ydd.system.service;
  2. import com.ydd.common.config.EnvConfig;
  3. import com.ydd.common.constant.Constants;
  4. import com.ydd.common.core.domain.entity.SysUser;
  5. import com.ydd.common.core.domain.model.LoginUser;
  6. import com.ydd.common.core.redis.RedisCache;
  7. import com.ydd.common.exception.CustomException;
  8. import com.ydd.common.exception.user.CaptchaException;
  9. import com.ydd.common.exception.user.CaptchaExpireException;
  10. import com.ydd.common.exception.user.UserNameDisableException;
  11. import com.ydd.common.exception.user.UserPasswordNotMatchException;
  12. import com.ydd.common.utils.MessageUtils;
  13. import com.ydd.ecloud.core.enums.StatusEnum;
  14. import com.ydd.ecloud.core.utils.HttpContextUtils;
  15. import com.ydd.framework.web.service.TokenService;
  16. import com.ydd.system.manager.AsyncManager;
  17. import com.ydd.system.manager.factory.AsyncFactory;
  18. import org.apache.commons.collections4.CollectionUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.security.authentication.AuthenticationManager;
  21. import org.springframework.security.authentication.BadCredentialsException;
  22. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  23. import org.springframework.security.core.Authentication;
  24. import org.springframework.stereotype.Component;
  25. import javax.annotation.Resource;
  26. import java.util.List;
  27. /**
  28. * 登录校验方法
  29. *
  30. * @author douya
  31. */
  32. @Component
  33. public class SysLoginService
  34. {
  35. @Autowired
  36. private TokenService tokenService;
  37. @Resource
  38. private AuthenticationManager authenticationManager;
  39. @Autowired
  40. private RedisCache redisCache;
  41. @Resource
  42. private ISysUserService iSysUserService;
  43. @Resource
  44. private ISysRoleService iSysRoleService;
  45. @Resource
  46. private EnvConfig envConfig;
  47. /**
  48. * 登录验证
  49. *
  50. * @param username 用户名
  51. * @param password 密码
  52. * @param code 验证码
  53. * @param uuid 唯一标识
  54. * @return 结果
  55. */
  56. public String login(String username, String password, String code, String uuid)
  57. {
  58. String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
  59. String captcha = redisCache.getCacheObject(verifyKey);
  60. redisCache.deleteObject(verifyKey);
  61. if (captcha == null)
  62. {
  63. AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
  64. throw new CaptchaExpireException();
  65. }
  66. if (!code.equalsIgnoreCase(captcha))
  67. {
  68. AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
  69. throw new CaptchaException();
  70. }
  71. // 代理商禁用不能登录
  72. Integer status = iSysUserService.selectAgentByMoblie(username);
  73. if (status != null) {
  74. if (status.equals(StatusEnum.off.value)) {
  75. AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.username.disable")));
  76. throw new UserNameDisableException();
  77. }
  78. }
  79. // 登录域名校验
  80. this.checkDomain(username);
  81. // 用户验证
  82. Authentication authentication = null;
  83. try
  84. {
  85. // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
  86. authentication = authenticationManager
  87. .authenticate(new UsernamePasswordAuthenticationToken(username, password));
  88. }
  89. catch (Exception e)
  90. {
  91. if (e instanceof BadCredentialsException)
  92. {
  93. AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
  94. throw new UserPasswordNotMatchException();
  95. }
  96. else
  97. {
  98. AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
  99. throw new CustomException(e.getMessage());
  100. }
  101. }
  102. AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
  103. LoginUser loginUser = (LoginUser) authentication.getPrincipal();
  104. // 生成token
  105. return tokenService.createToken(loginUser);
  106. }
  107. private void checkDomain(String username) {
  108. String serverName = HttpContextUtils.getHttpServletRequest().getServerName();
  109. if (serverName != null && !serverName.contains("localhost")) {
  110. SysUser sysUser = iSysUserService.selectUserByUserName(username);
  111. if (sysUser != null) {
  112. List<Integer> roleIdList = iSysRoleService.selectRoleListByUserId(sysUser.getUserId());
  113. if (CollectionUtils.isNotEmpty(roleIdList)) {
  114. Long roleId = Long.parseLong(roleIdList.get(0).toString());
  115. // 外部角色
  116. if (Constants.EXTERNAL_ROLE_IDS.contains(roleId)) {
  117. if (!serverName.contains(Constants.getExternalDomain(envConfig.getProfiles()))) {
  118. AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, "用户不存在!"));
  119. throw new CustomException("用户不存在!");
  120. }
  121. } else {
  122. // 内部角色
  123. if (!serverName.contains(Constants.getInSideDomain(envConfig.getProfiles()))) {
  124. AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, "用户不存在!"));
  125. throw new CustomException("用户不存在!");
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }
  132. }