SpringBoot整合kaptcha實(shí)現(xiàn)圖片驗(yàn)證碼功能
栗子
登錄
是所有系統(tǒng)都繞不開的一道坎,很多系統(tǒng)會(huì)在用戶名和密碼下發(fā)放置一個(gè)圖形驗(yàn)證碼,例如:
這些圖形驗(yàn)證碼看起來不僅很丑,而且模糊,但卻是保護(hù)系統(tǒng)的第一道屏障,它的作用是:設(shè)計(jì)的初衷其實(shí)就是為了防自動(dòng)化,防止一些人利用自動(dòng)工具惡意攻擊網(wǎng)站,比如批量注冊(cè),撐爆你的數(shù)據(jù)庫。
實(shí)現(xiàn)這個(gè)功能并不復(fù)雜,但是為了不讓大家重復(fù)造輪子,這里我給大家推薦一個(gè)現(xiàn)成的輪子:kaptcha
,使用起來非常簡(jiǎn)單,廢話不多說直接上代碼。
配置文件
SpringBoot項(xiàng)目中pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>SpringBoot-VerifyCode</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringBoot-VerifyCode</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 驗(yàn)證碼生成包 --> <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
項(xiàng)目代碼
項(xiàng)目結(jié)構(gòu)
SpringBootVerifyCodeApplication.java
package com.example.springbootverifycode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootVerifyCodeApplication { public static void main(String[] args) { SpringApplication.run(SpringBootVerifyCodeApplication.class, args); } }
VerifyCodeConfig.java
package com.example.springbootverifycode.config; import com.google.code.kaptcha.Producer; 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 VerifyCodeConfig { @Bean Producer producer() { Properties properties = new Properties(); //設(shè)置圖片邊框 properties.setProperty("kaptcha.border", "yes"); //設(shè)置圖片邊框?yàn)樗{(lán)色 properties.setProperty("kaptcha.border.color", "blue"); //背景顏色漸變開始 properties.put("kaptcha.background.clear.from", "127,255,212"); //背景顏色漸變結(jié)束 properties.put("kaptcha.background.clear.to", "240,255,255"); // 字體顏色 properties.put("kaptcha.textproducer.font.color", "black"); // 文字間隔 properties.put("kaptcha.textproducer.char.space", "10"); //如果需要去掉干擾線 properties.put("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise"); // 字體 properties.put("kaptcha.textproducer.font.names", "Arial,Courier,cmr10,宋體,楷體,微軟雅黑"); // 圖片寬度 properties.setProperty("kaptcha.image.width", "200"); // 圖片高度 properties.setProperty("kaptcha.image.height", "50"); // 從哪些字符中產(chǎn)生 properties.setProperty("kaptcha.textproducer.char.string", "0123456789abcdefghijklmnopqrsduvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); // 字符個(gè)數(shù) properties.setProperty("kaptcha.textproducer.char.length", "6"); DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); defaultKaptcha.setConfig(new Config(properties)); return defaultKaptcha; } }
KaptchaController.java
package com.example.springbootverifycode.controller; import com.google.code.kaptcha.Producer; 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.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.awt.image.BufferedImage; import java.io.IOException; @RestController public class KaptchaController { @Autowired private Producer producer; @GetMapping("/getVerifyCode") public void getVerifyCode(HttpServletResponse response, HttpSession session) throws IOException { response.setContentType("image/jpeg"); String text = producer.createText(); session.setAttribute("vf", text); BufferedImage image = producer.createImage(text); try (ServletOutputStream sos = response.getOutputStream()) { ImageIO.write(image, "jpg", sos); } } }
測(cè)試
生成運(yùn)算符驗(yàn)證碼
1、設(shè)置字符個(gè)數(shù)為7
// 文字個(gè)數(shù) properties.put("kaptcha.textproducer.char.space", "7");
2、獲取圖片
@GetMapping("/getCal") public void getCal(HttpServletResponse response, HttpSession session) throws IOException { response.setContentType("image/jpeg"); //生成文字驗(yàn)證碼 String text = producer.createText(); System.out.println("當(dāng)前生成的字符串為:" + text); //個(gè)位數(shù)字相加 String s1 = text.substring(0, 2); String s2 = text.substring(2, 4); int count = Integer.valueOf(s1).intValue() + Integer.valueOf(s2).intValue(); System.out.println("計(jì)算結(jié)果為:" + count); //生成圖片驗(yàn)證碼 BufferedImage image = producer.createImage(s1 + "+" + s2 + "=?"); try (ServletOutputStream sos = response.getOutputStream()) { ImageIO.write(image, "jpg", sos); } }
3、演示一下
后臺(tái)打?。?/strong>
訪問鏈接:
到此這篇關(guān)于SpringBoot整合kaptcha實(shí)現(xiàn)圖片驗(yàn)證碼功能的文章就介紹到這了,更多相關(guān)SpringBoot整合kaptcha內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot+kaptcha實(shí)現(xiàn)圖片驗(yàn)證碼功能詳解
- Springboot?+redis+谷歌開源Kaptcha實(shí)現(xiàn)圖片驗(yàn)證碼功能
- SpringBoot集成Kaptcha驗(yàn)證碼的詳細(xì)過程
- SpringBoot使用Kaptcha實(shí)現(xiàn)驗(yàn)證碼的生成與驗(yàn)證功能
- 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)文章
深入講解基于JDK的動(dòng)態(tài)代理機(jī)制
眾所周知相比于靜態(tài)代理,動(dòng)態(tài)代理避免了開發(fā)人員編寫各個(gè)繁鎖的靜態(tài)代理類,下面這篇文章主要給大家介紹了關(guān)于基于JDK的動(dòng)態(tài)代理機(jī)制的相關(guān)資料,文中通過圖文以及示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07mybatis的大于小于號(hào)轉(zhuǎn)義符號(hào)一覽
這篇文章主要介紹了mybatis的大于小于號(hào)轉(zhuǎn)義符號(hào)一覽,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08SpringMVC集成Web與MVC執(zhí)行流程和數(shù)據(jù)響應(yīng)及交互相關(guān)介紹全面總結(jié)
Spring MVC 是 Spring 提供的一個(gè)基于 MVC 設(shè)計(jì)模式的輕量級(jí) Web 開發(fā)框架,本質(zhì)上相當(dāng)于 Servlet,Spring MVC 角色劃分清晰,分工明細(xì),這篇文章主要介紹了SpringMVC集成Web與MVC執(zhí)行流程和數(shù)據(jù)響應(yīng)及交互2022-10-10Hibernate映射解析之關(guān)聯(lián)映射詳解
所謂關(guān)聯(lián)映射就是將關(guān)聯(lián)關(guān)系映射到數(shù)據(jù)庫里,在對(duì)象模型中就是一個(gè)或多個(gè)引用。下面這篇文章詳細(xì)的給大家介紹了Hibernate映射解析之關(guān)聯(lián)映射的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02token工作機(jī)制及原理附Java生成token工具類
這篇文章介紹了token工作機(jī)制及原理,內(nèi)附Java生成token工具類Demo。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12通過實(shí)例解析synchronized和lock區(qū)別
這篇文章主要介紹了通過實(shí)例解析synchronized和lock區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12