|
@@ -5,6 +5,7 @@ import cn.hutool.core.util.ObjectUtil;
|
|
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
import com.citu.framework.common.enums.TerminalEnum;
|
|
import com.citu.framework.common.enums.TerminalEnum;
|
|
import com.citu.framework.common.enums.UserTypeEnum;
|
|
import com.citu.framework.common.enums.UserTypeEnum;
|
|
|
|
+import com.citu.framework.common.pojo.CommonResult;
|
|
import com.citu.framework.common.util.monitor.TracerUtils;
|
|
import com.citu.framework.common.util.monitor.TracerUtils;
|
|
import com.citu.framework.common.util.servlet.ServletUtils;
|
|
import com.citu.framework.common.util.servlet.ServletUtils;
|
|
import com.citu.framework.common.util.validation.ValidationUtils;
|
|
import com.citu.framework.common.util.validation.ValidationUtils;
|
|
@@ -40,15 +41,20 @@ import com.xingyuv.captcha.model.vo.CaptchaVO;
|
|
import com.xingyuv.captcha.service.CaptchaService;
|
|
import com.xingyuv.captcha.service.CaptchaService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
import javax.validation.Validator;
|
|
import javax.validation.Validator;
|
|
|
|
+import java.util.Collections;
|
|
import java.util.Objects;
|
|
import java.util.Objects;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
import static com.citu.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
import static com.citu.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
import static com.citu.framework.common.util.servlet.ServletUtils.getClientIP;
|
|
import static com.citu.framework.common.util.servlet.ServletUtils.getClientIP;
|
|
import static com.citu.framework.web.core.util.WebFrameworkUtils.getTerminal;
|
|
import static com.citu.framework.web.core.util.WebFrameworkUtils.getTerminal;
|
|
|
|
+import static com.citu.module.menduner.system.dal.redis.RedisKeyConstants.MDE_AUTH_USER_PWD_LOCK;
|
|
|
|
+import static com.citu.module.menduner.system.dal.redis.RedisKeyConstants.MDE_AUTH_USER_SMS_CODE_LOCK;
|
|
import static com.citu.module.menduner.system.enums.ErrorCodeConstants.*;
|
|
import static com.citu.module.menduner.system.enums.ErrorCodeConstants.*;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -76,7 +82,6 @@ public class MdeAuthServiceImpl implements MdeAuthService {
|
|
protected MdeEnterpriseAuthService enterpriseAuthService;
|
|
protected MdeEnterpriseAuthService enterpriseAuthService;
|
|
@Resource
|
|
@Resource
|
|
protected PointOperateProducer pointOperateProducer;
|
|
protected PointOperateProducer pointOperateProducer;
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 验证码的开关,默认为 true
|
|
* 验证码的开关,默认为 true
|
|
*/
|
|
*/
|
|
@@ -84,9 +89,10 @@ public class MdeAuthServiceImpl implements MdeAuthService {
|
|
protected Boolean captchaEnable;
|
|
protected Boolean captchaEnable;
|
|
@Resource
|
|
@Resource
|
|
protected Validator validator;
|
|
protected Validator validator;
|
|
-
|
|
|
|
@Resource
|
|
@Resource
|
|
protected CaptchaService captchaService;
|
|
protected CaptchaService captchaService;
|
|
|
|
+ @Resource
|
|
|
|
+ private StringRedisTemplate redisTemplate;
|
|
|
|
|
|
@VisibleForTesting
|
|
@VisibleForTesting
|
|
protected void validateCaptcha(AppMdeAuthLoginReqVO reqVO) {
|
|
protected void validateCaptcha(AppMdeAuthLoginReqVO reqVO) {
|
|
@@ -146,8 +152,24 @@ public class MdeAuthServiceImpl implements MdeAuthService {
|
|
// 第一次登录
|
|
// 第一次登录
|
|
throw exception(MDE_USER_PHONE_INIT_PASSWORD);
|
|
throw exception(MDE_USER_PHONE_INIT_PASSWORD);
|
|
} else {
|
|
} else {
|
|
- createLoginLog(user.getId(), account, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
|
|
|
|
- throw exception(MDE_AUTH_LOGIN_BAD_CREDENTIALS);
|
|
|
|
|
|
+ // 获取错误次数
|
|
|
|
+ String num =
|
|
|
|
+ redisTemplate.opsForValue()
|
|
|
|
+ .get(String.format(MDE_AUTH_USER_PWD_LOCK, user.getPhone()));
|
|
|
|
+ Integer numInt = Integer.parseInt(null == num ? "0" : num) + 1;
|
|
|
|
+
|
|
|
|
+ redisTemplate.opsForValue()
|
|
|
|
+ .setIfAbsent(MDE_AUTH_USER_PWD_LOCK, String.valueOf(numInt), 8, TimeUnit.HOURS);
|
|
|
|
+ if (numInt >= 5) {
|
|
|
|
+ // 8个小时内输错5次 锁定
|
|
|
|
+ userService.disable(Collections.singletonList(user.getId()));
|
|
|
|
+ createLoginLog(user.getId(),account,logTypeEnum, LoginResultEnum.USER_DISABLED);
|
|
|
|
+ throw exception(MDE_AUTH_LOGIN_USER_DISABLED);
|
|
|
|
+ } else {
|
|
|
|
+ // 提示账户密码错误
|
|
|
|
+ createLoginLog(user.getId(), account, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
|
|
|
|
+ throw exception(MDE_AUTH_LOGIN_BAD_CREDENTIALS);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -189,16 +211,15 @@ public class MdeAuthServiceImpl implements MdeAuthService {
|
|
public AppMdeAuthLoginRespVO smsLogin(AppMdeAuthSmsLoginReqVO reqVO) {
|
|
public AppMdeAuthLoginRespVO smsLogin(AppMdeAuthSmsLoginReqVO reqVO) {
|
|
// 校验验证码
|
|
// 校验验证码
|
|
String userIp = getClientIP();
|
|
String userIp = getClientIP();
|
|
- smsCodeApi.useSmsCode(MdeAuthConvert.INSTANCE.convert(reqVO, SmsSceneEnum.MENDUNER_LOGIN.getScene(), userIp)
|
|
|
|
- .setMobile(reqVO.getPhone())).getCheckedData();
|
|
|
|
-
|
|
|
|
|
|
+ CommonResult<Boolean> result =
|
|
|
|
+ smsCodeApi.useSmsCode(MdeAuthConvert.INSTANCE.convert(reqVO, SmsSceneEnum.MENDUNER_LOGIN.getScene(), userIp)
|
|
|
|
+ .setMobile(reqVO.getPhone()));
|
|
|
|
|
|
// 获得获得注册用户
|
|
// 获得获得注册用户
|
|
MdeUserDO user = null;
|
|
MdeUserDO user = null;
|
|
if (!reqVO.isAutoRegister()) {
|
|
if (!reqVO.isAutoRegister()) {
|
|
user = userService.getUserByPhone(reqVO.getPhone());
|
|
user = userService.getUserByPhone(reqVO.getPhone());
|
|
if (null == user) {
|
|
if (null == user) {
|
|
- createLoginLog(null, reqVO.getPhone(), LoginLogTypeEnum.LOGIN_MOBILE, LoginResultEnum.BAD_CREDENTIALS);
|
|
|
|
throw exception(MDE_USER_MOBILE_NOT_EXISTS);
|
|
throw exception(MDE_USER_MOBILE_NOT_EXISTS);
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
@@ -206,13 +227,42 @@ public class MdeAuthServiceImpl implements MdeAuthService {
|
|
user = userService.createUserIfAbsent(reqVO.getPhone(), userIp, getTerminal().toString());
|
|
user = userService.createUserIfAbsent(reqVO.getPhone(), userIp, getTerminal().toString());
|
|
Assert.notNull(user, "获取用户失败,结果为空");
|
|
Assert.notNull(user, "获取用户失败,结果为空");
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ if (result.isError()) {
|
|
|
|
+ // 获取错误次数
|
|
|
|
+ String num =
|
|
|
|
+ redisTemplate.opsForValue()
|
|
|
|
+ .get(String.format(MDE_AUTH_USER_SMS_CODE_LOCK, reqVO.getPhone()));
|
|
|
|
+ Integer numInt = Integer.parseInt(null == num ? "0" : num) + 1;
|
|
|
|
+
|
|
|
|
+ redisTemplate.opsForValue()
|
|
|
|
+ .setIfAbsent(MDE_AUTH_USER_SMS_CODE_LOCK, String.valueOf(numInt), 8, TimeUnit.HOURS);
|
|
|
|
+
|
|
|
|
+ if (numInt >= 5) {
|
|
|
|
+ // 8个小时内输错5次 锁定
|
|
|
|
+ userService.disable(Collections.singletonList(user.getId()));
|
|
|
|
+ createLoginLog(user.getId(), user.getPhone(), LoginLogTypeEnum.LOGIN_SMS, LoginResultEnum.USER_DISABLED);
|
|
|
|
+ throw exception(MDE_AUTH_LOGIN_USER_DISABLED);
|
|
|
|
+ } else {
|
|
|
|
+ // 提示验证码错误
|
|
|
|
+ result.getCheckedData();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 是否禁用
|
|
|
|
+ if (ObjectUtil.notEqual(user.getStatus(), MendunerStatusEnum.ENABLE.getStatus())) {
|
|
|
|
+ createLoginLog(user.getId(), user.getPhone(), LoginLogTypeEnum.LOGIN_SMS, LoginResultEnum.USER_DISABLED);
|
|
|
|
+ throw exception(MDE_AUTH_LOGIN_USER_DISABLED);
|
|
|
|
+ }
|
|
|
|
+
|
|
// 如果 socialType 非空,说明需要绑定社交用户
|
|
// 如果 socialType 非空,说明需要绑定社交用户
|
|
String openid = null;
|
|
String openid = null;
|
|
if (reqVO.getSocialType() != null) {
|
|
if (reqVO.getSocialType() != null) {
|
|
openid = socialUserApi.bindSocialUser(new SocialUserBindReqDTO(user.getId(), getUserType().getValue(),
|
|
openid = socialUserApi.bindSocialUser(new SocialUserBindReqDTO(user.getId(), getUserType().getValue(),
|
|
reqVO.getSocialType(), reqVO.getSocialCode(), reqVO.getSocialState())).getCheckedData();
|
|
reqVO.getSocialType(), reqVO.getSocialCode(), reqVO.getSocialState())).getCheckedData();
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ // 登录成功就清理
|
|
|
|
+ redisTemplate.delete(MDE_AUTH_USER_SMS_CODE_LOCK);
|
|
// 创建 Token 令牌,记录登录日志
|
|
// 创建 Token 令牌,记录登录日志
|
|
return createTokenAfterLoginSuccess(user, reqVO.getPhone(), LoginLogTypeEnum.LOGIN_SMS, openid);
|
|
return createTokenAfterLoginSuccess(user, reqVO.getPhone(), LoginLogTypeEnum.LOGIN_SMS, openid);
|
|
}
|
|
}
|