SpringBoot集成kaptcha验证码
作者:eagle-zhang 发布时间:2023-06-26 03:56:17
标签:springBoot,kaptcha,验证码
本文实例为大家分享了SpringBoot集成kaptcha验证码的具体代码,供大家参考,具体内容如下
1.kaptcha相关介绍
Kaptcha是一个基于SimpleCaptcha的验证码开源项目。
2.集成方案
①pom.xml中配置依赖
<!-- 验证码-->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
②配置验证码Kaptcha相关设置
@Configuration
public class kaptchaConfig {
@Bean(name="captchaProducer")
public DefaultKaptcha getKaptchaBean(){
DefaultKaptcha defaultKaptcha=new DefaultKaptcha();
Properties properties=new Properties();
properties.setProperty("kaptcha.border", "yes");
properties.setProperty("kaptcha.border.color", "105,179,90");
properties.setProperty("kaptcha.textproducer.font.color", "blue");
properties.setProperty("kaptcha.image.width", "125");
properties.setProperty("kaptcha.image.height", "45");
properties.setProperty("kaptcha.session.key", "code");
properties.setProperty("kaptcha.textproducer.char.length", "4");
properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
Config config=new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
或者
在resources下创建myKaptcher.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">
<bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
<property name="config">
<bean class="com.google.code.kaptcha.util.Config">
<constructor-arg type="java.util.Properties">
<props>
<prop key = "kaptcha.border ">yes</prop>
<prop key="kaptcha.border.color">105,179,90</prop>
<prop key="kaptcha.textproducer.font.color">blue</prop>
<prop key="kaptcha.image.width">100</prop>
<prop key="kaptcha.image.height">50</prop>
<prop key="kaptcha.textproducer.font.size">27</prop>
<prop key="kaptcha.session.key">code</prop>
<prop key="kaptcha.textproducer.char.length">4</prop>
<prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>
<prop key="kaptcha.textproducer.char.string">23456789ABCEFGHJKMNOPQRSTUVWXYZ</prop>
<prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.WaterRipple</prop>
<prop key="kaptcha.noise.color">black</prop>
<prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoise</prop>
<!--<prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.DefaultNoise</prop>-->
<prop key="kaptcha.background.clear.from">185,56,213</prop>
<prop key="kaptcha.background.clear.to">white</prop>
<prop key="kaptcha.textproducer.char.space">3</prop>
</props>
</constructor-arg>
</bean>
</property>
</bean>
</beans>
然后在启动类Application中加载配置
@EnableTransactionManagement// 启动注解事务管理,等同于xml配置方式的 <tx:annotation-driven />
@SpringBootApplication
@EnableScheduling//启动注解定时任务
@MapperScan(basePackages = "com.shawn.mapper")
@ImportResource(locations={"classpath:mykaptcha.xml"})
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
两种配置方式在springboot中均可;
③KaptchaController
@CommonsLog
@Controller
public class KaptchaController extends BaseController {
@Autowired
private Producer captchaProducer;
@GetMapping("/getKaptchaImage")
public void getKaptchaImage() throws Exception {
response.setDateHeader("Expires", 0);
// Set standard HTTP/1.1 no-cache headers.
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
// Set standard HTTP/1.0 no-cache header.
response.setHeader("Pragma", "no-cache");
// return a jpeg
response.setContentType("image/jpeg");
// create the text for the image
String capText = captchaProducer.createText();
// store the text in the session
//request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
//将验证码存到session
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
log.info(capText);
// create the image with the text
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
// write the data out
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
}
}
3.测试效果
来源:https://blog.csdn.net/zhangxing52077/article/details/75270259


猜你喜欢
- 冒泡排序冒泡排序的思想:每次让当前的元素和它的下一个元素比较大小、如果前一个的元素大于后一个元素的话,交换两个元素。这样的话经历一次扫描之后
- 第一种就是 最常见的 用Try..Catch..再try中强转你要确认的string 类型成功就是int catch 就不是&n
- 命名空间的特性首先熟悉一下命名空间的两个概念。声明区域:可以在其中进行声明的区域,如全局文件的声明区域是文件,函数内声明的变量声明区域为代码
- 本文介绍了android沉浸式状态栏,分享给大家,希望对大家有帮助一、概述现在主流的App设计风格很多都用到了Materail Design
- 相信大家都见到了微信图标颜色渐变的过程,是不是感觉很牛逼?不得不说微信团队确实是很厉害的团队,不管是从设计还是开发人员。今天我带大家来看看,
- 本文实例讲述了Android编程开发之ScrollView嵌套GridView的方法。分享给大家供大家参考,具体如下:前些日子在开发中用到了
- 去年谷歌 I/O大会上介绍了一个非常厉害的新框架DataBinding, 数据绑定框架给我们带来了很大的方便,以前我们可能需要在每个Acti
- 引入依赖 <dependency> <g
- 最近项目中需要用到IO流来读取图片以提供前台页面展示,由于以前一直是用url路径的方式进行图片展示,一听说要项目要用IO流读取图片感觉好复杂
- 本文实例讲述了C#全局热键设置与窗体热键设置,分享给大家供大家参考。具体实现方法如下:1、窗体热键首先要设置主窗体KeyPreview为tr
- 简述偶然看到一篇关于阿里新orm框架的文章,好奇的点了进去。开发后端多年,看到这个还是有点兴奋的。常用mysql的orm框架mybatis、
- 配置文件请看上篇Java实现redis https://www.jb51.net/article/190922.htm下面测试redis的集
- properties和yml的区别这几天刚好看到Spring Boot当中有两种配置文件的方式,但是这两种配置方式有什么区别呢?proper
- Android 回调前言:Android中的回调最经典的就是点击事件设置监听(一般通过switch(v.getId()))这里写
- 判断是否为飞行模式: boolean isAirplaneMode = Settings.System.getInt(mConte
- 本文实例为大家分享了安卓Button按钮的四种点击事件,供大家参考,具体内容如下第一种:内部类实现 1.xml里面先设置Button属性&l
- Android绘图操作,通过继承View实现,在onDraw函数中实现绘图。下面是一个简单的例子:public class AndroidT
- 概述本篇文章主要讲解下Map家族中3个相对冷门的容器,分别是WeakHashMap、EnumMap、IdentityHashMap, 想必大
- 最近为公司做的一个Demo里面用到了ScrollView嵌套了GridView和ListView,然而在嵌套的时候我发现GridView和L
- 前言Spring Boot在所有内部日志中使用Commons Logging,但是默认配置也提供了对常用日志的支持,如:Java Util