Spring boot如何集成kaptcha并生成驗(yàn)證碼
更新時間:2020年07月20日 16:11:38 作者:慕塵
這篇文章主要介紹了Spring boot如何集成kaptcha并生成驗(yàn)證碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
kaptcha是一個開源的驗(yàn)證碼實(shí)現(xiàn)庫
1.添加依賴
<dependency> <groupId>com.github.axet</groupId> <artifactId>kaptcha</artifactId> <version>0.0.9</version> </dependency>
2.添加配置類
配置驗(yàn)證碼的生成屬性
package com.dream.road.config; 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; @Configuration public class KaptchaConfig { @Bean public DefaultKaptcha producer() { Properties properties = new Properties(); // 設(shè)置邊框 properties.put("kaptcha.border", "yes"); // 設(shè)置邊框顏色 properties.put("kaptcha.border.color", "105,179,90"); // 設(shè)置字體顏色 properties.put("kaptcha.textproducer.font.color", "black"); // 設(shè)置圖片寬度 properties.put("kaptcha.image.width", "160"); // 設(shè)置圖片高度 properties.put("kaptcha.image.height", "50"); //設(shè)置字體尺寸 properties.put("kaptcha.textproducer.font.size", "30"); // 設(shè)置驗(yàn)證碼長度 properties.put("kaptcha.textproducer.char.length", "5"); // 設(shè)置字體 properties.put("kaptcha.textproducer.font.names", "宋體,楷體,黑體"); Config config = new Config(properties); DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); defaultKaptcha.setConfig(config); return defaultKaptcha; } }
復(fù)制代碼
參數(shù):
- kaptcha.border:圖片邊框,值:yes , no
- kaptcha.border.color:邊框顏色,值: r,g,b (and optional alpha) 或者 white,black,blue
- kaptcha.image.width:圖片寬
- kaptcha.image.height:圖片高
- kaptcha.textproducer.char.length:驗(yàn)證碼長度
- kaptcha.textproducer.font.names:字體
- kaptcha.textproducer.font.size:字體大小
- kaptcha.textproducer.font.color:字體顏色
- kaptcha.textproducer.char.space:文字間隔
- kaptcha.background.clear.from:背景顏色漸變,開始顏色
- kaptcha.background.clear.to:背景顏色漸變,結(jié)束顏色
3.生成驗(yàn)證碼api
package com.dream.road.controller; import com.google.code.kaptcha.Constants; import com.google.code.kaptcha.Producer; import org.apache.tomcat.util.http.fileupload.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.awt.image.BufferedImage; import java.io.IOException; @RestController public class LoginController { @Autowired private Producer producer; @GetMapping("captcha.jpg") public void captcha(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException { response.setHeader("Cache-Control", "no-store, no-cache"); response.setContentType("image/jpeg"); // 生成文字驗(yàn)證碼 String text = producer.createText(); // 生成圖片驗(yàn)證碼 BufferedImage image = producer.createImage(text); // 保存到驗(yàn)證碼到 session request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, text); ServletOutputStream out = response.getOutputStream(); ImageIO.write(image, "jpg", out); IOUtils.closeQuietly(out); } }
4.測試
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- SpringBoot實(shí)現(xiàn)前端驗(yàn)證碼圖片生成和校驗(yàn)
- Spring Boot 驗(yàn)證碼的生成和驗(yàn)證詳解
- springboot控制層圖片驗(yàn)證碼生成
- springboot驗(yàn)證碼的生成與驗(yàn)證的兩種方法
- SpringBoot使用Captcha生成驗(yàn)證碼
- SpringBoot實(shí)現(xiàn)Thymeleaf驗(yàn)證碼生成
- SpringBoot 圖形驗(yàn)證碼的生成和校驗(yàn)
- springboot整合kaptcha生成驗(yàn)證碼功能
- springboot驗(yàn)證碼生成以及驗(yàn)證功能舉例詳解
相關(guān)文章
Spring Security中successHandler無效問題及解決
這篇文章主要介紹了Spring Security中successHandler無效問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08springboot整合@Retryable實(shí)現(xiàn)重試功能的示例代碼
本文主要介紹了springboot整合@Retryable實(shí)現(xiàn)重試功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05hadoop的hdfs文件操作實(shí)現(xiàn)上傳文件到hdfs
這篇文章主要介紹了使用hadoop的API對HDFS上的文件訪問,其中包括上傳文件到HDFS上、從HDFS上下載文件和刪除HDFS上的文件,需要的朋友可以參考下2014-03-03SpringBoot Web詳解靜態(tài)資源規(guī)則與定制化處理
這篇文章主要介紹了SpringBoot web場景的靜態(tài)資源規(guī)則與定制化,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06Java實(shí)現(xiàn)特定范圍的完數(shù)輸出算法示例
這篇文章主要介紹了Java實(shí)現(xiàn)特定范圍的完數(shù)輸出算法,簡單說明了完數(shù)的概念、計(jì)算原理并結(jié)合實(shí)例形式分析了java針對給定范圍內(nèi)的完數(shù)輸出操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-12-12