若依改用EasyCaptcha验证码

若依自带的验证码样式比较单一,所以想改用EasyCaptcha验证码,另外EasyCaptcha算术验证码可能会有负数,输入时需要写负号,比较麻烦,所以使用一个简单的方法过滤掉负数结果

原本的验证码依赖和代码可删可不删,注释掉即可

  1. 在父模块添加依赖

    xml 复制代码
    <properties>
    	<captcha.version>1.6.2</captcha.version>
    </properties>
    
    <dependencyManagement>
    	 <dependencies>
    <!-- EasyCaptcha验证码依赖  -->
    		<dependency>
           			 <groupId>com.github.whvcse</groupId>
          		           <artifactId>easy-captcha</artifactId>
            		 <version>${captcha.version}</version>
    		</dependency>
    	</dependencies>
    </dependencyManagement>
  2. ruoyi-admin模块中添加依赖

    xml 复制代码
    <!-- 验证码 -->
    <dependency>
        <groupId>com.github.whvcse</groupId>
        <artifactId>easy-captcha</artifactId>
    </dependency>
  3. 找到CaptchaController,java,修改getCode方法

    java 复制代码
    public AjaxResult getCode(HttpServletResponse response) throws IOException, FontFormatException {
            AjaxResult ajax = AjaxResult.success();
            boolean captchaEnabled = configService.selectCaptchaEnabled();
            ajax.put("captchaEnabled", captchaEnabled);
            if (!captchaEnabled)
            {
                return ajax;
            }
    
            // 保存验证码信息
            String uuid = IdUtils.simpleUUID();
            String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
         
            //新验证码
            //图形验证码
            //SpecCaptcha captcha = new SpecCaptcha(130, 48, 4);
            //算术验证码
            ArithmeticCaptcha captcha;
            String code;
    
            captcha = new ArithmeticCaptcha(130, 48);
            //得到验证码的值
            code = captcha.text();
            //若为负数则重新生成
            int i = Integer.parseInt(code);
            while (i < 0){
                System.out.println("code = " + code + ",负数,重新生成!!!!!!!!");
                captcha = new ArithmeticCaptcha(130, 48);
                code = captcha.text();
                i = Integer.parseInt(code);
                System.out.println("i = " + i + ",新值!!!");
            }
            // 设置内置字体
            captcha.setFont(Captcha.FONT_1);
            //captcha.setLen(2);  // 几位数运算,默认是两位
            //captcha.getArithmeticString();  // 获取运算的公式
            //存入redis
            redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
    
           //旧验证码
            //String capStr = null, code = null;
            //BufferedImage image = null;
            //
            //// 生成验证码
            //String captchaType = RuoYiConfig.getCaptchaType();
            //if ("math".equals(captchaType))
            //{
            //    String capText = captchaProducerMath.createText();
            //    capStr = capText.substring(0, capText.lastIndexOf("@"));
            //    code = capText.substring(capText.lastIndexOf("@") + 1);
            //    image = captchaProducerMath.createImage(capStr);
            //}
            //else if ("char".equals(captchaType))
            //{
            //    capStr = code = captchaProducer.createText();
            //    image = captchaProducer.createImage(capStr);
            //}
            //
            //redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
            //// 转换流信息写出
            //FastByteArrayOutputStream os = new FastByteArrayOutputStream();
            //try
            //{
            //    ImageIO.write(image, "jpg", os);
            //}
            //catch (IOException e)
            //{
            //    return AjaxResult.error(e.getMessage());
            //}
    
            ajax.put("uuid", uuid);
            ajax.put("img", captcha.toBase64());
            return ajax;
        }

另外,欢迎大家来我的博客------火柴人儿的小站来玩~

相关推荐
陵易居士2 分钟前
Spring如何解决项目中的循环依赖问题?
java·后端·spring
Aska_Lv15 分钟前
RocketMQ---core原理
后端
AronTing20 分钟前
10-Spring Cloud Alibaba 之 Dubbo 深度剖析与实战
后端·面试·架构
没逻辑24 分钟前
⏰ Redis 在支付系统中作为延迟任务队列的实践
redis·后端
雷渊26 分钟前
如何保证数据库和Es的数据一致性?
java·后端·面试
fjkxyl27 分钟前
Spring的启动流程
java·后端·spring
掘金酱28 分钟前
😊 酱酱宝的推荐:做任务赢积分“拿”华为MatePad Air、雷蛇机械键盘、 热门APP会员卡...
前端·后端·trae
总之就是非常可爱1 小时前
🚀 使用 ReadableStream 优雅地处理 SSE(Server-Sent Events)
前端·javascript·后端
夜寒花碎1 小时前
GO入门——Hello, World
后端·go
爱的叹息1 小时前
关于 Spring Boot 微服务解决方案的对比,并以 Spring Cloud Alibaba 为例,详细说明其核心组件的使用方式、配置及代码示例
spring boot·后端·微服务