使用google.kaptcha來生成圖片驗證碼的實現(xiàn)方法
1.導(dǎo)入依賴
<dependency> <groupId>com.google.code</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
圖片展示如下,具體jar包自己下載

jar包下載完可以使用就很完美,如果導(dǎo)入之后報錯不能使用,則手動添加jar,進入jar所在的位置,然后執(zhí)行下面命令
mvn install:install-file -DgroupId=com.google.code -DartifactId=kaptcha -Dversion=0.0.9 -Dfile=kaptcha-0.0.9.jar -Dpackaging=jar -DgeneratePom=true
然后開始寫代碼:
需要一個類,可以定義到entity中,看你心情吧,你開心就好,圖片如下

代碼如下:
@Configuration
public class KaptchaConfig {
@Bean
public DefaultKaptcha producer() {
Properties properties = new Properties();
properties.put("kaptcha.border", "no");
properties.put("kaptcha.textproducer.font.color", "black");
properties.put("kaptcha.textproducer.char.space", "5");
Config config = new Config(properties);
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
然后是controll代碼,也很簡單,直接上代碼,如下:
//圖片驗證碼
@RequestMapping("/captcha.jpg")
public void captcha(HttpServletResponse response)throws IOException {
response.setHeader("Cache-Control", "no-store, no-cache");
response.setContentType("image/jpeg");
//生成文字驗證碼
String text = producer.createText();
producer.createText();
//生成圖片驗證碼
BufferedImage image = producer.createImage(text);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(image, "jpg", out);
}
最后完成之后驗證碼是數(shù)字字母的組合,效果圖如下所示:

總結(jié)
以上所述是小編給大家介紹的使用google.kaptcha來生成圖片驗證碼的實現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
MyBatis 中 ${}和 #{}的正確使用方法(千萬不要亂用)
這篇文章主要介紹了MyBatis 中 ${}和 #{}的正確使用方法,本文給大家提到了MyBatis 中 ${}和 #{}的區(qū)別,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
Java 數(shù)據(jù)結(jié)構(gòu)之刪除鏈表中重復(fù)的結(jié)點
在一個排序的鏈表中,會存在重復(fù)的結(jié)點,如何實現(xiàn)刪除該鏈表中重復(fù)的結(jié)點,重復(fù)的結(jié)點不保留,并返回鏈表頭指針呢?接下來小編將帶你詳細介紹2021-12-12

