Springboot通過谷歌Kaptcha?組件生成圖形驗(yàn)證碼功能
圖形驗(yàn)證碼屬于老生常談了,具體細(xì)節(jié)這里就不說了。生成圖形驗(yàn)證碼的辦法非常多,今天講解一種通過Kaptcha組件快速生成圖形驗(yàn)證碼的方法。
Kaptcha是谷歌開源的一款簡(jiǎn)單實(shí)用的圖形驗(yàn)證碼組件。我個(gè)人推薦它的最大原因是容易上手,采用約定大于配置的方式,快速契合到項(xiàng)目中。
話不多說,我們看看該如何使用它:
一、首先我們?cè)趕pringboot中引入以下maven組件:
<dependency>
<groupId>com.google.code.kaptcha</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3</version>
</dependency>如果上述組件你一直無法拉取下來的話,也可以用如下配置:
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>二、接著我們?cè)趕pringboot項(xiàng)目中加入對(duì)應(yīng)的config配置類,(防盜連接:本文首發(fā)自http://www.cnblogs.com/jilodream/ )這一步也可以配合配置中心來完成。它的作用是自動(dòng)生成我們所需的config bean。
其中的配置項(xiàng)我們都可以選填,這里是只是一個(gè)參考,具體內(nèi)容可見下文表
@Component
public class KaptchaConfig {
@Bean
public DefaultKaptcha getDefaultKaptcha(){
com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
Properties properties = new Properties();
properties.put("kaptcha.border", "no");
properties.put("kaptcha.textproducer.font.color", "red");
properties.put("kaptcha.image.width", "213");
properties.put("kaptcha.image.height", "88");
properties.put("kaptcha.textproducer.font.size", "45");
properties.put("kaptcha.session.key", "verifyCode");
properties.put("kaptcha.textproducer.char.space", "6");
properties.put("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.WaterRipple");
// properties.put("kaptcha.textproducer.font.names", "宋體,楷體,微軟雅黑");
properties.put("kaptcha.background.clear.from", "yellow");
properties.put("kaptcha.background.clear.to", "green");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}配置表
| 配置名 | 配置作用 | 默認(rèn)值 |
| kaptcha.border | 圖片邊框,合法值:yes , no | yes |
| kaptcha.border.color | 邊框顏色,合法值: r,g,b (and optional alpha) 或者 white,black,blue. | black |
| kaptcha.image.width | 圖片寬 | 200 |
| kaptcha.image.height | 圖片高 | 50 |
| kaptcha.producer.impl | 圖片實(shí)現(xiàn)類 | com.google.code.kaptcha.impl.DefaultKaptcha |
| kaptcha.textproducer.impl | 文本實(shí)現(xiàn)類 | com.google.code.kaptcha.text.impl.DefaultTextCreator |
| kaptcha.textproducer.char.string | 文本集合,驗(yàn)證碼值從此集合中獲取 | abcde2345678gfynmnpwx |
| kaptcha.textproducer.char.length | 驗(yàn)證碼長度 | 5 |
| kaptcha.textproducer.font.names | 字體 | Arial, Courier |
| kaptcha.textproducer.font.size | 字體大小 | 40px. |
| kaptcha.textproducer.font.color | 字體顏色,合法值: r,g,b 或者 white,black,blue. | black |
| kaptcha.textproducer.char.space | 文字間隔 | 2 |
| kaptcha.noise.impl | 干擾實(shí)現(xiàn)類 | com.google.code.kaptcha.impl.DefaultNoise |
| kaptcha.noise.color | 干擾 顏色,合法值: r,g,b 或者 white,black,blue. | black |
| kaptcha.obscurificator.impl | 圖片樣式:<br />水紋 com.google.code.kaptcha.impl.WaterRipple <br /> 魚眼 com.google.code.kaptcha.impl.FishEyeGimpy <br /> 陰影 com.google.code.kaptcha.impl.ShadowGimpy | com.google.code.kaptcha.impl.WaterRipple |
| kaptcha.background.impl | 背景實(shí)現(xiàn)類 | com.google.code.kaptcha.impl.DefaultBackground |
| kaptcha.background.clear.from | 背景顏色漸變,開始顏色 | light grey |
| kaptcha.background.clear.to | 背景顏色漸變, 結(jié)束顏色 | white |
| kaptcha.word.impl | 文字渲染器 | com.google.code.kaptcha.text.impl.DefaultWordRenderer |
| kaptcha.session.key | session key | KAPTCHA_SESSION_KEY |
| kaptcha.session.date | session date | KAPTCHA_SESSION_DATE |
三、創(chuàng)建好config bean之后,我們就可以創(chuàng)建接口來生成驗(yàn)證碼了controller類,新增接口:
@GetMapping("/login/getVerifyCode")
public void getVerifyCode(String loginKey,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
try {
log.warn("query verify Code" + loginKey);
loadService.getVerifyCode(loginKey, httpServletRequest, httpServletResponse);
} catch (Exception e) {
log.error("get verify Code failed :", e);
}
}service類,新增邏輯:
public void getVerifyCode(String loginKey, HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) throws IOException {
ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream();
try {
//生產(chǎn)驗(yàn)證碼字符串并保存到session中
String verifyCode = captchaProducer.createText();
// httpServletRequest.getSession().setAttribute("verifyCode", verifyCode); // 寫入會(huì)話
//redisCache.setVerifyInfo(loginKey, verifyCode); //寫入redis
captchaMap.put(loginKey, verifyCode);//寫入內(nèi)存
log.warn("reset verify code key {}, code {}", loginKey, verifyCode);
BufferedImage challenge = captchaProducer.createImage(verifyCode);
ImageIO.write(challenge, "jpg", imgOutputStream);
} catch (IllegalArgumentException | IOException e) {
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
byte[] captchaOutputStream = imgOutputStream.toByteArray();
httpServletResponse.setHeader("Cache-Control", "no-store");
httpServletResponse.setHeader("Pragma", "no-cache");
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/jpeg");
try (ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream()) {
responseOutputStream.write(captchaOutputStream);
responseOutputStream.flush();
} catch (IOException ex) {
log.error("find ex in create a new verify Code", ex);
}
}相關(guān)代碼全部開發(fā)完畢后,我們調(diào)用接口查看效果:

這里我發(fā)現(xiàn)一個(gè)在本地環(huán)境使用ok,(防盜連接:本文首發(fā)自http://www.cnblogs.com/jilodream/ )但是在使用docker部署微服務(wù)時(shí)卻存在的一個(gè)異常:
java.lang.NullPointerException: null
at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264)
at sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:219)
at sun.awt.FontConfiguration.init(FontConfiguration.java:107)
.....網(wǎng)上查了下,大致原因是由于我們本地使用的是oraclejdk,但是在docker中jdk的版本則采用的是openjdk。openjdk有些字體的支撐不是很友好,這里需要在制作docker鏡像時(shí)添加如下語句解決:
RUN apk add --update font-adobe-100dpi ttf-dejavu fontconfig
到此這篇關(guān)于Springboot通過谷歌Kaptcha 組件生成圖形驗(yàn)證碼功能的文章就介紹到這了,更多相關(guān)Springboot圖形驗(yàn)證碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
細(xì)致解讀希爾排序算法與相關(guān)的Java代碼實(shí)現(xiàn)
這篇文章主要介紹了希爾排序算法與相關(guān)的Java代碼實(shí)現(xiàn),希爾排序的時(shí)間復(fù)雜度根據(jù)步長序列的不同而不同,需要的朋友可以參考下2016-05-05
對(duì)SpringMVC的@RequestParam的解釋
下面小編就為大家?guī)硪黄獙?duì)SpringMVC的@RequestParam的解釋。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
springboot項(xiàng)目mysql-connector-java默認(rèn)版本如何查看
這篇文章主要介紹了springboot項(xiàng)目mysql-connector-java默認(rèn)版本如何查看問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Java如何實(shí)現(xiàn)數(shù)據(jù)壓縮所有方式性能測(cè)試
本文介紹了多種壓縮算法及其在Java中的實(shí)現(xiàn),包括LZ4、BZip2、Deflate、Gzip和7z等,LZ4以其高效的壓縮和解壓縮速度而受到青睞,特別是在大數(shù)據(jù)處理場(chǎng)景中,通過對(duì)比不同壓縮算法的性能和壓縮率,我們選擇了最適合當(dāng)前項(xiàng)目需求的壓縮工具2025-02-02
springboot整合shiro之thymeleaf使用shiro標(biāo)簽的方法
Thymeleaf 是一個(gè)跟 Velocity、FreeMarker 類似的模板引擎,它可以完全替代 JSP ,這篇文章主要介紹了springboot整合shiro之thymeleaf使用shiro標(biāo)簽的相關(guān)知識(shí),需要的朋友可以參考下2021-10-10

