SysLoginService.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package com.ruoyi.system.service;
  2. import com.ruoyi.common.constant.Constants;
  3. import com.ruoyi.common.core.domain.entity.SysUser;
  4. import com.ruoyi.common.core.domain.model.LoginUser;
  5. import com.ruoyi.common.core.service.LogininforService;
  6. import com.ruoyi.common.core.service.TokenService;
  7. import com.ruoyi.common.exception.ServiceException;
  8. import com.ruoyi.common.exception.user.CaptchaException;
  9. import com.ruoyi.common.exception.user.CaptchaExpireException;
  10. import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
  11. import com.ruoyi.common.utils.DateUtils;
  12. import com.ruoyi.common.utils.MessageUtils;
  13. import com.ruoyi.common.utils.RedisUtils;
  14. import com.ruoyi.common.utils.ServletUtils;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.security.authentication.AuthenticationManager;
  17. import org.springframework.security.authentication.BadCredentialsException;
  18. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  19. import org.springframework.security.core.Authentication;
  20. import org.springframework.stereotype.Component;
  21. import javax.annotation.Resource;
  22. import javax.servlet.http.HttpServletRequest;
  23. /**
  24. * 登录校验方法
  25. *
  26. * @author ruoyi
  27. */
  28. @Component
  29. public class SysLoginService
  30. {
  31. @Autowired
  32. private TokenService tokenService;
  33. @Resource
  34. private AuthenticationManager authenticationManager;
  35. @Autowired
  36. private ISysUserService userService;
  37. @Autowired
  38. private ISysConfigService configService;
  39. @Autowired
  40. private LogininforService asyncService;
  41. /**
  42. * 登录验证
  43. *
  44. * @param username 用户名
  45. * @param password 密码
  46. * @param code 验证码
  47. * @param uuid 唯一标识
  48. * @return 结果
  49. */
  50. public String login(String username, String password, String code, String uuid)
  51. {
  52. HttpServletRequest request = ServletUtils.getRequest();
  53. boolean captchaOnOff = configService.selectCaptchaOnOff();
  54. // 验证码开关
  55. if (captchaOnOff)
  56. {
  57. validateCaptcha(username, code, uuid, request);
  58. }
  59. // 用户验证
  60. Authentication authentication = null;
  61. try
  62. {
  63. // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
  64. authentication = authenticationManager
  65. .authenticate(new UsernamePasswordAuthenticationToken(username, password));
  66. }
  67. catch (Exception e)
  68. {
  69. if (e instanceof BadCredentialsException)
  70. {
  71. asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match"), request);
  72. throw new UserPasswordNotMatchException();
  73. }
  74. else
  75. {
  76. AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
  77. throw new ServiceException(e.getMessage());
  78. }
  79. }
  80. asyncService.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"), request);
  81. LoginUser loginUser = (LoginUser) authentication.getPrincipal();
  82. recordLoginInfo(loginUser.getUserId());
  83. // 生成token
  84. return tokenService.createToken(loginUser);
  85. }
  86. /**
  87. * 校验验证码
  88. *
  89. * @param username 用户名
  90. * @param code 验证码
  91. * @param uuid 唯一标识
  92. * @return 结果
  93. */
  94. public void validateCaptcha(String username, String code, String uuid, HttpServletRequest request) {
  95. String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
  96. String captcha = RedisUtils.getCacheObject(verifyKey);
  97. RedisUtils.deleteObject(verifyKey);
  98. if (captcha == null) {
  99. asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"), request);
  100. throw new CaptchaExpireException();
  101. }
  102. if (!code.equalsIgnoreCase(captcha)) {
  103. asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"), request);
  104. throw new CaptchaException();
  105. }
  106. }
  107. /**
  108. * 记录登录信息
  109. *
  110. * @param userId 用户ID
  111. */
  112. public void recordLoginInfo(Long userId)
  113. {
  114. SysUser sysUser = new SysUser();
  115. sysUser.setUserId(userId);
  116. sysUser.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
  117. sysUser.setLoginDate(DateUtils.getNowDate());
  118. userService.updateUserProfile(sysUser);
  119. }
  120. }