From 0fe794cfcfa20273160525fc196f430b6d7c2a71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=9D=B0?= <693337446@qq.com> Date: Tue, 25 May 2021 11:03:24 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E6=96=B0=E5=A2=9E=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E7=A0=81=E5=BC=80=E5=85=B3,=E7=94=B1captcha.enabled=E6=8E=A7?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/common/CaptchaController.java | 7 +++- .../src/main/resources/application.yml | 4 ++- .../config/properties/CaptchaProperties.java | 20 ++++++++--- .../web/service/SysLoginService.java | 35 +++++++++++-------- ruoyi-ui/src/views/login.vue | 12 ++++--- 5 files changed, 53 insertions(+), 25 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java index 098865607..442dc6d53 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java @@ -52,6 +52,12 @@ public class CaptchaController { */ @GetMapping("/captchaImage") public AjaxResult getCode() { + Map 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 ajax = new HashMap<>(); ajax.put("uuid", uuid); ajax.put("img", captcha.getImageBase64()); return AjaxResult.success(ajax); diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index 535da0a9c..a6719dc2e 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -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: / diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/CaptchaProperties.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/CaptchaProperties.java index 24356bca6..905578b9d 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/CaptchaProperties.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/CaptchaProperties.java @@ -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; } diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java index b56b43705..5803ee55e 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/SysLoginService.java @@ -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 diff --git a/ruoyi-ui/src/views/login.vue b/ruoyi-ui/src/views/login.vue index d68f614d9..e8da56b7e 100644 --- a/ruoyi-ui/src/views/login.vue +++ b/ruoyi-ui/src/views/login.vue @@ -18,7 +18,7 @@ - + { - 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() {