springboot整合kaptcha生成驗證碼功能
介紹:kaptcha 是谷歌開源的非常實用的驗證碼生成工具
一、導入jar包
<!-- kaptcha驗證碼 --> <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
二、編寫kaptcha配置類
package com.zym.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 defaultKaptcha(){ DefaultKaptcha kaptcha = new DefaultKaptcha(); Properties properties = new Properties(); //邊框 properties.setProperty("kaptcha.border", "no"); //字體顏色 properties.setProperty("kaptcha.textproducer.font.color", "green"); //圖片寬度 properties.setProperty("kaptcha.image.width", "120"); //圖片高度 properties.setProperty("kaptcha.image.height", "30"); //字體大小 properties.setProperty("kaptcha.textproducer.font.size", "20"); //session key properties.setProperty("kaptcha.session.key", "kaptcha"); //驗證碼長度 properties.setProperty("kaptcha.textproducer.char.length", "4"); //字體 properties.setProperty("kaptcha.textproducer.font.names", "宋體"); //文字間隔 properties.setProperty("kaptcha.textproducer.char.space", "10"); //噪點實現(xiàn)類 properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise"); //圖片樣式-陰影 properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.ShadowGimpy"); Config config = new Config(properties); kaptcha.setConfig(config); return kaptcha; } }
三、編寫接口
package com.zym.controller; import com.google.code.kaptcha.impl.DefaultKaptcha; 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.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; @RestController public class KaptchaController { @Autowired private DefaultKaptcha defaultKaptcha; @GetMapping("/kaptcha") public void getKaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException { //設置響應頭 response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "no-cache"); response.setContentType("image/jpeg"); String text = defaultKaptcha.createText(); HttpSession session = request.getSession(); //將驗證碼存入session session.setAttribute("code", text); //創(chuàng)建驗證碼圖片 BufferedImage image = defaultKaptcha.createImage(text); ServletOutputStream os = response.getOutputStream(); ImageIO.write(image, "jpg", os); IOUtils.closeQuietly(os); } }
四、測試接口
使用PostMan:
輸入url:localhost:8080/kaptcha
成功得到驗證碼圖片,大功告成!
到此這篇關(guān)于springboot整合kaptcha生成驗證碼功能的文章就介紹到這了,更多相關(guān)springboot整合kaptcha驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用MyBatis查詢千萬級數(shù)據(jù)量操作實現(xiàn)
這篇文章主要為大家介紹了如何使用MyBatis?查詢千萬數(shù)據(jù)量的操作過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05詳解spring boot整合JMS(ActiveMQ實現(xiàn))
本篇文章主要介紹了詳解spring boot整合JMS(ActiveMQ實現(xiàn)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10Spring Boot 實現(xiàn)https ssl免密登錄(X.509 pki登錄)
這篇文章主要介紹了Spring Boot 實現(xiàn)https ssl免密登錄(X.509 pki登錄),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01