Spring boot如何整合kaptcha並生成驗證碼
阿新 • • 發佈:2020-07-21
kaptcha是一個開源的驗證碼實現庫
1.新增依賴
<dependency> <groupId>com.github.axet</groupId> <artifactId>kaptcha</artifactId> <version>0.0.9</version> </dependency>
2.新增配置類
配置驗證碼的生成屬性
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(); // 設定邊框 properties.put("kaptcha.border","yes"); // 設定邊框顏色 properties.put("kaptcha.border.color","105,179,90"); // 設定字型顏色 properties.put("kaptcha.textproducer.font.color","black"); // 設定圖片寬度 properties.put("kaptcha.image.width","160"); // 設定圖片高度 properties.put("kaptcha.image.height","50"); //設定字型尺寸 properties.put("kaptcha.textproducer.font.size","30"); // 設定驗證碼長度 properties.put("kaptcha.textproducer.char.length","5"); // 設定字型 properties.put("kaptcha.textproducer.font.names","宋體,楷體,黑體"); Config config = new Config(properties); DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); defaultKaptcha.setConfig(config); return defaultKaptcha; } }
複製程式碼
引數:
- 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:驗證碼長度
- kaptcha.textproducer.font.names:字型
- kaptcha.textproducer.font.size:字型大小
- kaptcha.textproducer.font.color:字型顏色
- kaptcha.textproducer.char.space:文字間隔
- kaptcha.background.clear.from:背景顏色漸變,開始顏色
- kaptcha.background.clear.to:背景顏色漸變,結束顏色
3.生成驗證碼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"); // 生成文字驗證碼 String text = producer.createText(); // 生成圖片驗證碼 BufferedImage image = producer.createImage(text); // 儲存到驗證碼到 session request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY,text); ServletOutputStream out = response.getOutputStream(); ImageIO.write(image,"jpg",out); IOUtils.closeQuietly(out); } }
4.測試
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。