|
@@ -4,7 +4,6 @@ import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
|
|
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
|
|
import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
|
|
|
-import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
|
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
|
|
|
import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils;
|
|
|
import cn.iocoder.yudao.module.system.api.logger.dto.LoginLogCreateReqDTO;
|
|
@@ -15,7 +14,6 @@ import cn.iocoder.yudao.module.system.controller.admin.auth.vo.*;
|
|
|
import cn.iocoder.yudao.module.system.convert.auth.AuthConvert;
|
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
|
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
|
|
-import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper;
|
|
|
import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum;
|
|
|
import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum;
|
|
|
import cn.iocoder.yudao.module.system.enums.oauth2.OAuth2ClientConstants;
|
|
@@ -24,20 +22,17 @@ import cn.iocoder.yudao.module.system.service.logger.LoginLogService;
|
|
|
import cn.iocoder.yudao.module.system.service.member.MemberService;
|
|
|
import cn.iocoder.yudao.module.system.service.oauth2.OAuth2TokenService;
|
|
|
import cn.iocoder.yudao.module.system.service.social.SocialUserService;
|
|
|
-import cn.iocoder.yudao.module.system.service.tenant.TenantService;
|
|
|
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
|
|
|
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 jakarta.annotation.Resource;
|
|
|
+import jakarta.validation.Validator;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.context.annotation.Lazy;
|
|
|
-import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import jakarta.annotation.Resource;
|
|
|
-import jakarta.validation.Validator;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
@@ -69,13 +64,7 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
|
|
private CaptchaService captchaService;
|
|
|
@Resource
|
|
|
private SmsCodeApi smsCodeApi;
|
|
|
- @Resource
|
|
|
- @Lazy // 延迟,避免循环依赖报错
|
|
|
- private TenantService tenantService;
|
|
|
- @Resource
|
|
|
- private AdminUserMapper userMapper;
|
|
|
- @Resource
|
|
|
- private PasswordEncoder passwordEncoder;
|
|
|
+
|
|
|
/**
|
|
|
* 验证码的开关,默认为 true
|
|
|
*/
|
|
@@ -258,38 +247,33 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
|
|
return UserTypeEnum.ADMIN;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ @Override
|
|
|
public AuthLoginRespVO register(AuthRegisterReqVO registerReqVO) {
|
|
|
- // 校验验证码
|
|
|
- AuthLoginReqVO loginReqVO = BeanUtils.toBean(registerReqVO, AuthLoginReqVO.class);
|
|
|
- validateCaptcha(loginReqVO);
|
|
|
- // 校验账户配合
|
|
|
- tenantService.handleTenantInfo(tenant -> {
|
|
|
- long count = userMapper.selectCount();
|
|
|
- if (count >= tenant.getAccountCount()) {
|
|
|
- throw exception(USER_COUNT_MAX, tenant.getAccountCount());
|
|
|
- }
|
|
|
- });
|
|
|
- // 校验用户名是否已存在
|
|
|
- if (userMapper.selectByUsername(registerReqVO.getUsername()) != null) {
|
|
|
- throw exception(USER_USERNAME_EXISTS);
|
|
|
- }
|
|
|
- // 插入用户
|
|
|
- AdminUserDO user = BeanUtils.toBean(registerReqVO, AdminUserDO.class);
|
|
|
- user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启
|
|
|
- user.setPassword(encodePassword(registerReqVO.getPassword())); // 加密密码
|
|
|
- userMapper.insert(user);
|
|
|
+ // 1. 校验验证码
|
|
|
+ validateCaptcha(registerReqVO);
|
|
|
|
|
|
- return createTokenAfterLoginSuccess(user.getId(), registerReqVO.getUsername(), LoginLogTypeEnum.LOGIN_USERNAME);
|
|
|
+ // 2. 校验用户名是否已存在
|
|
|
+ Long userId = userService.registerUser(registerReqVO);
|
|
|
+
|
|
|
+ // 3. 创建 Token 令牌,记录登录日志
|
|
|
+ return createTokenAfterLoginSuccess(userId, registerReqVO.getUsername(), LoginLogTypeEnum.LOGIN_USERNAME);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 对密码进行加密
|
|
|
- *
|
|
|
- * @param password 密码
|
|
|
- * @return 加密后的密码
|
|
|
- */
|
|
|
- private String encodePassword(String password) {
|
|
|
- return passwordEncoder.encode(password);
|
|
|
+ @VisibleForTesting
|
|
|
+ void validateCaptcha(AuthRegisterReqVO reqVO) {
|
|
|
+ // 如果验证码关闭,则不进行校验
|
|
|
+ if (!captchaEnable) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 校验验证码
|
|
|
+ ValidationUtils.validate(validator, reqVO, AuthLoginReqVO.CodeEnableGroup.class);
|
|
|
+ CaptchaVO captchaVO = new CaptchaVO();
|
|
|
+ captchaVO.setCaptchaVerification(reqVO.getCaptchaVerification());
|
|
|
+ ResponseModel response = captchaService.verification(captchaVO);
|
|
|
+ // 验证不通过
|
|
|
+ if (!response.isSuccess()) {
|
|
|
+ throw exception(AUTH_REGISTER_CAPTCHA_CODE_ERROR, response.getRepMsg());
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
}
|