在一个web应用中验证码是一个常见的元素。不管是防止机器人还是爬虫都有一定的作用,我们是自己编写生产验证码的工具类,也可以使用一些比较方便的验证码工具。在网上收集一些资料之后,今天给大家介绍一下kaptcha的和springboot一起使用的简单例子。

准备工作:
1.你要有一个springboot的hello world的工程,并能正常运行。
2.导入kaptcha的maven:
<!-- https://mvnrepository.com/artifact/com.github.penggle/kaptcha --> <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
开始实验:
我们有两种方式在springboot中使用kaptcha
第一种使用.xml的配置方式配置生成kaptcha的bean对象,在启动类上@ImportResource这个xml文件;在controller中注入其对象并使用
第二种是把kaptcha作为工程的一个类,加上@component注解在返回kaptcha的方法中加上@Bean注解,再在controller中注入其对象。
第一种方法:
在resources中创建一个xxx.xml文件 如:
mykaptcha.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">0123456789ABCEFGHIJKLMNOPQRSTUVWXYZ</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.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>
在springboot启动类上引入这个文件
@SpringBootApplication
@ImportResource(locations={"classpath:mykaptcha.xml"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
在controller中使用:
@Autowired
DefaultKaptcha defaultKaptcha;
......
@RequestMapping("/defaultKaptcha")
public void defaultKaptcha(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws Exception{
byte[] captchaChallengeAsJpeg = null;
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
try {
//生产验证码字符串并保存到session中
String createText = defaultKaptcha.createText();
httpServletRequest.getSession().setAttribute("vrifyCode", createText);
//使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
BufferedImage challenge = defaultKaptcha.createImage(createText);
ImageIO.write(challenge, "jpg", jpegOutputStream);
} catch (IllegalArgumentException e) {
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
//定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组
captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
httpServletResponse.setHeader("Cache-Control", "no-store");
httpServletResponse.setHeader("Pragma", "no-cache");
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/jpeg");
ServletOutputStream responseOutputStream =
httpServletResponse.getOutputStream();
responseOutputStream.write(captchaChallengeAsJpeg);
responseOutputStream.flush();
responseOutputStream.close();
}
验证的方法:
@RequestMapping("/imgvrifyControllerDefaultKaptcha")
public ModelAndView imgvrifyControllerDefaultKaptcha(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
ModelAndView andView = new ModelAndView();
String captchaId = (String) httpServletRequest.getSession().getAttribute("vrifyCode");
String parameter = httpServletRequest.getParameter("vrifyCode");
System.out.println("Session vrifyCode "+captchaId+" form vrifyCode "+parameter);
if (!captchaId.equals(parameter)) {
andView.addObject("info", "错误的验证码");
andView.setViewName("index");
} else {
andView.addObject("info", "登录成功");
andView.setViewName("succeed");
}
return andView;
}
模板html:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title>hello</title>
</head>
<body>
<h1 th:text="${info}" />
<div>
<!-- <img alt="这是图片" src="/img/001.png"/> -->
<img alt="验证码" onclick = "this.src='/defaultKaptcha?d='+new Date()*1" src="/defaultKaptcha" />
</div>
<form action="imgvrifyControllerDefaultKaptcha">
<input type="text" name="vrifyCode" />
<input type="submit" value="提交"></input>
</form>
</body>
</html>
启动并访问:
提交:
第二中方发:
这种方法把.xml文件换成使用代码来配置:
KaptchaConfig.Java:
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
@Component
public class KaptchaConfig {
@Bean
public DefaultKaptcha getDefaultKaptcha(){
com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.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", "110");
properties.setProperty("kaptcha.image.height", "40");
properties.setProperty("kaptcha.textproducer.font.size", "30");
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;
}
}
注意要去掉启动类中引入的.xml文件,不然会有两个相同的对象,而你没有指明要注入哪一个的话启动会失败。
启动并测试:
到这里就算成功了。(也有使用jcaptcha的,只是他们最好不要再一个工程中使用,使用到了相同的类,有时候会导致异常。)
补充:对于kaptcha的配置属性大家可以找找,根据属性就可以配置了。
总结
以上所述是小编给大家介绍的SpringBoot 集成Kaptcha实现验证码功能实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
# spring
# boot
# kaptcha实现验证码
# SpringBoot实现短信验证码校验方法思路详解
# SpringBoot实现前端验证码图片生成和校验
# Springboot实现验证码登录
# SpringBoot发送邮箱验证码功能
# SpringBoot使用邮箱发送验证码实现注册功能
# springboot实现邮箱验证码功能
# SpringBoot发送邮件功能 验证码5分钟过期
# SpringBoot登录验证码实现过程详解
# SpringBoot后端验证码的实现示例
# 验证码
# 微软
# 给大家
# 小编
# 宋体
# 是一个
# 这是
# 也有
# 会有
# 你要
# 在此
# 要去
# 有一定
# 不要再
# 可以使用
# 种方法
# 有两种
# 而你
# 并能
# 准备工作
相关文章:
如何高效利用200m空间完成建站?
C++ static_cast和dynamic_cast区别_C++静态转换与动态类型安全转换
如何挑选优质建站一级代理提升网站排名?
娃派WAP自助建站:免费模板+移动优化,快速打造专业网站
小视频制作网站有哪些,有什么看国内小视频的网站,求推荐?
移民网站制作流程,怎么看加拿大移民官网?
制作网站哪家好,cc、.co、.cm哪个域名更适合做网站?
天津个人网站制作公司,天津网约车驾驶员从业资格证官网?
mc皮肤壁纸制作器,苹果平板怎么设置自己想要的壁纸我的世界?
网站制作服务平台,有什么网站可以发布本地服务信息?
整蛊网站制作软件,手机不停的收到各种网站的验证码短信,是手机病毒还是人为恶搞?有这种手机病毒吗?
如何在橙子建站上传落地页?操作指南详解
香港服务器建站指南:外贸独立站搭建与跨境电商配置流程
如何选择高效响应式自助建站源码系统?
深圳企业网站制作设计,在深圳如何网上全流程注册公司?
如何用西部建站助手快速创建专业网站?
网站制作公司排行榜,抖音怎样做个人官方网站
如何挑选最适合建站的高性能VPS主机?
建站VPS配置与SEO优化指南:关键词排名提升策略
如何制作公司的网站链接,公司想做一个网站,一般需要花多少钱?
如何通过可视化优化提升建站效果?
为什么Go需要go mod文件_Go go mod文件作用说明
实例解析angularjs的filter过滤器
如何快速搭建自助建站会员专属系统?
建站之星官网登录失败?如何快速解决?
建设网站制作价格,怎样建立自己的公司网站?
简历在线制作网站免费版,如何创建个人简历?
宝塔建站教程:一键部署配置流程与SEO优化实战指南
如何在建站主机中优化服务器配置?
定制建站策划方案_专业建站与网站建设方案一站式指南
如何访问已购建站主机并解决登录问题?
广州营销型建站服务商推荐:技术优势与SEO优化解析
如何快速生成橙子建站落地页链接?
如何通过免费商城建站系统源码自定义网站主题与功能?
建站之星代理商如何保障技术支持与售后服务?
Android滚轮选择时间控件使用详解
制作农业网站的软件,比较好的农业网站推荐一下?
如何实现建站之星域名转发设置?
免费视频制作网站,更新又快又好的免费电影网站?
如何在IIS服务器上快速部署高效网站?
用v-html解决Vue.js渲染中html标签不被解析的问题
rsync同步时出现rsync: failed to set times on “xxxx”: Operation not permitted
大连网站制作公司哪家好一点,大连买房网站哪个好?
如何在阿里云购买域名并搭建网站?
如何用狗爹虚拟主机快速搭建网站?
营销式网站制作方案,销售哪个网站招聘效果最好?
图片制作网站免费软件,有没有免费的网站或软件可以将图片批量转为A4大小的pdf?
哪家制作企业网站好,开办像阿里巴巴那样的网络公司和网站要怎么做?
如何在阿里云服务器自主搭建网站?
C++如何将C风格字符串(char*)转换为std::string?(代码示例)
*请认真填写需求信息,我们会在24小时内与您取得联系。