From 8764a91bf7ca24be9608c3d96a1b3746fb38ffa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=9D=B0?= <693337446@qq.com> Date: Mon, 24 May 2021 15:16:14 +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=EF=BC=88=E9=9C=80=E9=85=8D=E7=BD=AE?= =?UTF-8?q?application=E5=92=8Cenv=E6=96=87=E4=BB=B6=E5=88=86=E5=88=AB?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E5=89=8D=E7=AB=AF=E5=92=8C=E5=90=8E=E7=AB=AF?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E7=A0=81=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application.yml | 2 ++ .../config/properties/CaptchaProperties.java | 20 ++++++++--- .../web/service/SysLoginService.java | 35 +++++++++++-------- ruoyi-ui/.env.development | 3 ++ ruoyi-ui/src/views/login.vue | 6 ++-- 5 files changed, 45 insertions(+), 21 deletions(-) diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index 535da0a9c..3838ff50e 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 扭曲干扰 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/.env.development b/ruoyi-ui/.env.development index 28b97c7e1..3ce56a300 100644 --- a/ruoyi-ui/.env.development +++ b/ruoyi-ui/.env.development @@ -6,3 +6,6 @@ VUE_APP_BASE_API = '/dev-api' # 路由懒加载 VUE_CLI_BABEL_TRANSPILE_MODULES = true + +#验证码开关 +VUE_APP_CAPTCHA_ENABLED = true diff --git a/ruoyi-ui/src/views/login.vue b/ruoyi-ui/src/views/login.vue index d68f614d9..d7a26f541 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;