Explorar el Código

支持配置验证码开关&类型

RuoYi hace 4 años
padre
commit
436c2154ad

+ 6 - 2
ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/ValidateCodeFilter.java

@@ -15,6 +15,7 @@ import com.alibaba.fastjson.JSON;
15
 import com.alibaba.fastjson.JSONObject;
15
 import com.alibaba.fastjson.JSONObject;
16
 import com.ruoyi.common.core.utils.StringUtils;
16
 import com.ruoyi.common.core.utils.StringUtils;
17
 import com.ruoyi.common.core.web.domain.AjaxResult;
17
 import com.ruoyi.common.core.web.domain.AjaxResult;
18
+import com.ruoyi.gateway.config.properties.CaptchaProperties;
18
 import com.ruoyi.gateway.service.ValidateCodeService;
19
 import com.ruoyi.gateway.service.ValidateCodeService;
19
 import reactor.core.publisher.Flux;
20
 import reactor.core.publisher.Flux;
20
 import reactor.core.publisher.Mono;
21
 import reactor.core.publisher.Mono;
@@ -32,6 +33,9 @@ public class ValidateCodeFilter extends AbstractGatewayFilterFactory<Object>
32
     @Autowired
33
     @Autowired
33
     private ValidateCodeService validateCodeService;
34
     private ValidateCodeService validateCodeService;
34
 
35
 
36
+    @Autowired
37
+    private CaptchaProperties captchaProperties;
38
+
35
     private static final String CODE = "code";
39
     private static final String CODE = "code";
36
 
40
 
37
     private static final String UUID = "uuid";
41
     private static final String UUID = "uuid";
@@ -42,8 +46,8 @@ public class ValidateCodeFilter extends AbstractGatewayFilterFactory<Object>
42
         return (exchange, chain) -> {
46
         return (exchange, chain) -> {
43
             ServerHttpRequest request = exchange.getRequest();
47
             ServerHttpRequest request = exchange.getRequest();
44
 
48
 
45
-            // 非登录请求,不处理
46
-            if (!StringUtils.containsIgnoreCase(request.getURI().getPath(), AUTH_URL))
49
+            // 非登录请求或验证码关闭,不处理
50
+            if (!StringUtils.containsIgnoreCase(request.getURI().getPath(), AUTH_URL) || !captchaProperties.isEnabled())
47
             {
51
             {
48
                 return chain.filter(exchange);
52
                 return chain.filter(exchange);
49
             }
53
             }

+ 12 - 3
ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/impl/ValidateCodeServiceImpl.java

@@ -16,6 +16,7 @@ import com.ruoyi.common.core.utils.StringUtils;
16
 import com.ruoyi.common.core.utils.sign.Base64;
16
 import com.ruoyi.common.core.utils.sign.Base64;
17
 import com.ruoyi.common.core.web.domain.AjaxResult;
17
 import com.ruoyi.common.core.web.domain.AjaxResult;
18
 import com.ruoyi.common.redis.service.RedisService;
18
 import com.ruoyi.common.redis.service.RedisService;
19
+import com.ruoyi.gateway.config.properties.CaptchaProperties;
19
 import com.ruoyi.gateway.service.ValidateCodeService;
20
 import com.ruoyi.gateway.service.ValidateCodeService;
20
 
21
 
21
 /**
22
 /**
@@ -35,8 +36,8 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
35
     @Autowired
36
     @Autowired
36
     private RedisService redisService;
37
     private RedisService redisService;
37
 
38
 
38
-    // 验证码类型
39
-    private String captchaType = "math";
39
+    @Autowired
40
+    private CaptchaProperties captchaProperties;
40
 
41
 
41
     /**
42
     /**
42
      * 生成验证码
43
      * 生成验证码
@@ -44,6 +45,14 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
44
     @Override
45
     @Override
45
     public AjaxResult createCapcha() throws IOException, CaptchaException
46
     public AjaxResult createCapcha() throws IOException, CaptchaException
46
     {
47
     {
48
+        AjaxResult ajax = AjaxResult.success();
49
+        boolean captchaOnOff = captchaProperties.isEnabled();
50
+        ajax.put("captchaOnOff", captchaOnOff);
51
+        if (!captchaOnOff)
52
+        {
53
+            return ajax;
54
+        }
55
+
47
         // 保存验证码信息
56
         // 保存验证码信息
48
         String uuid = IdUtils.simpleUUID();
57
         String uuid = IdUtils.simpleUUID();
49
         String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
58
         String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
@@ -51,6 +60,7 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
51
         String capStr = null, code = null;
60
         String capStr = null, code = null;
52
         BufferedImage image = null;
61
         BufferedImage image = null;
53
 
62
 
63
+        String captchaType = captchaProperties.getType();
54
         // 生成验证码
64
         // 生成验证码
55
         if ("math".equals(captchaType))
65
         if ("math".equals(captchaType))
56
         {
66
         {
@@ -77,7 +87,6 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
77
             return AjaxResult.error(e.getMessage());
87
             return AjaxResult.error(e.getMessage());
78
         }
88
         }
79
 
89
 
80
-        AjaxResult ajax = AjaxResult.success();
81
         ajax.put("uuid", uuid);
90
         ajax.put("uuid", uuid);
82
         ajax.put("img", Base64.encode(os.toByteArray()));
91
         ajax.put("img", Base64.encode(os.toByteArray()));
83
         return ajax;
92
         return ajax;

+ 10 - 4
ruoyi-ui/src/views/login.vue

@@ -18,7 +18,7 @@
18
           <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
18
           <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
19
         </el-input>
19
         </el-input>
20
       </el-form-item>
20
       </el-form-item>
21
-      <el-form-item prop="code">
21
+      <el-form-item prop="code" v-if="captchaOnOff">
22
         <el-input
22
         <el-input
23
           v-model="loginForm.code"
23
           v-model="loginForm.code"
24
           auto-complete="off"
24
           auto-complete="off"
@@ -81,6 +81,7 @@ export default {
81
         code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
81
         code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
82
       },
82
       },
83
       loading: false,
83
       loading: false,
84
+      captchaOnOff: true,
84
       redirect: undefined
85
       redirect: undefined
85
     };
86
     };
86
   },
87
   },
@@ -99,8 +100,11 @@ export default {
99
   methods: {
100
   methods: {
100
     getCode() {
101
     getCode() {
101
       getCodeImg().then(res => {
102
       getCodeImg().then(res => {
102
-        this.codeUrl = "data:image/gif;base64," + res.img;
103
-        this.loginForm.uuid = res.uuid;
103
+        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
104
+        if (this.captchaOnOff) {
105
+          this.codeUrl = "data:image/gif;base64," + res.img;
106
+          this.loginForm.uuid = res.uuid;
107
+        }
104
       });
108
       });
105
     },
109
     },
106
     getCookie() {
110
     getCookie() {
@@ -130,7 +134,9 @@ export default {
130
             this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
134
             this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
131
           }).catch(() => {
135
           }).catch(() => {
132
             this.loading = false;
136
             this.loading = false;
133
-            this.getCode();
137
+            if (this.captchaOnOff) {
138
+              this.getCode();
139
+            }
134
           });
140
           });
135
         }
141
         }
136
       });
142
       });

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 1 - 1
sql/ry_config_20210531.sql