SysLoginService.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.Service;
  21. import javax.annotation.Resource;
  22. import javax.servlet.http.HttpServletRequest;
  23. /**
  24. * 登录校验方法
  25. *
  26. * @author Lion Li
  27. */
  28. @Service
  29. public class SysLoginService {
  30. @Autowired
  31. private TokenService tokenService;
  32. @Resource
  33. private AuthenticationManager authenticationManager;
  34. @Autowired
  35. private ISysUserService userService;
  36. @Autowired
  37. private ISysConfigService configService;
  38. @Autowired
  39. private LogininforService asyncService;
  40. /**
  41. * 登录验证
  42. *
  43. * @param username 用户名
  44. * @param password 密码
  45. * @param code 验证码
  46. * @param uuid 唯一标识
  47. * @return 结果
  48. */
  49. public String login(String username, String password, String code, String uuid) {
  50. HttpServletRequest request = ServletUtils.getRequest();
  51. boolean captchaOnOff = configService.selectCaptchaOnOff();
  52. // 验证码开关
  53. if (captchaOnOff) {
  54. validateCaptcha(username, code, uuid, request);
  55. }
  56. // 用户验证
  57. Authentication authentication = null;
  58. try {
  59. // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
  60. authentication = authenticationManager
  61. .authenticate(new UsernamePasswordAuthenticationToken(username, password));
  62. } catch (Exception e) {
  63. if (e instanceof BadCredentialsException) {
  64. asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match"), request);
  65. throw new UserPasswordNotMatchException();
  66. } else {
  67. asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage(), request);
  68. throw new ServiceException(e.getMessage());
  69. }
  70. }
  71. asyncService.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"), request);
  72. LoginUser loginUser = (LoginUser) authentication.getPrincipal();
  73. recordLoginInfo(loginUser.getUserId(), username);
  74. // 生成token
  75. return tokenService.createToken(loginUser);
  76. }
  77. /**
  78. * 校验验证码
  79. *
  80. * @param username 用户名
  81. * @param code 验证码
  82. * @param uuid 唯一标识
  83. * @return 结果
  84. */
  85. public void validateCaptcha(String username, String code, String uuid, HttpServletRequest request) {
  86. String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
  87. String captcha = RedisUtils.getCacheObject(verifyKey);
  88. RedisUtils.deleteObject(verifyKey);
  89. if (captcha == null) {
  90. asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"), request);
  91. throw new CaptchaExpireException();
  92. }
  93. if (!code.equalsIgnoreCase(captcha)) {
  94. asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"), request);
  95. throw new CaptchaException();
  96. }
  97. }
  98. /**
  99. * 记录登录信息
  100. *
  101. * @param userId 用户ID
  102. */
  103. public void recordLoginInfo(Long userId, String username) {
  104. SysUser sysUser = new SysUser();
  105. sysUser.setUserId(userId);
  106. sysUser.setLoginIp(ServletUtils.getClientIP());
  107. sysUser.setLoginDate(DateUtils.getNowDate());
  108. sysUser.setUpdateBy(username);
  109. userService.updateUserProfile(sysUser);
  110. }
  111. }