springboot整合kaptcha生成驗(yàn)證碼功能
介紹:kaptcha 是谷歌開(kāi)源的非常實(shí)用的驗(yàn)證碼生成工具
一、導(dǎo)入jar包
<!-- kaptcha驗(yàn)證碼 --> <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
二、編寫(xiě)kaptcha配置類
package com.zym.config;
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 KaptchaConfig {
@Bean
public DefaultKaptcha defaultKaptcha(){
DefaultKaptcha kaptcha = new DefaultKaptcha();
Properties properties = new Properties();
//邊框
properties.setProperty("kaptcha.border", "no");
//字體顏色
properties.setProperty("kaptcha.textproducer.font.color", "green");
//圖片寬度
properties.setProperty("kaptcha.image.width", "120");
//圖片高度
properties.setProperty("kaptcha.image.height", "30");
//字體大小
properties.setProperty("kaptcha.textproducer.font.size", "20");
//session key
properties.setProperty("kaptcha.session.key", "kaptcha");
//驗(yàn)證碼長(zhǎng)度
properties.setProperty("kaptcha.textproducer.char.length", "4");
//字體
properties.setProperty("kaptcha.textproducer.font.names", "宋體");
//文字間隔
properties.setProperty("kaptcha.textproducer.char.space", "10");
//噪點(diǎn)實(shí)現(xiàn)類
properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
//圖片樣式-陰影
properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.ShadowGimpy");
Config config = new Config(properties);
kaptcha.setConfig(config);
return kaptcha;
}
}
三、編寫(xiě)接口
package com.zym.controller;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.apache.tomcat.util.http.fileupload.IOUtils;
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.HttpServletRequest;
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 DefaultKaptcha defaultKaptcha;
@GetMapping("/kaptcha")
public void getKaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
//設(shè)置響應(yīng)頭
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
String text = defaultKaptcha.createText();
HttpSession session = request.getSession();
//將驗(yàn)證碼存入session
session.setAttribute("code", text);
//創(chuàng)建驗(yàn)證碼圖片
BufferedImage image = defaultKaptcha.createImage(text);
ServletOutputStream os = response.getOutputStream();
ImageIO.write(image, "jpg", os);
IOUtils.closeQuietly(os);
}
}
四、測(cè)試接口
使用PostMan:
輸入url:localhost:8080/kaptcha
成功得到驗(yàn)證碼圖片,大功告成!

到此這篇關(guān)于springboot整合kaptcha生成驗(yàn)證碼功能的文章就介紹到這了,更多相關(guān)springboot整合kaptcha驗(yàn)證碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Springboot通過(guò)谷歌Kaptcha?組件生成圖形驗(yàn)證碼功能
- springboot驗(yàn)證碼生成以及驗(yàn)證功能舉例詳解
- SpringBoot使用Kaptcha實(shí)現(xiàn)驗(yàn)證碼的生成與驗(yàn)證功能
- springboot驗(yàn)證碼的生成與驗(yàn)證的兩種方法
- SpringBoot 圖形驗(yàn)證碼的生成和校驗(yàn)
- SpringBoot實(shí)現(xiàn)Thymeleaf驗(yàn)證碼生成
- SpringBoot使用Captcha生成驗(yàn)證碼
- springboot控制層圖片驗(yàn)證碼生成
- SpringBoot實(shí)現(xiàn)前端驗(yàn)證碼圖片生成和校驗(yàn)
- SpringBoot使用hutool-captcha實(shí)現(xiàn)驗(yàn)證碼生成與驗(yàn)證
相關(guān)文章
使用MyBatis查詢千萬(wàn)級(jí)數(shù)據(jù)量操作實(shí)現(xiàn)
這篇文章主要為大家介紹了如何使用MyBatis?查詢千萬(wàn)數(shù)據(jù)量的操作過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
JAVA爬蟲(chóng)實(shí)現(xiàn)自動(dòng)登錄淘寶
給大家分享一個(gè)關(guān)于JAVA爬蟲(chóng)的相關(guān)知識(shí)點(diǎn),通過(guò)代碼實(shí)現(xiàn)自動(dòng)登錄淘寶網(wǎng),有興趣的朋友測(cè)試下。2018-04-04
詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn))
本篇文章主要介紹了詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
Spring Boot 實(shí)現(xiàn)https ssl免密登錄(X.509 pki登錄)
這篇文章主要介紹了Spring Boot 實(shí)現(xiàn)https ssl免密登錄(X.509 pki登錄),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01

