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





參考資料:文檔
到此這篇關(guān)于Springboot整合hutool驗證碼的文章就介紹到這了,更多相關(guān)Springboot hutool驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于java關(guān)鍵字this和super的區(qū)別和理解
這篇文章主要給大家介紹了關(guān)于java關(guān)鍵字this和super的區(qū)別和理解的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01
Java的基本數(shù)據(jù)類型和運算方法(必看篇)
下面小編就為大家?guī)硪黄狫ava的基本數(shù)據(jù)類型和運算方法(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
SpringBoot實現(xiàn)短鏈接系統(tǒng)的使用示例
由于短鏈接可能涉及到用戶隱私和安全問題,所以短鏈接系統(tǒng)也需要符合相關(guān)的數(shù)據(jù)保護和安全標準,本文主要介紹了SpringBoot實現(xiàn)短鏈接系統(tǒng)的使用示例,感興趣的可以了解一下2023-09-09
Java集合與數(shù)組區(qū)別簡介及相互轉(zhuǎn)換實例
這篇文章主要介紹了Java集合與數(shù)組區(qū)別簡介及相互轉(zhuǎn)換實例,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
java應(yīng)用開發(fā)之JVM運行時內(nèi)存分析
這篇文章主要介紹了java應(yīng)用開發(fā)之JVM運行時內(nèi)存,文中附含圖文示例內(nèi)容分析非常簡要,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09

