add 新增验证码开关,由captcha.enabled控制

This commit is contained in:
朱杰 2021-05-25 11:03:24 +08:00
parent a272ea07a9
commit 0fe794cfcf
5 changed files with 53 additions and 25 deletions

View File

@ -52,6 +52,12 @@ public class CaptchaController {
*/
@GetMapping("/captchaImage")
public AjaxResult getCode() {
Map<String, Object> ajax = new HashMap<>();
Boolean Enabled = captchaProperties.getEnabled();
ajax.put("enabled", Enabled);
if (!Enabled) {
return AjaxResult.success(ajax);
}
// 保存验证码信息
String uuid = IdUtil.simpleUUID();
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
@ -90,7 +96,6 @@ public class CaptchaController {
code = captcha.getCode();
}
redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
Map<String,Object> ajax = new HashMap<>();
ajax.put("uuid", uuid);
ajax.put("img", captcha.getImageBase64());
return AjaxResult.success(ajax);

View File

@ -14,6 +14,8 @@ ruoyi:
addressEnabled: false
captcha:
# 验证码开关
enabled: true
# 验证码类型 math 数组计算 char 字符验证
type: math
# line 线段干扰 circle 圆圈干扰 shear 扭曲干扰
@ -26,7 +28,7 @@ captcha:
# 开发环境配置
server:
# 服务器的HTTP端口默认为8080
port: 8080
port: 8082
servlet:
# 应用的访问路径
context-path: /

View File

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

View File

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

View File

@ -18,7 +18,7 @@
<svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
</el-input>
</el-form-item>
<el-form-item prop="code">
<el-form-item prop="code" v-if="captchaEnabled">
<el-input
v-model="loginForm.code"
auto-complete="off"
@ -81,7 +81,8 @@ export default {
code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
},
loading: false,
redirect: undefined
redirect: undefined,
captchaEnabled:false
};
},
watch: {
@ -99,8 +100,11 @@ export default {
methods: {
getCode() {
getCodeImg().then(res => {
this.codeUrl = "data:image/gif;base64," + res.data.img;
this.loginForm.uuid = res.data.uuid;
this.captchaEnabled = res.data.enabled;
if(res.data.enabled){
this.codeUrl = "data:image/gif;base64," + res.data.img;
this.loginForm.uuid = res.data.uuid;
}
});
},
getCookie() {