当前位置: 首页 > news >正文

若依改用EasyCaptcha验证码

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

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

  1. 在父模块添加依赖

    <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>
    

    image-20250416093559192

  2. ruoyi-admin模块中添加依赖

    <!-- 验证码 -->
    <dependency><groupId>com.github.whvcse</groupId><artifactId>easy-captcha</artifactId>
    </dependency>
    

    image-20250416093638335

  3. 找到CaptchaController,java,修改getCode方法

    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();  // 获取运算的公式//存入redisredisCache.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;}
    

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

相关文章:

  • UE5在场景3D物体上播放本地视频(带声音)
  • 数据挖掘案例-电力负荷预测
  • L2-052 吉利矩阵分
  • Sentinel源码—3.ProcessorSlot的执行过程一
  • 第五章 5.2ESP32物联网应用:HTTP与Web服务器详细教学
  • dfs二叉树中的深搜(回溯、剪枝)--力扣129、814、230、257
  • SpringMVC学习(请求与响应。常见参数类型接收与响应。@RequestParam、@RequestBody的使用)(详细示例)
  • 阿里云集群开启debug
  • LangChain缓存嵌入技术完全指南:CacheBackedEmbedding原理与实践(附代码示例)
  • 遵守 Vue3 的单向数据流原则:父组件传递对象 + 子组件修改对象属性,安全地实现父子组件之间复杂对象的双向绑定示例代码及讲解
  • 道路运输安全员企业负责人考试内容与范围
  • Zookeeper 可观测性最佳实践
  • 【Qt】QWidget 核⼼属性详解
  • 为啥mac日历打不开浏览器
  • 《分布式软总线赋能老旧设备高效通信》
  • 分布式光伏电站运维难?Acrel-1000DP助力安全稳定运行
  • Mybatis-Plus的使用
  • 视频压缩技术学习(不高明版本)
  • 5G时代,视频分析设备平台EasyCVR实现通信基站远程安全便捷管控
  • 修改wsl中发行版Ubuntu的主机名
  • 哪个新闻网站做的好/电脑培训学校哪家最好
  • 网站建设排行公司/沈阳seo搜索引擎
  • 网站大量死链/网络营销策划方案
  • 做绿植o2o网站怎么样/天津seo网络营销
  • 破解wordpress登录/合肥网站优化公司
  • 龙文国土局漳滨村新农村建设网站/今日国际新闻热点