From 5dc873209e5b6ec2c4289e928831fad6888cfb98 Mon Sep 17 00:00:00 2001
From: 朱杰 <693337446@qq.com>
Date: 星期二, 25 五月 2021 14:02:34 +0800
Subject: [PATCH] add 增加验证码开关

---
 ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java |   32 ++++++++++++++++++--------------
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java   |    7 ++++++-
 ruoyi-admin/src/main/resources/application.yml                                     |    2 ++
 ruoyi-ui/src/views/login.vue                                                       |   12 ++++++++----
 4 files changed, 34 insertions(+), 19 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java
index 0988656..2f303ba 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java
@@ -52,6 +52,12 @@
 	 */
 	@GetMapping("/captchaImage")
 	public AjaxResult getCode() {
+		Map<String, Object> ajax = new HashMap<>();
+		Boolean enabled = captchaProperties.getEnabled();
+		ajax.put("enabled", enabled);
+		if (!enabled) {
+			return AjaxResult.success(ajax);
+		}
 		// 淇濆瓨楠岃瘉鐮佷俊鎭�
 		String uuid = IdUtil.simpleUUID();
 		String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
@@ -90,7 +96,6 @@
 			code = captcha.getCode();
 		}
 		redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
-		Map<String,Object> ajax = new HashMap<>();
 		ajax.put("uuid", uuid);
 		ajax.put("img", captcha.getImageBase64());
 		return AjaxResult.success(ajax);
diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml
index 535da0a..3838ff5 100644
--- a/ruoyi-admin/src/main/resources/application.yml
+++ b/ruoyi-admin/src/main/resources/application.yml
@@ -14,6 +14,8 @@
   addressEnabled: false
 
 captcha:
+  # 楠岃瘉鐮佸紑鍏�
+  enabled: true
   # 楠岃瘉鐮佺被鍨� math 鏁扮粍璁$畻 char 瀛楃楠岃瘉
   type: math
   # line 绾挎骞叉壈 circle 鍦嗗湀骞叉壈 shear 鎵洸骞叉壈
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java
index ccf6940..bc22ca0 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java
@@ -12,6 +12,7 @@
 import com.ruoyi.common.utils.MessageUtils;
 import com.ruoyi.common.utils.ServletUtils;
 import com.ruoyi.common.utils.ip.IpUtils;
+import com.ruoyi.framework.config.properties.CaptchaProperties;
 import com.ruoyi.framework.manager.AsyncManager;
 import com.ruoyi.framework.manager.factory.AsyncFactory;
 import com.ruoyi.system.service.ISysUserService;
@@ -41,7 +42,10 @@
     @Autowired
     private RedisCache redisCache;
 
-    @Autowired
+	@Autowired
+	private CaptchaProperties captchaProperties;
+
+	@Autowired
     private ISysUserService userService;
 
     /**
@@ -55,19 +59,19 @@
      */
     public String login(String username, String password, String code, String uuid)
     {
-        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
-        String captcha = redisCache.getCacheObject(verifyKey);
-        redisCache.deleteObject(verifyKey);
-        if (captcha == null)
-        {
-            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
-            throw new CaptchaExpireException();
-        }
-        if (!code.equalsIgnoreCase(captcha))
-        {
-            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
-            throw new CaptchaException();
-        }
+		if(captchaProperties.getEnabled()) {
+			String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
+			String captcha = redisCache.getCacheObject(verifyKey);
+			redisCache.deleteObject(verifyKey);
+			if (captcha == null) {
+				AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
+				throw new CaptchaExpireException();
+			}
+			if (!code.equalsIgnoreCase(captcha)) {
+				AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
+				throw new CaptchaException();
+			}
+		}
         // 鐢ㄦ埛楠岃瘉
         Authentication authentication = null;
         try
diff --git a/ruoyi-ui/src/views/login.vue b/ruoyi-ui/src/views/login.vue
index d68f614..e8da56b 100644
--- a/ruoyi-ui/src/views/login.vue
+++ b/ruoyi-ui/src/views/login.vue
@@ -18,7 +18,7 @@
           <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
         </el-input>
       </el-form-item>
-      <el-form-item prop="code">
+      <el-form-item prop="code" v-if="captchaEnabled">
         <el-input
           v-model="loginForm.code"
           auto-complete="off"
@@ -81,7 +81,8 @@
         code: [{ required: true, trigger: "change", message: "楠岃瘉鐮佷笉鑳戒负绌�" }]
       },
       loading: false,
-      redirect: undefined
+      redirect: undefined,
+      captchaEnabled:false
     };
   },
   watch: {
@@ -99,8 +100,11 @@
   methods: {
     getCode() {
       getCodeImg().then(res => {
-        this.codeUrl = "data:image/gif;base64," + res.data.img;
-        this.loginForm.uuid = res.data.uuid;
+        this.captchaEnabled = res.data.enabled;
+        if(res.data.enabled){
+          this.codeUrl = "data:image/gif;base64," + res.data.img;
+          this.loginForm.uuid = res.data.uuid;
+        }
       });
     },
     getCookie() {

--
Gitblit v1.9.3