SysLoginService.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package com.bizmatics.service.system.impl;
  2. import com.bizmatics.common.core.exception.BusinessException;
  3. import com.bizmatics.common.mvc.utils.IpUtils;
  4. import com.bizmatics.common.mvc.utils.ServletUtils;
  5. import com.bizmatics.common.spring.config.redis.RedisHelper;
  6. import com.bizmatics.model.constants.Constants;
  7. import com.bizmatics.model.system.SysUser;
  8. import com.bizmatics.service.system.ISysConfigService;
  9. import com.bizmatics.service.system.ISysUserService;
  10. import com.bizmatics.service.config.security.LoginUser;
  11. import com.bizmatics.service.util.manager.AsyncManager;
  12. import com.bizmatics.service.util.manager.MessageUtils;
  13. import com.bizmatics.service.util.manager.factory.AsyncFactory;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.security.authentication.AuthenticationManager;
  16. import org.springframework.security.authentication.BadCredentialsException;
  17. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  18. import org.springframework.security.core.Authentication;
  19. import org.springframework.stereotype.Component;
  20. import javax.annotation.Resource;
  21. import java.util.Date;
  22. /**
  23. * 登录校验方法
  24. *
  25. * @author ruoyi
  26. */
  27. @Component
  28. public class SysLoginService
  29. {
  30. @Autowired
  31. private TokenService tokenService;
  32. @Resource
  33. private AuthenticationManager authenticationManager;
  34. @Autowired
  35. private RedisHelper redisHelper;
  36. @Autowired
  37. private ISysUserService userService;
  38. @Autowired
  39. private ISysConfigService configService;
  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. {
  51. boolean captchaOnOff = configService.selectCaptchaOnOff();
  52. // 验证码开关
  53. if (captchaOnOff)
  54. {
  55. validateCaptcha(username, code, uuid);
  56. }
  57. // 用户验证
  58. Authentication authentication = null;
  59. try
  60. {
  61. // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
  62. authentication = authenticationManager
  63. .authenticate(new UsernamePasswordAuthenticationToken(username, password));
  64. }
  65. catch (Exception e)
  66. {
  67. if (e instanceof BadCredentialsException)
  68. {
  69. AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
  70. throw new BusinessException("添加异常");
  71. }
  72. else
  73. {
  74. AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage()));
  75. throw new BusinessException(e.getMessage());
  76. }
  77. }
  78. AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
  79. LoginUser loginUser = (LoginUser) authentication.getPrincipal();
  80. recordLoginInfo(loginUser.getUser());
  81. // 生成token
  82. return tokenService.createToken(loginUser);
  83. }
  84. /**
  85. * 校验验证码
  86. *
  87. * @param username 用户名
  88. * @param code 验证码
  89. * @param uuid 唯一标识
  90. * @return 结果
  91. */
  92. public void validateCaptcha(String username, String code, String uuid)
  93. {
  94. String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
  95. String captcha = redisHelper.get(verifyKey).toString();
  96. redisHelper.delete(verifyKey);
  97. if (captcha == null)
  98. {
  99. AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
  100. throw new BusinessException("发送异常");
  101. }
  102. if (!code.equalsIgnoreCase(captcha))
  103. {
  104. AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
  105. throw new BusinessException("发送异常");
  106. }
  107. }
  108. /**
  109. * 记录登录信息
  110. */
  111. public void recordLoginInfo(SysUser user)
  112. {
  113. user.setLoginIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
  114. user.setLoginDate(new Date());
  115. userService.updateUserProfile(user);
  116. }
  117. }