日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java 验证码_Java - 验证码 - 由Kaptcha组件实现

發布時間:2023/12/15 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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组件实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。