SysLoginService.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package com.ruoyi.system.service;
  2. import cn.dev33.satoken.stp.StpUtil;
  3. import com.ruoyi.common.constant.Constants;
  4. import com.ruoyi.common.core.domain.entity.SysUser;
  5. import com.ruoyi.common.core.domain.model.LoginUser;
  6. import com.ruoyi.common.core.service.LogininforService;
  7. import com.ruoyi.common.enums.DeviceType;
  8. import com.ruoyi.common.enums.UserStatus;
  9. import com.ruoyi.common.enums.UserType;
  10. import com.ruoyi.common.exception.ServiceException;
  11. import com.ruoyi.common.exception.user.CaptchaException;
  12. import com.ruoyi.common.exception.user.CaptchaExpireException;
  13. import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
  14. import com.ruoyi.common.utils.*;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.stereotype.Service;
  18. import javax.servlet.http.HttpServletRequest;
  19. /**
  20. * 登录校验方法
  21. *
  22. * @author Lion Li
  23. */
  24. @Slf4j
  25. @Service
  26. public class SysLoginService {
  27. @Autowired
  28. private ISysUserService userService;
  29. @Autowired
  30. private ISysConfigService configService;
  31. @Autowired
  32. private LogininforService asyncService;
  33. @Autowired
  34. private SysPermissionService permissionService;
  35. /**
  36. * 登录验证
  37. *
  38. * @param username 用户名
  39. * @param password 密码
  40. * @param code 验证码
  41. * @param uuid 唯一标识
  42. * @return 结果
  43. */
  44. public String login(String username, String password, String code, String uuid) {
  45. HttpServletRequest request = ServletUtils.getRequest();
  46. boolean captchaOnOff = configService.selectCaptchaOnOff();
  47. // 验证码开关
  48. if (captchaOnOff) {
  49. validateCaptcha(username, code, uuid, request);
  50. }
  51. SysUser user = userService.selectUserByUserName(username);
  52. if (StringUtils.isNull(user)) {
  53. log.info("登录用户:{} 不存在.", username);
  54. throw new ServiceException("登录用户:" + username + " 不存在");
  55. } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
  56. log.info("登录用户:{} 已被删除.", username);
  57. throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
  58. } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
  59. log.info("登录用户:{} 已被停用.", username);
  60. throw new ServiceException("对不起,您的账号:" + username + " 已停用");
  61. }
  62. if (!SecurityUtils.matchesPassword(password, user.getPassword())) {
  63. asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match"), request);
  64. throw new UserPasswordNotMatchException();
  65. }
  66. LoginUser loginUser = new LoginUser();
  67. loginUser.setUserId(user.getUserId());
  68. loginUser.setDeptId(user.getDeptId());
  69. loginUser.setUsername(user.getUserName());
  70. loginUser.setMenuPermission(permissionService.getMenuPermission(user));
  71. loginUser.setRolePermission(permissionService.getRolePermission(user));
  72. asyncService.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"), request);
  73. recordLoginInfo(user.getUserId(), username);
  74. // 生成token
  75. LoginUtils.loginByDevice(loginUser, UserType.SYS_USER, DeviceType.PC);
  76. return StpUtil.getTokenValue();
  77. }
  78. /**
  79. * 校验验证码
  80. *
  81. * @param username 用户名
  82. * @param code 验证码
  83. * @param uuid 唯一标识
  84. * @return 结果
  85. */
  86. public void validateCaptcha(String username, String code, String uuid, HttpServletRequest request) {
  87. String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
  88. String captcha = RedisUtils.getCacheObject(verifyKey);
  89. RedisUtils.deleteObject(verifyKey);
  90. if (captcha == null) {
  91. asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"), request);
  92. throw new CaptchaExpireException();
  93. }
  94. if (!code.equalsIgnoreCase(captcha)) {
  95. asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"), request);
  96. throw new CaptchaException();
  97. }
  98. }
  99. /**
  100. * 记录登录信息
  101. *
  102. * @param userId 用户ID
  103. */
  104. public void recordLoginInfo(Long userId, String username) {
  105. SysUser sysUser = new SysUser();
  106. sysUser.setUserId(userId);
  107. sysUser.setLoginIp(ServletUtils.getClientIP());
  108. sysUser.setLoginDate(DateUtils.getNowDate());
  109. sysUser.setUpdateBy(username);
  110. userService.updateUserProfile(sysUser);
  111. }
  112. }