SpringBoot使用Kaptcha實現(xiàn)驗證碼的生成與驗證功能
當(dāng)我們在項目中登錄使用驗證碼的時候,不妨試試Kaptcha生成驗證碼,非常簡單
1、首先,我們在pom.xml文件中引入kaptcha的maven依賴
<!-- kaptcha驗證碼 --> <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
2、然后,我們編寫kaptcha的配置類:KaptchaConfig.java
package com.lzzy.meet.common.kaptcha;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.util.Properties;
/**
* @ClassName KaptchaConfig
* kaptcha配置類
* @Author
* @Date 2019-09-05 13:50:50
* @Version 1.0
**/
@Slf4j
@Component
public class KaptchaConfig {
@Bean
public DefaultKaptcha getKaptcheCode() {
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
Properties properties = new Properties();
properties.setProperty("kaptcha.border", "no");
properties.setProperty("kaptcha.textproducer.font.color", "black");
properties.setProperty("kaptcha.image.width", "100");
properties.setProperty("kaptcha.image.height", "36");
properties.setProperty("kaptcha.textproducer.font.size", "30");
properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.ShadowGimpy");
properties.setProperty("kaptcha.session.key", "code");
properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
properties.setProperty("kaptcha.background.clear.from", "232,240,254");
properties.setProperty("kaptcha.background.clear.to", "232,240,254");
properties.setProperty("kaptcha.textproducer.char.length", "4");
properties.setProperty("kaptcha.textproducer.font.names", "彩云,宋體,楷體,微軟雅黑");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
3、接下來,我們編寫kaptcha的控制層:KaptchaController.java
package com.lzzy.meet.common.kaptcha;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
/**
* @ClassName KaptchaController
* kaptcha調(diào)用
* @Author
* @Date 2019-09-05 13:59:59
* @Version 1.0
**/
@Slf4j
@Controller
@RequestMapping("kaptcha")
public class KaptchaController {
@Autowired
private Producer producer;
@GetMapping("kaptcha-image")
public void getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
String capText = producer.createText();
log.info("******************當(dāng)前驗證碼為:{}******************", capText);
// 將驗證碼存于session中
request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
BufferedImage bi = producer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
// 向頁面輸出驗證碼
ImageIO.write(bi, "jpg", out);
try {
// 清空緩存區(qū)
out.flush();
} finally {
// 關(guān)閉輸出流
out.close();
}
}
}
4、然后,我們就可以在前端調(diào)用katpcha的接口生成驗證碼了:
<img th:src="@{/kaptcha/kaptcha-image}" class="ver_btn" onclick="this.src=this.src+'?c='+Math.random();"/>由于我這里使用的是 thymeleaf 模板引擎,所以路徑名稱會有點奇怪,生成的驗證碼樣式如圖所示:

5、最后,我們將用戶在客戶端登陸時輸入的驗證碼傳送到服務(wù)端進(jìn)行驗證:
/**
* 驗證驗證碼
* @param
* @return 正確:true/錯誤:false
*/
public static boolean validate(String registerCode) {
// 獲取Session中驗證碼
Object captcha = ServletUtils.getAttribute(Constants.KAPTCHA_SESSION_KEY);
// 判斷驗證碼是否為空
if (StringUtils.isEmpty(registerCode)) {
return false;
}
// 校驗驗證碼的正確與否
boolean result = registerCode.equalsIgnoreCase(captcha.toString());
if (result) {
// 正確了后,將驗證碼從session中刪掉
ServletUtils.getRequest().getSession().removeAttribute(Constants.KAPTCHA_SESSION_KEY);
}
// 返回驗證結(jié)果
return result;
}這樣我們就成功的使用kaptcha完成了驗證碼的生成與驗證功能
到此這篇關(guān)于SpringBoot使用Kaptcha實現(xiàn)驗證碼的文章就介紹到這了,更多相關(guān)SpringBoot Kaptcha 驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot+kaptcha實現(xiàn)圖片驗證碼功能詳解
- SpringBoot整合kaptcha實現(xiàn)圖片驗證碼功能
- Springboot?+redis+谷歌開源Kaptcha實現(xiàn)圖片驗證碼功能
- SpringBoot集成Kaptcha驗證碼的詳細(xì)過程
- SpringBoot+kaptcha實現(xiàn)驗證碼花式玩法詳解
- Google Kaptcha 框架實現(xiàn)登錄驗證碼功能(SSM 和 SpringBoot)
- springboot整合kaptcha驗證碼的示例代碼
- SpringBoot 集成Kaptcha實現(xiàn)驗證碼功能實例詳解
- SpringBoot整合Kaptcha實現(xiàn)圖片驗證碼加減乘除功能
相關(guān)文章
Java 實現(xiàn)漢字轉(zhuǎn)換為拼音的實例
這篇文章主要介紹了Java 實現(xiàn)漢字轉(zhuǎn)換為拼音的實例的相關(guān)資料,需要的朋友可以參考下2016-12-12
Java中Json字符串直接轉(zhuǎn)換為對象的方法(包括多層List集合)
下面小編就為大家?guī)硪黄狫ava中Json字符串直接轉(zhuǎn)換為對象的方法(包括多層List集合)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
利用IDEA工具修改Maven多模塊項目標(biāo)識包名全過程記錄
當(dāng)我們?yōu)榧追椒?wù)提供軟件開發(fā)服務(wù)時,需要按照甲方的要求去修改軟件的標(biāo)識,對于Maven項目來說就對應(yīng)著groupId,一般地寫對方公司的域名,如com.example,接下來通過本文給大家分享IDEA修改Maven多模塊項目標(biāo)識包名,感興趣的朋友一起看看吧2022-09-09
SpringBoot中配置Web靜態(tài)資源路徑的方法
這篇文章主要介紹了SpringBoot中配置Web靜態(tài)資源路徑的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

