add 增加验证码开关

This commit is contained in:
朱杰 2021-05-25 11:03:24 +08:00 committed by 疯狂的狮子li
parent 3131b7ea9d
commit 5dc873209e
4 changed files with 34 additions and 19 deletions

View File

@ -52,6 +52,12 @@ public class CaptchaController {
*/ */
@GetMapping("/captchaImage") @GetMapping("/captchaImage")
public AjaxResult getCode() { 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 uuid = IdUtil.simpleUUID();
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid; String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
@ -90,7 +96,6 @@ public class CaptchaController {
code = captcha.getCode(); code = captcha.getCode();
} }
redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES); redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
Map<String,Object> ajax = new HashMap<>();
ajax.put("uuid", uuid); ajax.put("uuid", uuid);
ajax.put("img", captcha.getImageBase64()); ajax.put("img", captcha.getImageBase64());
return AjaxResult.success(ajax); return AjaxResult.success(ajax);

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

@ -12,6 +12,7 @@ import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MessageUtils; import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.ServletUtils; import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.ip.IpUtils; import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.framework.config.properties.CaptchaProperties;
import com.ruoyi.framework.manager.AsyncManager; import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory; import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.system.service.ISysUserService; import com.ruoyi.system.service.ISysUserService;
@ -41,7 +42,10 @@ public class SysLoginService
@Autowired @Autowired
private RedisCache redisCache; private RedisCache redisCache;
@Autowired @Autowired
private CaptchaProperties captchaProperties;
@Autowired
private ISysUserService userService; private ISysUserService userService;
/** /**
@ -55,19 +59,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

@ -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">
<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:false
}; };
}, },
watch: { watch: {
@ -99,8 +100,11 @@ export default {
methods: { methods: {
getCode() { getCode() {
getCodeImg().then(res => { getCodeImg().then(res => {
this.codeUrl = "data:image/gif;base64," + res.data.img; this.captchaEnabled = res.data.enabled;
this.loginForm.uuid = res.data.uuid; if(res.data.enabled){
this.codeUrl = "data:image/gif;base64," + res.data.img;
this.loginForm.uuid = res.data.uuid;
}
}); });
}, },
getCookie() { getCookie() {