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;