add 新增验证码开关(需配置application和env文件分别控制前端和后端验证码)

This commit is contained in:
朱杰 2021-05-24 15:16:14 +08:00
parent a272ea07a9
commit 8764a91bf7
5 changed files with 45 additions and 21 deletions

View File

@ -14,6 +14,8 @@ ruoyi:
addressEnabled: false addressEnabled: false
captcha: captcha:
# 验证码开关
enabled: true
# 验证码类型 math 数组计算 char 字符验证 # 验证码类型 math 数组计算 char 字符验证
type: math type: math
# line 线段干扰 circle 圆圈干扰 shear 扭曲干扰 # line 线段干扰 circle 圆圈干扰 shear 扭曲干扰

View File

@ -13,12 +13,24 @@ import org.springframework.stereotype.Component;
@Component @Component
@ConfigurationProperties(prefix = "captcha") @ConfigurationProperties(prefix = "captcha")
public class CaptchaProperties { public class CaptchaProperties {
// 验证码类型 /**
* 验证码类型
*/
private String type; private String type;
// 验证码类别 /**
* 验证码类别
*/
private String category; private String category;
// 数字验证码位数 /**
* 数字验证码位数
*/
private Integer numberLength; private Integer numberLength;
// 字符验证码长度 /**
* 字符验证码长度
*/
private Integer charLength; private Integer charLength;
/**
* 验证码开关
*/
private Boolean enabled;
} }

View File

@ -1,6 +1,8 @@
package com.ruoyi.framework.web.service; package com.ruoyi.framework.web.service;
import javax.annotation.Resource; import javax.annotation.Resource;
import com.ruoyi.framework.config.properties.CaptchaProperties;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.BadCredentialsException;
@ -20,7 +22,7 @@ import com.ruoyi.framework.manager.factory.AsyncFactory;
/** /**
* 登录校验方法 * 登录校验方法
* *
* @author ruoyi * @author ruoyi
*/ */
@Component @Component
@ -35,9 +37,12 @@ public class SysLoginService
@Autowired @Autowired
private RedisCache redisCache; private RedisCache redisCache;
@Autowired
private CaptchaProperties captchaProperties;
/** /**
* 登录验证 * 登录验证
* *
* @param username 用户名 * @param username 用户名
* @param password 密码 * @param password 密码
* @param code 验证码 * @param code 验证码
@ -46,19 +51,19 @@ public class SysLoginService
*/ */
public String login(String username, String password, String code, String uuid) public String login(String username, String password, String code, String uuid)
{ {
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid; if(captchaProperties.getEnabled()) {
String captcha = redisCache.getCacheObject(verifyKey); String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
redisCache.deleteObject(verifyKey); String captcha = redisCache.getCacheObject(verifyKey);
if (captcha == null) redisCache.deleteObject(verifyKey);
{ if (captcha == null) {
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"))); AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
throw new CaptchaExpireException(); throw new CaptchaExpireException();
} }
if (!code.equalsIgnoreCase(captcha)) if (!code.equalsIgnoreCase(captcha)) {
{ AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"))); throw new CaptchaException();
throw new CaptchaException(); }
} }
// 用户验证 // 用户验证
Authentication authentication = null; Authentication authentication = null;
try try

View File

@ -6,3 +6,6 @@ VUE_APP_BASE_API = '/dev-api'
# 路由懒加载 # 路由懒加载
VUE_CLI_BABEL_TRANSPILE_MODULES = true VUE_CLI_BABEL_TRANSPILE_MODULES = true
#验证码开关
VUE_APP_CAPTCHA_ENABLED = true

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="captchaEnabled==='true'">
<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: process.env.VUE_APP_CAPTCHA_ENABLED
}; };
}, },
watch: { watch: {
@ -98,6 +99,7 @@ export default {
}, },
methods: { methods: {
getCode() { getCode() {
if(this.captchaEnabled==='true')
getCodeImg().then(res => { getCodeImg().then(res => {
this.codeUrl = "data:image/gif;base64," + res.data.img; this.codeUrl = "data:image/gif;base64," + res.data.img;
this.loginForm.uuid = res.data.uuid; this.loginForm.uuid = res.data.uuid;