Explorar o código

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

RuoYi %!s(int64=4) %!d(string=hai) anos
pai
achega
436c2154ad

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

@@ -15,6 +15,7 @@ import com.alibaba.fastjson.JSON;
15 15
 import com.alibaba.fastjson.JSONObject;
16 16
 import com.ruoyi.common.core.utils.StringUtils;
17 17
 import com.ruoyi.common.core.web.domain.AjaxResult;
18
+import com.ruoyi.gateway.config.properties.CaptchaProperties;
18 19
 import com.ruoyi.gateway.service.ValidateCodeService;
19 20
 import reactor.core.publisher.Flux;
20 21
 import reactor.core.publisher.Mono;
@@ -32,6 +33,9 @@ public class ValidateCodeFilter extends AbstractGatewayFilterFactory<Object>
32 33
     @Autowired
33 34
     private ValidateCodeService validateCodeService;
34 35
 
36
+    @Autowired
37
+    private CaptchaProperties captchaProperties;
38
+
35 39
     private static final String CODE = "code";
36 40
 
37 41
     private static final String UUID = "uuid";
@@ -42,8 +46,8 @@ public class ValidateCodeFilter extends AbstractGatewayFilterFactory<Object>
42 46
         return (exchange, chain) -> {
43 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 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 16
 import com.ruoyi.common.core.utils.sign.Base64;
17 17
 import com.ruoyi.common.core.web.domain.AjaxResult;
18 18
 import com.ruoyi.common.redis.service.RedisService;
19
+import com.ruoyi.gateway.config.properties.CaptchaProperties;
19 20
 import com.ruoyi.gateway.service.ValidateCodeService;
20 21
 
21 22
 /**
@@ -35,8 +36,8 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
35 36
     @Autowired
36 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 45
     @Override
45 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 57
         String uuid = IdUtils.simpleUUID();
49 58
         String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
@@ -51,6 +60,7 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
51 60
         String capStr = null, code = null;
52 61
         BufferedImage image = null;
53 62
 
63
+        String captchaType = captchaProperties.getType();
54 64
         // 生成验证码
55 65
         if ("math".equals(captchaType))
56 66
         {
@@ -77,7 +87,6 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
77 87
             return AjaxResult.error(e.getMessage());
78 88
         }
79 89
 
80
-        AjaxResult ajax = AjaxResult.success();
81 90
         ajax.put("uuid", uuid);
82 91
         ajax.put("img", Base64.encode(os.toByteArray()));
83 92
         return ajax;

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

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

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 1 - 1
sql/ry_config_20210531.sql