|
@@ -1,18 +1,14 @@
|
|
|
package com.citu.module.menduner.system.service.auth;
|
|
|
|
|
|
-import cn.hutool.core.lang.Assert;
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
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.framework.security.core.LoginUser;
|
|
|
-import com.citu.module.menduner.system.controller.app.jobhunt.auth.enterprise.AppEnterpriseAuthLoginReqVO;
|
|
|
-import com.citu.module.menduner.system.controller.app.jobhunt.auth.enterprise.AppEnterpriseAuthSmsLoginReqVO;
|
|
|
+import com.citu.module.menduner.common.util.LoginUserContext;
|
|
|
import com.citu.module.menduner.system.controller.app.jobhunt.auth.enterprise.AppEnterpriseAuthSwitchLoginReqVO;
|
|
|
-import com.citu.module.menduner.system.controller.app.jobhunt.auth.vo.AppMdeAuthLoginReqVO;
|
|
|
import com.citu.module.menduner.system.controller.app.jobhunt.auth.vo.AppMdeAuthLoginRespVO;
|
|
|
import com.citu.module.menduner.system.convert.MdeAuthConvert;
|
|
|
import com.citu.module.menduner.system.dal.dataobject.enterprise.EnterpriseUserBindDO;
|
|
@@ -20,33 +16,23 @@ 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.menduner.common.util.LoginUserContext;
|
|
|
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.OAuth2AccessTokenCheckRespDTO;
|
|
|
import com.citu.module.system.api.oauth2.dto.OAuth2AccessTokenCreateReqDTO;
|
|
|
import com.citu.module.system.api.oauth2.dto.OAuth2AccessTokenRespDTO;
|
|
|
-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;
|
|
|
-import static com.citu.framework.web.core.util.WebFrameworkUtils.getTerminal;
|
|
|
import static com.citu.module.menduner.system.enums.ErrorCodeConstants.*;
|
|
|
|
|
|
/**
|
|
@@ -64,109 +50,25 @@ public class MdeEnterpriseAuthServiceImpl implements MdeEnterpriseAuthService {
|
|
|
@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;
|
|
|
@Resource
|
|
|
private EnterpriseUserBindService userBindService;
|
|
|
|
|
|
- @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
|
|
|
- public AppMdeAuthLoginRespVO login(AppEnterpriseAuthLoginReqVO reqVO) {
|
|
|
- // 校验验证码
|
|
|
- validateCaptcha(reqVO);
|
|
|
- // 使用手机 + 密码,进行登录。
|
|
|
- MdeUserDO user = check(reqVO.getPhone(), reqVO.getPassword());
|
|
|
- EnterpriseUserBindDO bindUser = check2(reqVO.getEnterpriseId(), user.getId(), reqVO.getPhone(),
|
|
|
- LoginLogTypeEnum.LOGIN_MOBILE);
|
|
|
-
|
|
|
- // 如果 socialType 非空,说明需要绑定社交用户
|
|
|
- String openid = null;
|
|
|
- if (null != reqVO.getSocialType()) {
|
|
|
- openid = socialUserApi.bindSocialUser(new SocialUserBindReqDTO(user.getId(), getUserType().getValue(),
|
|
|
- reqVO.getSocialType(), reqVO.getSocialCode(), reqVO.getSocialState())).getCheckedData();
|
|
|
- }
|
|
|
-
|
|
|
- // 创建 Token 令牌,记录登录日志
|
|
|
- return createTokenAfterLoginSuccess(
|
|
|
- bindUser,
|
|
|
- user,
|
|
|
- reqVO.getPhone(),
|
|
|
- LoginLogTypeEnum.LOGIN_MOBILE,
|
|
|
- openid,
|
|
|
- reqVO.getEnterpriseId()
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- @DSTransactional
|
|
|
- public AppMdeAuthLoginRespVO smsLogin(AppEnterpriseAuthSmsLoginReqVO reqVO) {
|
|
|
- // 校验验证码
|
|
|
- String userIp = getClientIP();
|
|
|
-// smsCodeApi.useSmsCode(MdeAuthConvert.INSTANCE.convert(reqVO, SmsSceneEnum.MENDUNER_LOGIN.getScene(), userIp).setphone(reqVO.getPhone())).getCheckedData();
|
|
|
-
|
|
|
- // 获得获得注册用户
|
|
|
- MdeUserDO user = userService.createUserIfAbsent(reqVO.getPhone(), userIp, getTerminal().toString());
|
|
|
- Assert.notNull(user, "获取用户失败,结果为空");
|
|
|
- EnterpriseUserBindDO bindUser = check2(reqVO.getEnterpriseId(), user.getId(),
|
|
|
- null, LoginLogTypeEnum.LOGIN_SMS);
|
|
|
-
|
|
|
-
|
|
|
- // 如果 socialType 非空,说明需要绑定社交用户
|
|
|
- String openid = null;
|
|
|
- if (null != reqVO.getSocialType()) {
|
|
|
- openid = socialUserApi.bindSocialUser(new SocialUserBindReqDTO(user.getId(), getUserType().getValue(),
|
|
|
- reqVO.getSocialType(), reqVO.getSocialCode(), reqVO.getSocialState())).getCheckedData();
|
|
|
- }
|
|
|
-
|
|
|
- // 创建 Token 令牌,记录登录日志
|
|
|
- return createTokenAfterLoginSuccess(bindUser, user, reqVO.getPhone(),
|
|
|
- LoginLogTypeEnum.LOGIN_SMS, openid, reqVO.getEnterpriseId());
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
@DSTransactional
|
|
|
public AppMdeAuthLoginRespVO switchLogin(AppEnterpriseAuthSwitchLoginReqVO reqVO) {
|
|
|
LoginUser loginUser = LoginUserContext.get();
|
|
|
// 查询用户
|
|
|
MdeUserDO user = userService.getMdeUser(loginUser.getId());
|
|
|
- EnterpriseUserBindDO bindUser = check2(reqVO.getEnterpriseId(), user.getId(), user.getPhone(),
|
|
|
- LoginLogTypeEnum.LOGIN_MOBILE);
|
|
|
+ EnterpriseUserBindDO bindUser = check(reqVO.getEnterpriseId(), user.getId(), user.getPhone(),
|
|
|
+ LoginLogTypeEnum.LOGIN_SWITCH);
|
|
|
|
|
|
// 创建 Token 令牌,记录登录日志
|
|
|
AppMdeAuthLoginRespVO resp = createTokenAfterLoginSuccess(
|
|
|
bindUser,
|
|
|
user,
|
|
|
user.getPhone(),
|
|
|
- LoginLogTypeEnum.LOGIN_MOBILE,
|
|
|
+ LoginLogTypeEnum.LOGIN_SWITCH,
|
|
|
null,
|
|
|
reqVO.getEnterpriseId()
|
|
|
);
|
|
@@ -174,26 +76,10 @@ public class MdeEnterpriseAuthServiceImpl implements MdeEnterpriseAuthService {
|
|
|
return resp;
|
|
|
}
|
|
|
|
|
|
- protected MdeUserDO check(String phone, String password) {
|
|
|
- final LoginLogTypeEnum logTypeEnum = LoginLogTypeEnum.LOGIN_MOBILE;
|
|
|
- // 校验账号是否存在
|
|
|
- MdeUserDO user = userService.getUserByPhone(phone);
|
|
|
- if (null == user) {
|
|
|
- createLoginLog(null, phone, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
|
|
|
- throw exception(MDE_AUTH_LOGIN_BAD_CREDENTIALS);
|
|
|
- }
|
|
|
- if (!userService.isPasswordMatch(password, user.getPassword())) {
|
|
|
- createLoginLog(user.getId(), phone, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
|
|
|
- throw exception(MDE_AUTH_LOGIN_BAD_CREDENTIALS);
|
|
|
- }
|
|
|
-
|
|
|
- return user;
|
|
|
- }
|
|
|
-
|
|
|
- protected EnterpriseUserBindDO check2(Long enterpriseId,
|
|
|
- Long userId,
|
|
|
- String phone,
|
|
|
- LoginLogTypeEnum logTypeEnum) {
|
|
|
+ protected EnterpriseUserBindDO check(Long enterpriseId,
|
|
|
+ Long userId,
|
|
|
+ String phone,
|
|
|
+ LoginLogTypeEnum logTypeEnum) {
|
|
|
// 查询绑定用户
|
|
|
EnterpriseUserBindDO bind = userBindService.selectByEnterpriseIdAndUserId(
|
|
|
enterpriseId,
|