2021-09-17 14:48:36 +08:00
|
|
|
package com.ruoyi.system.service;
|
2020-02-13 10:48:51 +08:00
|
|
|
|
2021-09-23 19:13:58 +08:00
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
2020-02-13 10:48:51 +08:00
|
|
|
import com.ruoyi.common.constant.Constants;
|
2021-05-25 10:00:36 +08:00
|
|
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
2021-09-17 14:48:36 +08:00
|
|
|
import com.ruoyi.common.core.service.LogininforService;
|
2021-09-23 19:13:58 +08:00
|
|
|
import com.ruoyi.common.enums.UserStatus;
|
2021-08-17 10:38:07 +08:00
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
2020-02-13 10:48:51 +08:00
|
|
|
import com.ruoyi.common.exception.user.CaptchaException;
|
|
|
|
import com.ruoyi.common.exception.user.CaptchaExpireException;
|
|
|
|
import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
|
2021-09-23 19:13:58 +08:00
|
|
|
import com.ruoyi.common.utils.*;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2021-05-25 13:40:18 +08:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2021-09-23 19:13:58 +08:00
|
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
2021-09-27 17:58:36 +08:00
|
|
|
import org.springframework.stereotype.Service;
|
2021-05-25 13:40:18 +08:00
|
|
|
|
2021-06-12 20:14:50 +08:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
2020-02-13 10:48:51 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 登录校验方法
|
2021-05-25 10:00:36 +08:00
|
|
|
*
|
2020-02-13 10:48:51 +08:00
|
|
|
* @author ruoyi
|
|
|
|
*/
|
2021-09-23 19:13:58 +08:00
|
|
|
@Slf4j
|
2021-09-27 17:58:36 +08:00
|
|
|
@Service
|
|
|
|
public class SysLoginService {
|
2020-02-13 10:48:51 +08:00
|
|
|
|
2021-09-27 17:58:36 +08:00
|
|
|
@Autowired
|
2021-05-25 10:00:36 +08:00
|
|
|
private ISysUserService userService;
|
|
|
|
|
2021-09-27 17:58:36 +08:00
|
|
|
@Autowired
|
|
|
|
private ISysConfigService configService;
|
2021-07-13 13:34:10 +08:00
|
|
|
|
2021-09-27 17:58:36 +08:00
|
|
|
@Autowired
|
|
|
|
private LogininforService asyncService;
|
2021-06-12 20:14:50 +08:00
|
|
|
|
2020-02-13 10:48:51 +08:00
|
|
|
/**
|
|
|
|
* 登录验证
|
2021-05-25 10:00:36 +08:00
|
|
|
*
|
2020-02-13 10:48:51 +08:00
|
|
|
* @param username 用户名
|
|
|
|
* @param password 密码
|
2021-09-27 17:58:36 +08:00
|
|
|
* @param code 验证码
|
|
|
|
* @param uuid 唯一标识
|
2020-02-13 10:48:51 +08:00
|
|
|
* @return 结果
|
|
|
|
*/
|
2021-09-27 17:58:36 +08:00
|
|
|
public String login(String username, String password, String code, String uuid) {
|
|
|
|
HttpServletRequest request = ServletUtils.getRequest();
|
|
|
|
boolean captchaOnOff = configService.selectCaptchaOnOff();
|
2021-07-13 13:34:10 +08:00
|
|
|
// 验证码开关
|
2021-09-27 17:58:36 +08:00
|
|
|
if (captchaOnOff) {
|
2021-07-24 19:03:36 +08:00
|
|
|
validateCaptcha(username, code, uuid, request);
|
2021-07-13 13:34:10 +08:00
|
|
|
}
|
2021-09-23 19:13:58 +08:00
|
|
|
SysUser user = userService.selectUserByUserName(username);
|
2021-09-28 09:46:56 +08:00
|
|
|
if (StringUtils.isNull(user)) {
|
2021-09-23 19:13:58 +08:00
|
|
|
log.info("登录用户:{} 不存在.", username);
|
|
|
|
throw new ServiceException("登录用户:" + username + " 不存在");
|
2021-09-28 09:46:56 +08:00
|
|
|
} else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
|
2021-09-23 19:13:58 +08:00
|
|
|
log.info("登录用户:{} 已被删除.", username);
|
|
|
|
throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
|
2021-09-28 09:46:56 +08:00
|
|
|
} else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
|
2021-09-23 19:13:58 +08:00
|
|
|
log.info("登录用户:{} 已被停用.", username);
|
|
|
|
throw new ServiceException("对不起,您的账号:" + username + " 已停用");
|
2020-02-13 10:48:51 +08:00
|
|
|
}
|
2021-09-23 19:13:58 +08:00
|
|
|
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
|
|
|
String encodePassword = passwordEncoder.encode(password);
|
2021-09-28 09:46:56 +08:00
|
|
|
if (SecurityUtils.matchesPassword(user.getPassword(), encodePassword)) {
|
2021-09-23 19:13:58 +08:00
|
|
|
asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match"), request);
|
|
|
|
throw new UserPasswordNotMatchException();
|
|
|
|
}
|
|
|
|
|
2021-06-12 20:14:50 +08:00
|
|
|
asyncService.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"), request);
|
2021-09-26 17:15:40 +08:00
|
|
|
recordLoginInfo(user.getUserId());
|
2020-02-13 10:48:51 +08:00
|
|
|
// 生成token
|
2021-09-23 19:13:58 +08:00
|
|
|
StpUtil.login(user.getUserId(), "PC");
|
|
|
|
return StpUtil.getTokenValue();
|
2020-02-13 10:48:51 +08:00
|
|
|
}
|
2021-05-25 10:00:36 +08:00
|
|
|
|
2021-07-13 13:34:10 +08:00
|
|
|
/**
|
|
|
|
* 校验验证码
|
|
|
|
*
|
|
|
|
* @param username 用户名
|
2021-09-27 17:58:36 +08:00
|
|
|
* @param code 验证码
|
|
|
|
* @param uuid 唯一标识
|
2021-07-13 13:34:10 +08:00
|
|
|
* @return 结果
|
|
|
|
*/
|
2021-07-24 19:03:36 +08:00
|
|
|
public void validateCaptcha(String username, String code, String uuid, HttpServletRequest request) {
|
2021-09-27 17:58:36 +08:00
|
|
|
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
|
|
|
|
String captcha = RedisUtils.getCacheObject(verifyKey);
|
2021-09-07 13:20:24 +08:00
|
|
|
RedisUtils.deleteObject(verifyKey);
|
2021-09-27 17:58:36 +08:00
|
|
|
if (captcha == null) {
|
|
|
|
asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"), request);
|
|
|
|
throw new CaptchaExpireException();
|
|
|
|
}
|
|
|
|
if (!code.equalsIgnoreCase(captcha)) {
|
|
|
|
asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"), request);
|
|
|
|
throw new CaptchaException();
|
|
|
|
}
|
2021-07-13 13:34:10 +08:00
|
|
|
}
|
|
|
|
|
2021-05-25 10:00:36 +08:00
|
|
|
/**
|
|
|
|
* 记录登录信息
|
2021-09-26 10:05:29 +08:00
|
|
|
*
|
|
|
|
* @param userId 用户ID
|
2021-05-25 10:00:36 +08:00
|
|
|
*/
|
2021-09-27 17:58:36 +08:00
|
|
|
public void recordLoginInfo(Long userId) {
|
2021-09-26 10:05:29 +08:00
|
|
|
SysUser sysUser = new SysUser();
|
|
|
|
sysUser.setUserId(userId);
|
2021-09-26 10:08:24 +08:00
|
|
|
sysUser.setLoginIp(ServletUtils.getClientIP());
|
2021-09-26 10:05:29 +08:00
|
|
|
sysUser.setLoginDate(DateUtils.getNowDate());
|
|
|
|
userService.updateUserProfile(sysUser);
|
2021-05-25 10:00:36 +08:00
|
|
|
}
|
2020-02-13 10:48:51 +08:00
|
|
|
}
|