Hutool工具類
maven創(chuàng)建springboot項(xiàng)目
pom.xml
<!-- 添加圖形驗(yàn)證碼依賴 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-captcha</artifactId>
<version>5.8.6</version>
</dependency>
ICaptcha接口定義如下方法:
createCode 創(chuàng)建驗(yàn)證碼李皇,實(shí)現(xiàn)類需同時(shí)生成隨機(jī)驗(yàn)證碼字符串和驗(yàn)證碼圖片
getCode 獲取驗(yàn)證碼的文字內(nèi)容
verify 驗(yàn)證驗(yàn)證碼是否正確,建議忽略大小寫
write 將驗(yàn)證碼寫出到目標(biāo)流中
1、可以寫一個(gè)普通的java類用來測試
public static void main(String[] args) {
// 定義圖形驗(yàn)證碼的長和寬
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
// 圖形驗(yàn)證碼寫出到指定文件
lineCaptcha.write("E:/test/captcha.png");
}
效果:
2糖儡、寫在controller中
package com.xx.controller;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
@RestController
public class HuToolController {
@GetMapping("/LineCaptcha")
public void createCaptcha(HttpServletResponse response) throws IOException {
// 定義圖形驗(yàn)證碼的長和寬
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
// 圖形驗(yàn)證碼寫出队伟,可以寫出到文件纫塌,也可以寫出到流
lineCaptcha.write(response.getOutputStream());
// 關(guān)閉流
response.getOutputStream().close();
}
}
效果:
圓形干擾驗(yàn)證碼:
// 定義圖形驗(yàn)證碼的長熟掂、寬、驗(yàn)證碼字符數(shù)骡楼、干擾元素個(gè)數(shù)
CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 30);
效果:
扭曲干擾驗(yàn)證碼:
// 定義圖形驗(yàn)證碼的長熔号、寬、驗(yàn)證碼字符數(shù)鸟整、干擾線寬度
ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
GIF驗(yàn)證碼:
// 定義GIF驗(yàn)證碼的長引镊、寬
GifCaptcha captcha = CaptchaUtil.createGifCaptcha(200, 100);
Kaptcha入門配置
maven創(chuàng)建springboot項(xiàng)目
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
</parent>
<dependencies>
<!--添加驗(yàn)證碼實(shí)現(xiàn)庫的依賴 kaptcha-->
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>kaptcha</artifactId>
<version>0.0.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
一、配置文件方式
主啟動類
package com.xx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource(locations = {"classpath:kaptchaConfig.xml"})
public class ManageResourcesApplication {
public static void main(String[] args) {
SpringApplication.run(ManageResourcesApplication.class, args);
}
}
resources目錄下創(chuàng)建文件:kaptchaConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Kaptcha驗(yàn)證碼生成器 -->
<bean name="kaptchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha" scope="singleton">
<property name="config">
<bean class="com.google.code.kaptcha.util.Config">
<constructor-arg>
<props>
<prop key="kaptcha.border">no</prop>
<prop key="kaptcha.textproducer.font.color">black</prop>
<prop key="kaptcha.textproducer.char.space">4</prop>
<prop key="kaptcha.textproducer.char.length">4</prop>
<prop key="kaptcha.textproducer.char.string">123456789</prop>
</props>
</constructor-arg>
</bean>
</property>
</bean>
</beans>
控制器Controller
package com.xx.controller;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
@RequestMapping(value = "login")
@RestController
public class SysLoginController {
@Autowired
private Producer producer;
@GetMapping(value = "captchat.jpg")
public void createKacptcha(HttpServletResponse response, HttpServletRequest request) throws IOException {
response.setHeader("Cache-Control","no-store");
response.setContentType("image/jpeg");
// 文字驗(yàn)證碼
String text = producer.createText();
// 圖片驗(yàn)證碼
BufferedImage image = producer.createImage(text);
// 保存驗(yàn)證碼到session
request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY,text);
ServletOutputStream outputStream = response.getOutputStream();
ImageIO.write(image,"jpg",outputStream);
IOUtils.closeQuietly(outputStream);
}
}
二篮条、配置類方式
主啟動類
@SpringBootApplication
public class ManageResourcesApplication {
public static void main(String[] args) {
SpringApplication.run(ManageResourcesApplication.class, args);
}
}
配置類
package com.xx.comfig;
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
/**
* kaptcha驗(yàn)證碼生成器配置
* @author lwf
* @date 2019/8/21 16:10
*/
@Configuration
public class KaptchaConfig {
@Bean
public DefaultKaptcha producer() {
Properties properties = new Properties();
properties.put("kaptcha.border","no");
properties.put("kaptcha.textproducer.font,color","black");
properties.put("kaptcha.textproducer.char.space","5");
Config config = new Config(properties);
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
控制器Controller
package com.xx.controller;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
@RequestMapping(value = "login")
@RestController
public class SysLoginController {
@Autowired
private Producer producer;
@GetMapping(value = "captchat.jpg")
public void createKacptcha(HttpServletResponse response, HttpServletRequest request) throws IOException {
response.setHeader("Cache-Control","no-store");
response.setContentType("image/jpeg");
// 文字驗(yàn)證碼
String text = producer.createText();
// 圖片驗(yàn)證碼
BufferedImage image = producer.createImage(text);
// 保存驗(yàn)證碼到session
request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY,text);
ServletOutputStream outputStream = response.getOutputStream();
ImageIO.write(image,"jpg",outputStream);
IOUtils.closeQuietly(outputStream);
}
}
測試
啟動服務(wù)訪問:http://localhost:8080/login/captchat.jpg
效果: