|
@@ -4,6 +4,10 @@ import cn.hutool.core.lang.Assert;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
import com.citu.framework.common.enums.UserTypeEnum;
|
|
|
+import com.citu.framework.common.util.monitor.TracerUtils;
|
|
|
+import com.citu.framework.common.util.servlet.ServletUtils;
|
|
|
+import com.citu.framework.common.util.validation.ValidationUtils;
|
|
|
+import com.citu.module.menduner.system.controller.app.auth.vo.AppMdeAuthLoginReqVO;
|
|
|
import com.citu.module.menduner.system.controller.app.auth.vo.AppMdeAuthLoginRespVO;
|
|
|
import com.citu.module.menduner.system.controller.app.auth.vo.enterprise.AppEnterpriseAuthLoginReqVO;
|
|
|
import com.citu.module.menduner.system.controller.app.auth.vo.enterprise.AppEnterpriseAuthSmsLoginReqVO;
|
|
@@ -12,16 +16,31 @@ import com.citu.module.menduner.system.dal.dataobject.enterprise.EnterpriseUserB
|
|
|
import com.citu.module.menduner.system.dal.dataobject.user.MdeUserDO;
|
|
|
import com.citu.module.menduner.system.enums.MendunerStatusEnum;
|
|
|
import com.citu.module.menduner.system.service.enterprise.bind.EnterpriseUserBindService;
|
|
|
+import com.citu.module.menduner.system.service.user.MdeUserService;
|
|
|
+import com.citu.module.system.api.logger.LoginLogApi;
|
|
|
+import com.citu.module.system.api.logger.dto.LoginLogCreateReqDTO;
|
|
|
+import com.citu.module.system.api.oauth2.OAuth2TokenApi;
|
|
|
import com.citu.module.system.api.oauth2.dto.OAuth2AccessTokenCreateReqDTO;
|
|
|
import com.citu.module.system.api.oauth2.dto.OAuth2AccessTokenRespDTO;
|
|
|
+import com.citu.module.system.api.sms.SmsCodeApi;
|
|
|
+import com.citu.module.system.api.social.SocialClientApi;
|
|
|
+import com.citu.module.system.api.social.SocialUserApi;
|
|
|
import com.citu.module.system.api.social.dto.SocialUserBindReqDTO;
|
|
|
import com.citu.module.system.enums.logger.LoginLogTypeEnum;
|
|
|
import com.citu.module.system.enums.logger.LoginResultEnum;
|
|
|
import com.citu.module.system.enums.oauth2.OAuth2ClientConstants;
|
|
|
+import com.google.common.annotations.VisibleForTesting;
|
|
|
+import com.xingyuv.captcha.model.common.ResponseModel;
|
|
|
+import com.xingyuv.captcha.model.vo.CaptchaVO;
|
|
|
+import com.xingyuv.captcha.service.CaptchaService;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import javax.validation.Validator;
|
|
|
+
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
import static com.citu.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
import static com.citu.framework.common.util.servlet.ServletUtils.getClientIP;
|
|
@@ -35,12 +54,49 @@ import static com.citu.module.menduner.system.enums.ErrorCodeConstants.*;
|
|
|
**/
|
|
|
@Service
|
|
|
@Validated
|
|
|
-public class MdeEnterpriseAuthServiceImpl extends MdeAuthServiceImpl implements MdeEnterpriseAuthService {
|
|
|
+public class MdeEnterpriseAuthServiceImpl implements MdeEnterpriseAuthService {
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
private EnterpriseUserBindService bindService;
|
|
|
+ @Resource
|
|
|
+ protected MdeUserService userService;
|
|
|
+ @Resource
|
|
|
+ protected LoginLogApi loginLogApi;
|
|
|
+ @Resource
|
|
|
+ protected SocialUserApi socialUserApi;
|
|
|
+ @Resource
|
|
|
+ protected OAuth2TokenApi oauth2TokenApi;
|
|
|
|
|
|
+ /**
|
|
|
+ * 验证码的开关,默认为 true
|
|
|
+ */
|
|
|
+ @Value("${citu.captcha.enable:true}")
|
|
|
+ protected Boolean captchaEnable;
|
|
|
+ @Resource
|
|
|
+ protected Validator validator;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ protected CaptchaService captchaService;
|
|
|
+
|
|
|
+ @VisibleForTesting
|
|
|
+ protected void validateCaptcha(AppMdeAuthLoginReqVO reqVO) {
|
|
|
+ // 如果验证码关闭,则不进行校验
|
|
|
+ if (!captchaEnable) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 校验验证码
|
|
|
+ ValidationUtils.validate(validator, reqVO, AppMdeAuthLoginReqVO.CodeEnableGroup.class);
|
|
|
+ CaptchaVO captchaVO = new CaptchaVO();
|
|
|
+ captchaVO.setCaptchaVerification(reqVO.getCaptchaVerification());
|
|
|
+ ResponseModel response = captchaService.verification(captchaVO);
|
|
|
+ // 验证不通过
|
|
|
+ if (!response.isSuccess()) {
|
|
|
+ // 创建登录失败日志(验证码不正确)
|
|
|
+ createLoginLog(null, reqVO.getPhone(), LoginLogTypeEnum.LOGIN_USERNAME, LoginResultEnum.CAPTCHA_CODE_ERROR);
|
|
|
+ throw exception(MDE_AUTH_LOGIN_CAPTCHA_CODE_ERROR, response.getRepMsg());
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
@DSTransactional
|
|
@@ -84,7 +140,6 @@ public class MdeEnterpriseAuthServiceImpl extends MdeAuthServiceImpl implements
|
|
|
}
|
|
|
|
|
|
|
|
|
- @Override
|
|
|
protected MdeUserDO check(String mobile, String password) {
|
|
|
final LoginLogTypeEnum logTypeEnum = LoginLogTypeEnum.LOGIN_MOBILE;
|
|
|
// 校验账号是否存在
|
|
@@ -111,12 +166,12 @@ public class MdeEnterpriseAuthServiceImpl extends MdeAuthServiceImpl implements
|
|
|
return user;
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
+
|
|
|
protected UserTypeEnum getUserType() {
|
|
|
return UserTypeEnum.ADMIN;
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
+
|
|
|
protected AppMdeAuthLoginRespVO createTokenAfterLoginSuccess(MdeUserDO user, String phone, LoginLogTypeEnum logType, String openid) {
|
|
|
// 插入登陆日志
|
|
|
createLoginLog(user.getId(), phone, logType, LoginResultEnum.SUCCESS);
|
|
@@ -127,4 +182,22 @@ public class MdeEnterpriseAuthServiceImpl extends MdeAuthServiceImpl implements
|
|
|
// 构建返回结果
|
|
|
return MdeAuthConvert.INSTANCE.convert(accessTokenRespDTO, openid);
|
|
|
}
|
|
|
+
|
|
|
+ protected void createLoginLog(Long userId, String mobile, LoginLogTypeEnum logType, LoginResultEnum loginResult) {
|
|
|
+ // 插入登录日志
|
|
|
+ LoginLogCreateReqDTO reqDTO = new LoginLogCreateReqDTO();
|
|
|
+ reqDTO.setLogType(logType.getType());
|
|
|
+ reqDTO.setTraceId(TracerUtils.getTraceId());
|
|
|
+ reqDTO.setUserId(userId);
|
|
|
+ reqDTO.setUserType(getUserType().getValue());
|
|
|
+ reqDTO.setUsername(mobile);
|
|
|
+ reqDTO.setUserAgent(ServletUtils.getUserAgent());
|
|
|
+ reqDTO.setUserIp(getClientIP());
|
|
|
+ reqDTO.setResult(loginResult.getResult());
|
|
|
+ loginLogApi.createLoginLog(reqDTO);
|
|
|
+ // 更新最后登录时间
|
|
|
+ if (userId != null && Objects.equals(LoginResultEnum.SUCCESS.getResult(), loginResult.getResult())) {
|
|
|
+ userService.updateUserLogin(userId, getClientIP());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|