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

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

相关推荐
lindd9119112 分钟前
4G模块应用,内网穿透,前端网页的制作第七讲(智能头盔数据上传至网页端)
前端·后端·零基础·rt-thread·实时操作系统·项目复刻
Loo国昌43 分钟前
【LangChain1.0】第八阶段:文档处理工程(LangChain篇)
人工智能·后端·算法·语言模型·架构·langchain
vx_bisheyuange1 小时前
基于SpringBoot的海鲜市场系统
java·spring boot·后端·毕业设计
李慕婉学姐1 小时前
【开题答辩过程】以《基于Spring Boot和大数据的医院挂号系统的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
大数据·spring boot·后端
源代码•宸2 小时前
Leetcode—3. 无重复字符的最长子串【中等】
经验分享·后端·算法·leetcode·面试·golang·string
0和1的舞者3 小时前
基于Spring的论坛系统-前置知识
java·后端·spring·系统·开发·知识
invicinble4 小时前
对于springboot
java·spring boot·后端
码界奇点4 小时前
基于Spring Boot与Vue的校园后台管理系统设计与实现
vue.js·spring boot·后端·毕业设计·源代码管理
爱编程的小庄4 小时前
Rust 发行版本及工具介绍
开发语言·后端·rust
Apifox.6 小时前
测试用例越堆越多?用 Apifox 测试套件让自动化回归更易维护
运维·前端·后端·测试工具·单元测试·自动化·测试用例