Springboot整合hutool驗(yàn)證碼的實(shí)例代碼
在 Spring Boot 中,你可以將 Hutool 生成驗(yàn)證碼的功能集成到 RESTful API 接口中。
依賴
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.14</version> <!-- 使用最新版本 -->
</dependency>創(chuàng)建驗(yàn)證碼
package com.base.controller;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.captcha.generator.RandomGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@RestController
@RequestMapping("/api/captcha")
@Api(tags = "驗(yàn)證碼")
public class CaptchaController {
@GetMapping("/image")
@ApiOperation("線段干擾的驗(yàn)證碼")
public void getCaptchaImage(HttpServletResponse response) throws IOException {
//定義圖形驗(yàn)證碼的長和寬
LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100);
System.out.println("驗(yàn)證碼:"+captcha.getCode());
// 設(shè)置響應(yīng)類型為圖片
response.setContentType("image/png");
// 將驗(yàn)證碼圖片寫入響應(yīng)
captcha.write(response.getOutputStream());
}
@GetMapping("/image2")
@ApiOperation("圓圈干擾驗(yàn)證碼")
public void getCaptchaImage2(HttpServletResponse response) throws IOException {
// 創(chuàng)建驗(yàn)證碼對象
//定義圖形驗(yàn)證碼的長、寬、驗(yàn)證碼字符數(shù)、干擾元素個(gè)數(shù)
CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
System.out.println("驗(yàn)證碼:"+captcha.getCode());
// 設(shè)置響應(yīng)類型為圖片
response.setContentType("image/png");
// 將驗(yàn)證碼圖片寫入響應(yīng)
captcha.write(response.getOutputStream());
}
@GetMapping("/image3")
@ApiOperation("扭曲干擾驗(yàn)證碼")
public void getCaptchaImage3(HttpServletResponse response) throws IOException {
//定義圖形驗(yàn)證碼的長、寬、驗(yàn)證碼字符數(shù)、干擾線寬度
ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
System.out.println("驗(yàn)證碼:"+captcha.getCode());
// 設(shè)置響應(yīng)類型為圖片
response.setContentType("image/png");
// 將驗(yàn)證碼圖片寫入響應(yīng)
captcha.write(response.getOutputStream());
}
@GetMapping("/image4")
@ApiOperation("自定義純數(shù)字的驗(yàn)證碼")
public void getCaptchaImage4(HttpServletResponse response) throws IOException {
//定義圖形驗(yàn)證碼的長、寬、驗(yàn)證碼字符數(shù)、干擾線寬度
// 自定義純數(shù)字的驗(yàn)證碼(隨機(jī)4位數(shù)字,可重復(fù))
RandomGenerator randomGenerator = new RandomGenerator("0123456789", 4);
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
lineCaptcha.setGenerator(randomGenerator);
// 重新生成code
lineCaptcha.createCode();
// 設(shè)置響應(yīng)類型為圖片
response.setContentType("image/png");
// 將驗(yàn)證碼圖片寫入響應(yīng)
lineCaptcha.write(response.getOutputStream());
}
@GetMapping("/image5")
@ApiOperation("加減乘除的驗(yàn)證碼")
public void getCaptchaImage5(HttpServletResponse response) throws IOException {
ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 45, 4, 4);
// 自定義驗(yàn)證碼內(nèi)容為四則運(yùn)算方式
captcha.setGenerator(new MathGenerator(1));
// 重新生成code
captcha.createCode();
MathGenerator mathGenerator = new MathGenerator();
// 用戶輸入校驗(yàn)
System.out.println("驗(yàn)證結(jié)果:"+mathGenerator.verify(captcha.getCode(), "1"));
// 設(shè)置響應(yīng)類型為圖片
response.setContentType("image/png");
// 將驗(yàn)證碼圖片寫入響應(yīng)
captcha.write(response.getOutputStream());
}
}訪問驗(yàn)證碼接口
上面提到的5種樣式,效果如下:





參考資料:文檔
到此這篇關(guān)于Springboot整合hutool驗(yàn)證碼的文章就介紹到這了,更多相關(guān)Springboot hutool驗(yàn)證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot參數(shù)校驗(yàn)之@Valid的使用詳解
這篇文章主要通過示例為大家詳細(xì)介紹一下介紹了SpringBoot參數(shù)校驗(yàn)中@Valid的使用方法,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-06-06
完美解決gson將Integer默認(rèn)轉(zhuǎn)換成Double的問題
下面小編就為大家?guī)硪黄昝澜鉀Qgson將Integer默認(rèn)轉(zhuǎn)換成Double的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
SpringBoot項(xiàng)目中使用EasyPOI方式導(dǎo)出合同Word文檔的代碼實(shí)現(xiàn)
本文介紹了如何使用EasyPOI庫在Java中導(dǎo)出Word合同文檔,通過模板驅(qū)動的方式,可以靈活地將動態(tài)數(shù)據(jù)填充到預(yù)設(shè)的Word模板中,生成格式規(guī)范的合同文件,重點(diǎn)講解了導(dǎo)入依賴、模板指令、制作模板和代碼實(shí)現(xiàn)等步驟,需要的朋友可以參考下2026-01-01
Spring中配置ContextLoaderListener方式
這篇文章主要介紹了Spring中配置ContextLoaderListener方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
spring?boot2升級spring?boot3的整體步驟流程
從Spring Boot2到Spring Boot3的升級是一次全面而深刻的變革,它不僅帶來了技術(shù)棧的更新和新特性的引入,還顯著提升了應(yīng)用的性能和開發(fā)效率,這篇文章主要給大家介紹了關(guān)于spring?boot2升級spring?boot3的整體步驟,需要的朋友可以參考下2025-08-08

