|
@@ -5,23 +5,28 @@ import cn.dev33.satoken.secure.BCrypt;
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.ruoyi.common.constant.CacheConstants;
|
|
|
import com.ruoyi.common.constant.Constants;
|
|
|
+import com.ruoyi.common.core.domain.event.LogininforEvent;
|
|
|
import com.ruoyi.common.core.domain.dto.RoleDTO;
|
|
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
|
|
import com.ruoyi.common.core.domain.model.LoginUser;
|
|
|
import com.ruoyi.common.core.domain.model.XcxLoginUser;
|
|
|
-import com.ruoyi.common.core.service.LogininforService;
|
|
|
import com.ruoyi.common.enums.DeviceType;
|
|
|
import com.ruoyi.common.enums.LoginType;
|
|
|
import com.ruoyi.common.enums.UserStatus;
|
|
|
-import com.ruoyi.common.exception.user.*;
|
|
|
+import com.ruoyi.common.exception.user.CaptchaException;
|
|
|
+import com.ruoyi.common.exception.user.CaptchaExpireException;
|
|
|
+import com.ruoyi.common.exception.user.UserException;
|
|
|
import com.ruoyi.common.helper.LoginHelper;
|
|
|
import com.ruoyi.common.utils.DateUtils;
|
|
|
import com.ruoyi.common.utils.MessageUtils;
|
|
|
import com.ruoyi.common.utils.ServletUtils;
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
import com.ruoyi.common.utils.redis.RedisUtils;
|
|
|
+import com.ruoyi.common.utils.spring.SpringUtils;
|
|
|
+import com.ruoyi.system.mapper.SysUserMapper;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
@@ -42,9 +47,8 @@ import java.util.function.Supplier;
|
|
|
@Service
|
|
|
public class SysLoginService {
|
|
|
|
|
|
- private final ISysUserService userService;
|
|
|
+ private final SysUserMapper userMapper;
|
|
|
private final ISysConfigService configService;
|
|
|
- private final LogininforService asyncService;
|
|
|
private final SysPermissionService permissionService;
|
|
|
|
|
|
@Value("${user.password.maxRetryCount}")
|
|
@@ -76,7 +80,7 @@ public class SysLoginService {
|
|
|
// 生成token
|
|
|
LoginHelper.loginByDevice(loginUser, DeviceType.PC);
|
|
|
|
|
|
- asyncService.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"), request);
|
|
|
+ recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
|
|
|
recordLoginInfo(user.getUserId(), username);
|
|
|
return StpUtil.getTokenValue();
|
|
|
}
|
|
@@ -85,21 +89,19 @@ public class SysLoginService {
|
|
|
// 通过手机号查找用户
|
|
|
SysUser user = loadUserByPhonenumber(phonenumber);
|
|
|
|
|
|
- HttpServletRequest request = ServletUtils.getRequest();
|
|
|
- checkLogin(LoginType.SMS, user.getUserName(), () -> !validateSmsCode(phonenumber, smsCode, request));
|
|
|
+ checkLogin(LoginType.SMS, user.getUserName(), () -> !validateSmsCode(phonenumber, smsCode));
|
|
|
// 此处可根据登录用户的数据不同 自行创建 loginUser
|
|
|
LoginUser loginUser = buildLoginUser(user);
|
|
|
// 生成token
|
|
|
LoginHelper.loginByDevice(loginUser, DeviceType.APP);
|
|
|
|
|
|
- asyncService.recordLogininfor(user.getUserName(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"), request);
|
|
|
+ recordLogininfor(user.getUserName(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
|
|
|
recordLoginInfo(user.getUserId(), user.getUserName());
|
|
|
return StpUtil.getTokenValue();
|
|
|
}
|
|
|
|
|
|
|
|
|
public String xcxLogin(String xcxCode) {
|
|
|
- HttpServletRequest request = ServletUtils.getRequest();
|
|
|
// xcxCode 为 小程序调用 wx.login 授权后获取
|
|
|
// todo 以下自行实现
|
|
|
// 校验 appid + appsrcret + xcxCode 调用登录凭证校验接口 获取 session_key 与 openid
|
|
@@ -115,7 +117,7 @@ public class SysLoginService {
|
|
|
// 生成token
|
|
|
LoginHelper.loginByDevice(loginUser, DeviceType.XCX);
|
|
|
|
|
|
- asyncService.recordLogininfor(user.getUserName(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"), request);
|
|
|
+ recordLogininfor(user.getUserName(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
|
|
|
recordLoginInfo(user.getUserId(), user.getUserName());
|
|
|
return StpUtil.getTokenValue();
|
|
|
}
|
|
@@ -127,18 +129,35 @@ public class SysLoginService {
|
|
|
try {
|
|
|
LoginUser loginUser = LoginHelper.getLoginUser();
|
|
|
StpUtil.logout();
|
|
|
- asyncService.recordLogininfor(loginUser.getUsername(), Constants.LOGOUT, MessageUtils.message("user.logout.success"), ServletUtils.getRequest());
|
|
|
+ recordLogininfor(loginUser.getUsername(), Constants.LOGOUT, MessageUtils.message("user.logout.success"));
|
|
|
} catch (NotLoginException ignored) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 记录登录信息
|
|
|
+ *
|
|
|
+ * @param username 用户名
|
|
|
+ * @param status 状态
|
|
|
+ * @param message 消息内容
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private void recordLogininfor(String username, String status, String message) {
|
|
|
+ LogininforEvent logininforEvent = new LogininforEvent();
|
|
|
+ logininforEvent.setUsername(username);
|
|
|
+ logininforEvent.setStatus(status);
|
|
|
+ logininforEvent.setMessage(message);
|
|
|
+ logininforEvent.setRequest(ServletUtils.getRequest());
|
|
|
+ SpringUtils.context().publishEvent(logininforEvent);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 校验短信验证码
|
|
|
*/
|
|
|
- private boolean validateSmsCode(String phonenumber, String smsCode, HttpServletRequest request) {
|
|
|
+ private boolean validateSmsCode(String phonenumber, String smsCode) {
|
|
|
String code = RedisUtils.getCacheObject(CacheConstants.CAPTCHA_CODE_KEY + phonenumber);
|
|
|
if (StringUtils.isBlank(code)) {
|
|
|
- asyncService.recordLogininfor(phonenumber, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"), request);
|
|
|
+ recordLogininfor(phonenumber, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"));
|
|
|
throw new CaptchaExpireException();
|
|
|
}
|
|
|
return code.equals(smsCode);
|
|
@@ -156,43 +175,41 @@ public class SysLoginService {
|
|
|
String captcha = RedisUtils.getCacheObject(verifyKey);
|
|
|
RedisUtils.deleteObject(verifyKey);
|
|
|
if (captcha == null) {
|
|
|
- asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"), request);
|
|
|
+ recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"));
|
|
|
throw new CaptchaExpireException();
|
|
|
}
|
|
|
if (!code.equalsIgnoreCase(captcha)) {
|
|
|
- asyncService.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"), request);
|
|
|
+ recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"));
|
|
|
throw new CaptchaException();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private SysUser loadUserByUsername(String username) {
|
|
|
- SysUser user = userService.selectUserByUserName(username);
|
|
|
+ SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>()
|
|
|
+ .select(SysUser::getUserName, SysUser::getStatus)
|
|
|
+ .eq(SysUser::getUserName, username));
|
|
|
if (ObjectUtil.isNull(user)) {
|
|
|
log.info("登录用户:{} 不存在.", username);
|
|
|
throw new UserException("user.not.exists", username);
|
|
|
- } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
|
|
|
- log.info("登录用户:{} 已被删除.", username);
|
|
|
- throw new UserException("user.password.delete", username);
|
|
|
} else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
|
|
|
log.info("登录用户:{} 已被停用.", username);
|
|
|
throw new UserException("user.blocked", username);
|
|
|
}
|
|
|
- return user;
|
|
|
+ return userMapper.selectUserByUserName(username);
|
|
|
}
|
|
|
|
|
|
private SysUser loadUserByPhonenumber(String phonenumber) {
|
|
|
- SysUser user = userService.selectUserByPhonenumber(phonenumber);
|
|
|
+ SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>()
|
|
|
+ .select(SysUser::getPhonenumber, SysUser::getStatus)
|
|
|
+ .eq(SysUser::getPhonenumber, phonenumber));
|
|
|
if (ObjectUtil.isNull(user)) {
|
|
|
log.info("登录用户:{} 不存在.", phonenumber);
|
|
|
throw new UserException("user.not.exists", phonenumber);
|
|
|
- } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
|
|
|
- log.info("登录用户:{} 已被删除.", phonenumber);
|
|
|
- throw new UserException("user.password.delete", phonenumber);
|
|
|
} else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
|
|
|
log.info("登录用户:{} 已被停用.", phonenumber);
|
|
|
throw new UserException("user.blocked", phonenumber);
|
|
|
}
|
|
|
- return user;
|
|
|
+ return userMapper.selectUserByPhonenumber(phonenumber);
|
|
|
}
|
|
|
|
|
|
private SysUser loadUserByOpenid(String openid) {
|
|
@@ -202,9 +219,6 @@ public class SysLoginService {
|
|
|
if (ObjectUtil.isNull(user)) {
|
|
|
log.info("登录用户:{} 不存在.", openid);
|
|
|
// todo 用户不存在 业务逻辑自行实现
|
|
|
- } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
|
|
|
- log.info("登录用户:{} 已被删除.", openid);
|
|
|
- // todo 用户已被删除 业务逻辑自行实现
|
|
|
} else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
|
|
|
log.info("登录用户:{} 已被停用.", openid);
|
|
|
// todo 用户已被停用 业务逻辑自行实现
|
|
@@ -240,14 +254,13 @@ public class SysLoginService {
|
|
|
sysUser.setLoginIp(ServletUtils.getClientIP());
|
|
|
sysUser.setLoginDate(DateUtils.getNowDate());
|
|
|
sysUser.setUpdateBy(username);
|
|
|
- userService.updateUserProfile(sysUser);
|
|
|
+ userMapper.updateById(sysUser);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 登录校验
|
|
|
*/
|
|
|
private void checkLogin(LoginType loginType, String username, Supplier<Boolean> supplier) {
|
|
|
- HttpServletRequest request = ServletUtils.getRequest();
|
|
|
String errorKey = CacheConstants.PWD_ERR_CNT_KEY + username;
|
|
|
String loginFail = Constants.LOGIN_FAIL;
|
|
|
|
|
@@ -255,7 +268,7 @@ public class SysLoginService {
|
|
|
Integer errorNumber = RedisUtils.getCacheObject(errorKey);
|
|
|
// 锁定时间内登录 则踢出
|
|
|
if (ObjectUtil.isNotNull(errorNumber) && errorNumber.equals(maxRetryCount)) {
|
|
|
- asyncService.recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitExceed(), maxRetryCount, lockTime), request);
|
|
|
+ recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitExceed(), maxRetryCount, lockTime));
|
|
|
throw new UserException(loginType.getRetryLimitExceed(), maxRetryCount, lockTime);
|
|
|
}
|
|
|
|
|
@@ -265,12 +278,12 @@ public class SysLoginService {
|
|
|
// 达到规定错误次数 则锁定登录
|
|
|
if (errorNumber.equals(maxRetryCount)) {
|
|
|
RedisUtils.setCacheObject(errorKey, errorNumber, Duration.ofMinutes(lockTime));
|
|
|
- asyncService.recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitExceed(), maxRetryCount, lockTime), request);
|
|
|
+ recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitExceed(), maxRetryCount, lockTime));
|
|
|
throw new UserException(loginType.getRetryLimitExceed(), maxRetryCount, lockTime);
|
|
|
} else {
|
|
|
// 未达到规定错误次数 则递增
|
|
|
RedisUtils.setCacheObject(errorKey, errorNumber);
|
|
|
- asyncService.recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitCount(), errorNumber), request);
|
|
|
+ recordLogininfor(username, loginFail, MessageUtils.message(loginType.getRetryLimitCount(), errorNumber));
|
|
|
throw new UserException(loginType.getRetryLimitCount(), errorNumber);
|
|
|
}
|
|
|
}
|