Ver código fonte

!116 优化注册方法
Merge pull request !116 from MichelleChung/2.X

疯狂的狮子Li 2 anos atrás
pai
commit
ab03ad969a

+ 17 - 0
ruoyi-api/ruoyi-api-system/src/main/java/org/dromara/system/api/RemoteConfigService.java

@@ -0,0 +1,17 @@
+package org.dromara.system.api;
+
+/**
+ * 配置服务
+ *
+ * @author Michelle.Chung
+ */
+public interface RemoteConfigService {
+
+    /**
+     * 获取注册开关
+     * @param tenantId 租户id
+     * @return true开启,false关闭
+     */
+    boolean selectRegisterEnabled(String tenantId);
+
+}

+ 8 - 0
ruoyi-api/ruoyi-api-system/src/main/java/org/dromara/system/api/RemoteUserService.java

@@ -57,6 +57,14 @@ public interface RemoteUserService {
      */
     XcxLoginUser getUserInfoByOpenid(String openid) throws UserException;
 
+    /**
+     * 校验用户名称是否唯一
+     *
+     * @param remoteUserBo 用户信息
+     * @return 结果
+     */
+    boolean checkUserNameUnique(RemoteUserBo remoteUserBo);
+
     /**
      * 注册用户信息
      *

+ 6 - 0
ruoyi-auth/src/main/java/org/dromara/auth/controller/TokenController.java

@@ -26,6 +26,7 @@ import org.dromara.common.social.config.properties.SocialProperties;
 import org.dromara.common.social.utils.SocialUtils;
 import org.dromara.common.tenant.helper.TenantHelper;
 import org.dromara.system.api.RemoteClientService;
+import org.dromara.system.api.RemoteConfigService;
 import org.dromara.system.api.RemoteSocialService;
 import org.dromara.system.api.RemoteTenantService;
 import org.dromara.system.api.domain.vo.RemoteClientVo;
@@ -52,6 +53,8 @@ public class TokenController {
     private final SocialProperties socialProperties;
     private final SysLoginService sysLoginService;
 
+    @DubboReference
+    private final RemoteConfigService remoteConfigService;
     @DubboReference
     private final RemoteTenantService remoteTenantService;
     @DubboReference
@@ -142,6 +145,9 @@ public class TokenController {
      */
     @PostMapping("register")
     public R<Void> register(@RequestBody RegisterBody registerBody) {
+        if (!remoteConfigService.selectRegisterEnabled(registerBody.getTenantId())) {
+            return R.fail("当前系统没有开启注册功能!");
+        }
         // 用户注册
         sysLoginService.register(registerBody);
         return R.ok();

+ 36 - 0
ruoyi-auth/src/main/java/org/dromara/auth/service/SysLoginService.java

@@ -10,6 +10,7 @@ import lombok.extern.slf4j.Slf4j;
 import me.zhyd.oauth.model.AuthUser;
 import org.apache.dubbo.config.annotation.DubboReference;
 import org.dromara.auth.form.RegisterBody;
+import org.dromara.auth.properties.CaptchaProperties;
 import org.dromara.auth.properties.UserPasswordProperties;
 import org.dromara.common.core.constant.Constants;
 import org.dromara.common.core.constant.GlobalConstants;
@@ -17,6 +18,8 @@ import org.dromara.common.core.constant.TenantConstants;
 import org.dromara.common.core.enums.LoginType;
 import org.dromara.common.core.enums.TenantStatus;
 import org.dromara.common.core.enums.UserType;
+import org.dromara.common.core.exception.CaptchaException;
+import org.dromara.common.core.exception.user.CaptchaExpireException;
 import org.dromara.common.core.exception.user.UserException;
 import org.dromara.common.core.utils.MessageUtils;
 import org.dromara.common.core.utils.ServletUtils;
@@ -61,6 +64,8 @@ public class SysLoginService {
 
     @Autowired
     private UserPasswordProperties userPasswordProperties;
+    @Autowired
+    private final CaptchaProperties captchaProperties;
 
     /**
      * 绑定第三方用户
@@ -119,12 +124,22 @@ public class SysLoginService {
         // 校验用户类型是否存在
         String userType = UserType.getUserType(registerBody.getUserType()).getUserType();
 
+        boolean captchaEnabled = captchaProperties.getEnabled();
+        // 验证码开关
+        if (captchaEnabled) {
+            validateCaptcha(tenantId, username, registerBody.getCode(), registerBody.getUuid());
+        }
+
         // 注册用户信息
         RemoteUserBo remoteUserBo = new RemoteUserBo();
         remoteUserBo.setUserName(username);
         remoteUserBo.setNickName(username);
         remoteUserBo.setPassword(BCrypt.hashpw(password));
         remoteUserBo.setUserType(userType);
+        // 校验用户名是否唯一
+        if (!remoteUserService.checkUserNameUnique(remoteUserBo)) {
+            throw new UserException("user.register.save.error", username);
+        }
         boolean regFlag = remoteUserService.registerUserInfo(remoteUserBo);
         if (!regFlag) {
             throw new UserException("user.register.error");
@@ -132,6 +147,27 @@ public class SysLoginService {
         recordLogininfor(tenantId, username, Constants.REGISTER, MessageUtils.message("user.register.success"));
     }
 
+    /**
+     * 校验验证码
+     *
+     * @param username 用户名
+     * @param code     验证码
+     * @param uuid     唯一标识
+     */
+    public void validateCaptcha(String tenantId, String username, String code, String uuid) {
+        String verifyKey = GlobalConstants.CAPTCHA_CODE_KEY + StringUtils.defaultString(uuid, "");
+        String captcha = RedisUtils.getCacheObject(verifyKey);
+        RedisUtils.deleteObject(verifyKey);
+        if (captcha == null) {
+            recordLogininfor(tenantId, username, Constants.REGISTER, MessageUtils.message("user.jcaptcha.expire"));
+            throw new CaptchaExpireException();
+        }
+        if (!code.equalsIgnoreCase(captcha)) {
+            recordLogininfor(tenantId, username, Constants.REGISTER, MessageUtils.message("user.jcaptcha.error"));
+            throw new CaptchaException();
+        }
+    }
+
     /**
      * 记录登录信息
      *

+ 29 - 0
ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/dubbo/RemoteConfigServiceImpl.java

@@ -0,0 +1,29 @@
+package org.dromara.system.dubbo;
+
+import lombok.RequiredArgsConstructor;
+import org.apache.dubbo.config.annotation.DubboService;
+import org.dromara.system.api.RemoteConfigService;
+import org.dromara.system.service.ISysConfigService;
+import org.springframework.stereotype.Service;
+
+/**
+ * 配置服务
+ *
+ * @author Michelle.Chung
+ */
+@RequiredArgsConstructor
+@Service
+@DubboService
+public class RemoteConfigServiceImpl implements RemoteConfigService {
+
+    private final ISysConfigService configService;
+
+    /**
+     * 获取注册开关
+     */
+    @Override
+    public boolean selectRegisterEnabled(String tenantId) {
+        return configService.selectRegisterEnabled(tenantId);
+    }
+
+}

+ 5 - 0
ruoyi-modules/ruoyi-system/src/main/java/org/dromara/system/dubbo/RemoteUserServiceImpl.java

@@ -143,6 +143,11 @@ public class RemoteUserServiceImpl implements RemoteUserService {
         return loginUser;
     }
 
+    @Override
+    public boolean checkUserNameUnique(RemoteUserBo remoteUserBo) {
+        return userService.checkUserNameUnique(MapstructUtils.convert(remoteUserBo, SysUserBo.class));
+    }
+
     @Override
     public Boolean registerUserInfo(RemoteUserBo remoteUserBo) throws UserException, ServiceException {
         SysUserBo sysUserBo = MapstructUtils.convert(remoteUserBo, SysUserBo.class);