java 验证码_Java - 验证码 - 由Kaptcha组件实现
生活随笔
收集整理的這篇文章主要介紹了
java 验证码_Java - 验证码 - 由Kaptcha组件实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文是基于SpringBoot整合Kaptcha驗證碼實現
Kaptcha 是一個可高度配置的實用驗證碼生成工具,在項目開發中能夠非常方便實現驗證碼
先來看一個由 Kaptcha 制作的驗證碼效果圖
快速進入如何進行配置與實現的
第1步:配置 Kaptcha 的依賴庫
com.github.penggle kaptcha 2.3.2第2步:編寫配置類
package com.dingsheng.configuration;import com.google.code.kaptcha.impl.DefaultKaptcha;import com.google.code.kaptcha.util.Config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.util.Properties;/** * 驗證碼 - Kaptcha - 插件 */@Configurationpublic class KaptchaConfig { @Bean public DefaultKaptcha getDefaultKaptcha() { DefaultKaptcha captchaProducer = new DefaultKaptcha(); Properties properties = new Properties(); properties.setProperty("kaptcha.border", "yes"); properties.setProperty("kaptcha.border.color", "105,179,90"); properties.setProperty("kaptcha.textproducer.font.color", "blue"); properties.setProperty("kaptcha.image.width", "100"); properties.setProperty("kaptcha.image.height", "38"); properties.setProperty("kaptcha.textproducer.font.size", "30"); properties.setProperty("kaptcha.session.key", "code"); properties.setProperty("kaptcha.textproducer.char.length", "4"); properties.setProperty("kaptcha.textproducer.font.names", "宋體,楷體,微軟雅黑"); Config config = new Config(properties); captchaProducer.setConfig(config); return captchaProducer; }}第3步:編寫KaptchaController
package com.dingsheng.controller;import com.google.code.kaptcha.Constants;import com.google.code.kaptcha.Producer;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import javax.imageio.ImageIO;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.awt.image.BufferedImage;import java.io.IOException;@Controllerpublic class KatchaController { @Autowired private Producer producer = null; @GetMapping("/captcha") public void getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws IOException { HttpSession session = request.getSession(); response.setDateHeader("Expires", 0); response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); // 瀏覽器和任何中繼的Web代理,都不會存儲這次相應的數據 response.addHeader("Cache-Control", "post-check=0, pre-check=0"); response.setHeader("Pragma", "no-cache"); response.setContentType("image/jpeg"); // 生成驗證碼 String capText = producer.createText(); session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText); // 輸出驗證碼 BufferedImage bi = producer.createImage(capText); ServletOutputStream out = response.getOutputStream(); ImageIO.write(bi, "jpg", out); out.flush(); out.close(); }}第4步:頁面引入驗證碼的請求地址
通過上面的4個步驟,即可以實現由 Kaptcha 制作的驗證碼
總結
以上是生活随笔為你收集整理的java 验证码_Java - 验证码 - 由Kaptcha组件实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: switch最大选项数目_随时随地学习C
- 下一篇: 手机上彩APP哪个好?预测最精确的彩票A