若依改用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;
        }

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

相关推荐
Jagger_31 分钟前
SonarQube:提升代码质量的前后端解决方案
前端·后端·ai编程
在逃牛马43 分钟前
【Uni-App+SSM 宠物项目实战】Day6:MP 实体类与 Mapper 生成
后端
remaindertime1 小时前
(九)Spring Cloud Alibaba 2023.x:微服务接口文档统一管理与聚合
后端·spring cloud·微服务
Barcke1 小时前
📘 初识 WebFlux
spring boot·后端·spring
JohnYan1 小时前
工作笔记 - 一个浏览器环境适用的类型转换工具
javascript·后端·设计模式
得物技术1 小时前
0基础带你精通Java对象序列化--以Hessian为例|得物技术
java·后端·编程语言
Java水解2 小时前
MySQL UPDATE 语句:数据更新操作详解
后端·mysql
Java水解2 小时前
深入浅出:在 Koa 中实现优雅的中间件依赖注入
后端·koa
lssjzmn2 小时前
构建实时消息应用:Spring Boot + Vue 与 WebSocket 的有机融合
java·后端·架构
金銀銅鐵2 小时前
[Java] 浅析可重复注解(Repeatable Annotation) 是如何实现的
java·后端