SpringBoot使用Kaptcha實(shí)現(xiàn)驗(yàn)證碼的生成與驗(yàn)證功能
當(dāng)我們?cè)陧?xiàng)目中登錄使用驗(yàn)證碼的時(shí)候,不妨試試Kaptcha生成驗(yàn)證碼,非常簡(jiǎn)單
1、首先,我們?cè)趐om.xml文件中引入kaptcha的maven依賴(lài)
<!-- kaptcha驗(yàn)證碼 --> <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
2、然后,我們編寫(xiě)kaptcha的配置類(lèi):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配置類(lèi)
* @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、接下來(lái),我們編寫(xiě)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)前驗(yàn)證碼為:{}******************", capText);
// 將驗(yàn)證碼存于session中
request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
BufferedImage bi = producer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
// 向頁(yè)面輸出驗(yàn)證碼
ImageIO.write(bi, "jpg", out);
try {
// 清空緩存區(qū)
out.flush();
} finally {
// 關(guān)閉輸出流
out.close();
}
}
}
4、然后,我們就可以在前端調(diào)用katpcha的接口生成驗(yàn)證碼了:
<img th:src="@{/kaptcha/kaptcha-image}" class="ver_btn" onclick="this.src=this.src+'?c='+Math.random();"/>由于我這里使用的是 thymeleaf 模板引擎,所以路徑名稱(chēng)會(huì)有點(diǎn)奇怪,生成的驗(yàn)證碼樣式如圖所示:

5、最后,我們將用戶(hù)在客戶(hù)端登陸時(shí)輸入的驗(yàn)證碼傳送到服務(wù)端進(jìn)行驗(yàn)證:
/**
* 驗(yàn)證驗(yàn)證碼
* @param
* @return 正確:true/錯(cuò)誤:false
*/
public static boolean validate(String registerCode) {
// 獲取Session中驗(yàn)證碼
Object captcha = ServletUtils.getAttribute(Constants.KAPTCHA_SESSION_KEY);
// 判斷驗(yàn)證碼是否為空
if (StringUtils.isEmpty(registerCode)) {
return false;
}
// 校驗(yàn)驗(yàn)證碼的正確與否
boolean result = registerCode.equalsIgnoreCase(captcha.toString());
if (result) {
// 正確了后,將驗(yàn)證碼從session中刪掉
ServletUtils.getRequest().getSession().removeAttribute(Constants.KAPTCHA_SESSION_KEY);
}
// 返回驗(yàn)證結(jié)果
return result;
}這樣我們就成功的使用kaptcha完成了驗(yàn)證碼的生成與驗(yàn)證功能
到此這篇關(guān)于SpringBoot使用Kaptcha實(shí)現(xiàn)驗(yàn)證碼的文章就介紹到這了,更多相關(guān)SpringBoot Kaptcha 驗(yàn)證碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot+kaptcha實(shí)現(xiàn)圖片驗(yàn)證碼功能詳解
- SpringBoot整合kaptcha實(shí)現(xiàn)圖片驗(yàn)證碼功能
- Springboot?+redis+谷歌開(kāi)源Kaptcha實(shí)現(xiàn)圖片驗(yàn)證碼功能
- SpringBoot集成Kaptcha驗(yàn)證碼的詳細(xì)過(guò)程
- SpringBoot+kaptcha實(shí)現(xiàn)驗(yàn)證碼花式玩法詳解
- Google Kaptcha 框架實(shí)現(xiàn)登錄驗(yàn)證碼功能(SSM 和 SpringBoot)
- springboot整合kaptcha驗(yàn)證碼的示例代碼
- SpringBoot 集成Kaptcha實(shí)現(xiàn)驗(yàn)證碼功能實(shí)例詳解
- SpringBoot整合Kaptcha實(shí)現(xiàn)圖片驗(yàn)證碼加減乘除功能
相關(guān)文章
Spring JPA事務(wù)管理與自定義操作實(shí)例解析(最新推薦)
在Spring框架中,數(shù)據(jù)持久化操作常常與事務(wù)管理緊密相關(guān),本文將深入探討Spring Data JPA中的事務(wù)管理機(jī)制,并結(jié)合具體實(shí)例,展示如何自定義事務(wù)行為以滿(mǎn)足不同的業(yè)務(wù)需求,感興趣的朋友一起看看吧2024-12-12
SpringBoot mybatis 實(shí)現(xiàn)多級(jí)樹(shù)形菜單的示例代碼
這篇文章主要介紹了SpringBoot mybatis 實(shí)現(xiàn)多級(jí)樹(shù)形菜單的示例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05
ExecutorService實(shí)現(xiàn)獲取線(xiàn)程返回值
這篇文章主要介紹了ExecutorService實(shí)現(xiàn)獲取線(xiàn)程返回值,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Java?axios與spring前后端分離傳參規(guī)范總結(jié)
這篇文章主要介紹了Java?axios與spring前后端分離傳參規(guī)范總結(jié),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08
Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:樸素字符匹配 Brute Force
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:樸素字符匹配 Brute Force,本文直接給出實(shí)例代碼,代碼中包含詳細(xì)注釋,需要的朋友可以參考下2015-06-06
Java Map所有的值轉(zhuǎn)為String類(lèi)型
本文主要介紹了Java Map所有的值轉(zhuǎn)為String類(lèi)型,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05

