添加验证码开关

This commit is contained in:
liwenhui 2021-05-30 00:54:30 +08:00
parent 67feb9947e
commit 16d9989e2f
3 changed files with 266 additions and 256 deletions

View File

@ -4,6 +4,7 @@ import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBuffer;
@ -36,6 +37,9 @@ public class ValidateCodeFilter extends AbstractGatewayFilterFactory<Object>
private static final String UUID = "uuid"; private static final String UUID = "uuid";
@Value("${ruoyi.captchaEnabled}")
private boolean captchaEnabled;
@Override @Override
public GatewayFilter apply(Object config) public GatewayFilter apply(Object config)
{ {
@ -48,18 +52,17 @@ public class ValidateCodeFilter extends AbstractGatewayFilterFactory<Object>
return chain.filter(exchange); return chain.filter(exchange);
} }
try if (captchaEnabled) {
{ try {
String rspStr = resolveBodyFromRequest(request); String rspStr = resolveBodyFromRequest(request);
JSONObject obj = JSONObject.parseObject(rspStr); JSONObject obj = JSONObject.parseObject(rspStr);
validateCodeService.checkCapcha(obj.getString(CODE), obj.getString(UUID)); validateCodeService.checkCapcha(obj.getString(CODE), obj.getString(UUID));
} } catch (Exception e) {
catch (Exception e) ServerHttpResponse response = exchange.getResponse();
{ response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
ServerHttpResponse response = exchange.getResponse(); return exchange.getResponse().writeWith(
response.getHeaders().add("Content-Type", "application/json;charset=UTF-8"); Mono.just(response.bufferFactory().wrap(JSON.toJSONBytes(AjaxResult.error(e.getMessage())))));
return exchange.getResponse().writeWith( }
Mono.just(response.bufferFactory().wrap(JSON.toJSONBytes(AjaxResult.error(e.getMessage())))));
} }
return chain.filter(exchange); return chain.filter(exchange);
}; };

View File

@ -37,5 +37,9 @@ module.exports = {
* The default is only used in the production env * The default is only used in the production env
* If you want to also use it in dev, you can pass ['production', 'development'] * If you want to also use it in dev, you can pass ['production', 'development']
*/ */
errorLog: 'production' errorLog: 'production',
/**
* 是否显示验证码
*/
captchaEnabled:false
} }

View File

@ -18,7 +18,7 @@
<svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
</el-input> </el-input>
</el-form-item> </el-form-item>
<el-form-item prop="code"> <el-form-item prop="code" v-if="this.captchaEnabled">
<el-input <el-input
v-model="loginForm.code" v-model="loginForm.code"
auto-complete="off" auto-complete="off"
@ -81,7 +81,8 @@ export default {
code: [{ required: true, trigger: "change", message: "验证码不能为空" }] code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
}, },
loading: false, loading: false,
redirect: undefined redirect: undefined,
captchaEnabled: this.$store.state.settings.captchaEnabled
}; };
}, },
watch: { watch: {
@ -93,7 +94,9 @@ export default {
} }
}, },
created() { created() {
this.getCode(); if (this.captchaEnabled){
this.getCode();
}
this.getCookie(); this.getCookie();
}, },
methods: { methods: {