添加验证码开关

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

@ -1,41 +1,45 @@
module.exports = { module.exports = {
title: '若依管理系统', title: '若依管理系统',
/** /**
* 侧边栏主题 深色主题theme-dark浅色主题theme-light * 侧边栏主题 深色主题theme-dark浅色主题theme-light
*/ */
sideTheme: 'theme-dark', sideTheme: 'theme-dark',
/** /**
* 是否系统布局配置 * 是否系统布局配置
*/ */
showSettings: false, showSettings: false,
/** /**
* 是否显示顶部导航 * 是否显示顶部导航
*/ */
topNav: false, topNav: false,
/** /**
* 是否显示 tagsView * 是否显示 tagsView
*/ */
tagsView: true, tagsView: true,
/** /**
* 是否固定头部 * 是否固定头部
*/ */
fixedHeader: false, fixedHeader: false,
/** /**
* 是否显示logo * 是否显示logo
*/ */
sidebarLogo: true, sidebarLogo: true,
/** /**
* @type {string | array} 'production' | ['production', 'development'] * @type {string | array} 'production' | ['production', 'development']
* @description Need show err logs component. * @description Need show err logs component.
* 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

@ -1,203 +1,206 @@
<template> <template>
<div class="login"> <div class="login">
<el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form"> <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
<h3 class="title">若依后台管理系统</h3> <h3 class="title">若依后台管理系统</h3>
<el-form-item prop="username"> <el-form-item prop="username">
<el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号"> <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
<svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" /> <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
</el-input> </el-input>
</el-form-item> </el-form-item>
<el-form-item prop="password"> <el-form-item prop="password">
<el-input <el-input
v-model="loginForm.password" v-model="loginForm.password"
type="password" type="password"
auto-complete="off" auto-complete="off"
placeholder="密码" placeholder="密码"
@keyup.enter.native="handleLogin" @keyup.enter.native="handleLogin"
> >
<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"
placeholder="验证码" placeholder="验证码"
style="width: 63%" style="width: 63%"
@keyup.enter.native="handleLogin" @keyup.enter.native="handleLogin"
> >
<svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" /> <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
</el-input> </el-input>
<div class="login-code"> <div class="login-code">
<img :src="codeUrl" @click="getCode" class="login-code-img"/> <img :src="codeUrl" @click="getCode" class="login-code-img"/>
</div> </div>
</el-form-item> </el-form-item>
<el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox> <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
<el-form-item style="width:100%;"> <el-form-item style="width:100%;">
<el-button <el-button
:loading="loading" :loading="loading"
size="medium" size="medium"
type="primary" type="primary"
style="width:100%;" style="width:100%;"
@click.native.prevent="handleLogin" @click.native.prevent="handleLogin"
> >
<span v-if="!loading"> </span> <span v-if="!loading"> </span>
<span v-else> 中...</span> <span v-else> 中...</span>
</el-button> </el-button>
</el-form-item> </el-form-item>
</el-form> </el-form>
<!-- 底部 --> <!-- 底部 -->
<div class="el-login-footer"> <div class="el-login-footer">
<span>Copyright © 2018-2021 ruoyi.vip All Rights Reserved.</span> <span>Copyright © 2018-2021 ruoyi.vip All Rights Reserved.</span>
</div> </div>
</div> </div>
</template> </template>
<script> <script>
import { getCodeImg } from "@/api/login"; import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie"; import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt' import { encrypt, decrypt } from '@/utils/jsencrypt'
export default { export default {
name: "Login", name: "Login",
data() { data() {
return { return {
codeUrl: "", codeUrl: "",
cookiePassword: "", cookiePassword: "",
loginForm: { loginForm: {
username: "admin", username: "admin",
password: "admin123", password: "admin123",
rememberMe: false, rememberMe: false,
code: "", code: "",
uuid: "" uuid: ""
}, },
loginRules: { loginRules: {
username: [ username: [
{ required: true, trigger: "blur", message: "用户名不能为空" } { required: true, trigger: "blur", message: "用户名不能为空" }
], ],
password: [ password: [
{ required: true, trigger: "blur", message: "密码不能为空" } { required: true, trigger: "blur", message: "密码不能为空" }
], ],
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: { },
$route: { watch: {
handler: function(route) { $route: {
this.redirect = route.query && route.query.redirect; handler: function(route) {
}, this.redirect = route.query && route.query.redirect;
immediate: true },
} immediate: true
}, }
created() { },
this.getCode(); created() {
this.getCookie(); if (this.captchaEnabled){
}, this.getCode();
methods: { }
getCode() { this.getCookie();
getCodeImg().then(res => { },
this.codeUrl = "data:image/gif;base64," + res.img; methods: {
this.loginForm.uuid = res.uuid; getCode() {
}); getCodeImg().then(res => {
}, this.codeUrl = "data:image/gif;base64," + res.img;
getCookie() { this.loginForm.uuid = res.uuid;
const username = Cookies.get("username"); });
const password = Cookies.get("password"); },
const rememberMe = Cookies.get('rememberMe') getCookie() {
this.loginForm = { const username = Cookies.get("username");
username: username === undefined ? this.loginForm.username : username, const password = Cookies.get("password");
password: password === undefined ? this.loginForm.password : decrypt(password), const rememberMe = Cookies.get('rememberMe')
rememberMe: rememberMe === undefined ? false : Boolean(rememberMe) this.loginForm = {
}; username: username === undefined ? this.loginForm.username : username,
}, password: password === undefined ? this.loginForm.password : decrypt(password),
handleLogin() { rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
this.$refs.loginForm.validate(valid => { };
if (valid) { },
this.loading = true; handleLogin() {
if (this.loginForm.rememberMe) { this.$refs.loginForm.validate(valid => {
Cookies.set("username", this.loginForm.username, { expires: 30 }); if (valid) {
Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 }); this.loading = true;
Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 }); if (this.loginForm.rememberMe) {
} else { Cookies.set("username", this.loginForm.username, { expires: 30 });
Cookies.remove("username"); Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
Cookies.remove("password"); Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
Cookies.remove('rememberMe'); } else {
} Cookies.remove("username");
this.$store.dispatch("Login", this.loginForm).then(() => { Cookies.remove("password");
this.$router.push({ path: this.redirect || "/" }).catch(()=>{}); Cookies.remove('rememberMe');
}).catch(() => { }
this.loading = false; this.$store.dispatch("Login", this.loginForm).then(() => {
this.getCode(); this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
}); }).catch(() => {
} this.loading = false;
}); this.getCode();
} });
} }
}; });
</script> }
}
<style rel="stylesheet/scss" lang="scss"> };
.login { </script>
display: flex;
justify-content: center; <style rel="stylesheet/scss" lang="scss">
align-items: center; .login {
height: 100%; display: flex;
background-image: url("../assets/images/login-background.jpg"); justify-content: center;
background-size: cover; align-items: center;
} height: 100%;
.title { background-image: url("../assets/images/login-background.jpg");
margin: 0px auto 30px auto; background-size: cover;
text-align: center; }
color: #707070; .title {
} margin: 0px auto 30px auto;
text-align: center;
.login-form { color: #707070;
border-radius: 6px; }
background: #ffffff;
width: 400px; .login-form {
padding: 25px 25px 5px 25px; border-radius: 6px;
.el-input { background: #ffffff;
height: 38px; width: 400px;
input { padding: 25px 25px 5px 25px;
height: 38px; .el-input {
} height: 38px;
} input {
.input-icon { height: 38px;
height: 39px; }
width: 14px; }
margin-left: 2px; .input-icon {
} height: 39px;
} width: 14px;
.login-tip { margin-left: 2px;
font-size: 13px; }
text-align: center; }
color: #bfbfbf; .login-tip {
} font-size: 13px;
.login-code { text-align: center;
width: 33%; color: #bfbfbf;
height: 38px; }
float: right; .login-code {
img { width: 33%;
cursor: pointer; height: 38px;
vertical-align: middle; float: right;
} img {
} cursor: pointer;
.el-login-footer { vertical-align: middle;
height: 40px; }
line-height: 40px; }
position: fixed; .el-login-footer {
bottom: 0; height: 40px;
width: 100%; line-height: 40px;
text-align: center; position: fixed;
color: #fff; bottom: 0;
font-family: Arial; width: 100%;
font-size: 12px; text-align: center;
letter-spacing: 1px; color: #fff;
} font-family: Arial;
.login-code-img { font-size: 12px;
height: 38px; letter-spacing: 1px;
} }
</style> .login-code-img {
height: 38px;
}
</style>