Hutool是一個小而全的Java工具類庫,它提供了很多實用的工具類,包括但不限于日期處理、加密解密、文件操作、反射操作、HTTP客戶端等。
核心工具類:CaptchaUtil,CaptchaUtil 是 Hutool 提供的一個工具類,用于創建各種類型的驗證碼。它提供了靜態工廠方法,幫助我們快速生成不同風格的驗證碼。
POM依賴
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.3</version>
</dependency>
CaptchaUtil
package cn.hutool.captcha;/*** 圖形驗證碼工具*/
public class CaptchaUtil {/*** 創建線干擾的驗證碼,默認5位驗證碼,150條干擾線* @param width 圖片寬* @param height 圖片高*/public static LineCaptcha createLineCaptcha(int width, int height) {return new LineCaptcha(width, height);}/*** 創建線干擾的驗證碼* @param width 圖片寬* @param height 圖片高* @param codeCount 字符個數* @param lineCount 干擾線條數*/public static LineCaptcha createLineCaptcha(int width, int height, int codeCount, int lineCount) {return new LineCaptcha(width, height, codeCount, lineCount);}/*** 創建圓圈干擾的驗證碼,默認5位驗證碼,15個干擾圈* @param width 圖片寬* @param height 圖片高*/public static CircleCaptcha createCircleCaptcha(int width, int height) {return new CircleCaptcha(width, height);}/*** 創建圓圈干擾的驗證碼* @param width 圖片寬* @param height 圖片高* @param codeCount 字符個數* @param circleCount 干擾圓圈條數*/public static CircleCaptcha createCircleCaptcha(int width, int height, int codeCount, int circleCount) {return new CircleCaptcha(width, height, codeCount, circleCount);}/*** 創建扭曲干擾的驗證碼,默認5位驗證碼* @param width 圖片寬* @param height 圖片高*/public static ShearCaptcha createShearCaptcha(int width, int height) {return new ShearCaptcha(width, height);}/*** 創建扭曲干擾的驗證碼,默認5位驗證碼* @param width 圖片寬* @param height 圖片高* @param codeCount 字符個數* @param thickness 干擾線寬度*/public static ShearCaptcha createShearCaptcha(int width, int height, int codeCount, int thickness) {return new ShearCaptcha(width, height, codeCount, thickness);}/*** 創建GIF驗證碼* @param width 寬* @param height 高*/public static GifCaptcha createGifCaptcha(int width, int height) {return new GifCaptcha(width, height);}/*** 創建GIF驗證碼* @param width 寬* @param height 高* @param codeCount 字符個數*/public static GifCaptcha createGifCaptcha(int width, int height, int codeCount) {return new GifCaptcha(width, height, codeCount);}
}
前端頁面
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>登錄</title>
</head>
<body><form action="/login" method="post">用戶名:<input type="text" name="username"><br>密碼:<input type="password" name="password"><br>驗證碼:<input type="text" name="verificationCode"><img src="/verification-code/getImage"><input type="submit" value="登錄"></form>
</body>
</html>
后端代碼
@Controller
@RequestMapping("/verification-code")
@Slf4j
public class VerificationCodeController {@GetMapping("/getImage")public void generateCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {// 1. 生成驗證碼圖片// 圖片寬:90 圖片高:30 字符個數:4 干擾線寬度:3ICaptcha captcha = CaptchaUtil.createShearCaptcha(90, 30, 4, 3);// 2. 將生成的驗證碼(文本)保存到 Redis / Session 中// 存入Redis / Session后,在后續可通過攔截器進行校驗request.getSession().setAttribute("verificationCode", captcha.getCode());// 3. 設置響應類型為 JPEG 圖片response.setContentType("image/jpeg");// 4. 回寫captcha.write(response.getOutputStream());}
}
驗證碼效果