From 8503c4460271a1df48ea40a91cd2da527e401274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Wed, 21 Dec 2022 10:42:29 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=A0=81=E7=BB=93=E6=9E=9C=E4=BD=BF=E7=94=A8spel?= =?UTF-8?q?=E5=BC=95=E6=93=8E=E8=87=AA=E5=8A=A8=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/ValidateCodeServiceImpl.java | 28 ++++++------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/impl/ValidateCodeServiceImpl.java b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/impl/ValidateCodeServiceImpl.java index 10842e2f..29aa6001 100644 --- a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/impl/ValidateCodeServiceImpl.java +++ b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/impl/ValidateCodeServiceImpl.java @@ -2,7 +2,6 @@ package com.ruoyi.gateway.service.impl; import cn.hutool.captcha.AbstractCaptcha; import cn.hutool.captcha.generator.CodeGenerator; -import cn.hutool.core.convert.Convert; import cn.hutool.core.util.IdUtil; import com.ruoyi.common.core.constant.CacheConstants; import com.ruoyi.common.core.constant.Constants; @@ -17,6 +16,9 @@ import com.ruoyi.gateway.config.properties.CaptchaProperties; import com.ruoyi.gateway.enums.CaptchaType; import com.ruoyi.gateway.service.ValidateCodeService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.expression.Expression; +import org.springframework.expression.ExpressionParser; +import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.stereotype.Service; import java.io.IOException; @@ -57,30 +59,18 @@ public class ValidateCodeServiceImpl implements ValidateCodeService { AbstractCaptcha captcha = SpringUtils.getBean(captchaProperties.getCategory().getClazz()); captcha.setGenerator(codeGenerator); captcha.createCode(); - String code = isMath ? getCodeResult(captcha.getCode()) : captcha.getCode(); + String code = captcha.getCode(); + if (isMath) { + ExpressionParser parser = new SpelExpressionParser(); + Expression exp = parser.parseExpression(StringUtils.remove(code, "=")); + code = exp.getValue(String.class); + } RedisUtils.setCacheObject(verifyKey, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION)); ajax.put("uuid", uuid); ajax.put("img", captcha.getImageBase64()); return R.ok(ajax); } - private String getCodeResult(String capStr) { - int numberLength = captchaProperties.getNumberLength(); - int a = Convert.toInt(StringUtils.substring(capStr, 0, numberLength).trim()); - char operator = capStr.charAt(numberLength); - int b = Convert.toInt(StringUtils.substring(capStr, numberLength + 1, numberLength + 1 + numberLength).trim()); - switch (operator) { - case '*': - return Convert.toStr(a * b); - case '+': - return Convert.toStr(a + b); - case '-': - return Convert.toStr(a - b); - default: - return StringUtils.EMPTY; - } - } - /** * 校验验证码 */