SpringBoot集成Kaptcha实现验证码功能实例详解

  

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

  

  

1。你要有一个springboot的hello world的工程,并能正常运行。

  

2。导入kaptcha的maven:

        & lt; !——https://mvnrepository.com/artifact/com.github.penggle/kaptcha——比;   & lt; dependency>   & lt; groupId> com.github.penggle   & lt; artifactId> kaptcha   & lt; version> 2.3.2   & lt;/dependency>之前      

  

我们有两种方式在springboot中使用kaptcha

  

第一种使用xml的配置方式配置生成kaptcha的bean对象,在启动类上@ImportResource这个xml文件,在控制器中注入其对象并使用

  

第二种是把kaptcha作为工程的一个类,加上@ component注解在返回kaptcha的方法中加@ bean注上解,再在控制器中注入其对象。

  

<强>第一种方法:

  

在资源中创建一个xxx。xml文件如:

  

mykaptcha.xml文件:

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt;豆类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”比;   & lt; bean id=癱aptchaProducer”类=癱om.google.code.kaptcha.impl.DefaultKaptcha”比;   & lt;属性名="配置"比;   & lt; bean类=癱om.google.code.kaptcha.util.Config”比;   & lt; constructor-arg类型=癹ava.util.Properties”比;   & lt; props>   & lt;支撑关键=" kaptcha。边境”在yes   & lt;支撑关键=" kaptcha.border.color "在105179年,90 & lt;/prop>   & lt;支撑关键=" kaptcha.textproducer.font.color "祝辞blue   & lt;支撑关键=" kaptcha.image.width "在100 & lt;/prop>   & lt;支撑关键=" kaptcha.image.height "祝辞50 & lt;/prop>   & lt;支撑关键=" kaptcha.textproducer.font.size "在27日& lt;/prop>   & lt;支撑关键=" kaptcha.session.key "祝辞code   & lt;支撑关键=" kaptcha.textproducer.char.length "祝辞4 & lt;/prop>   & lt;支撑关键=発aptcha.textproducer.font.names”在宋体,楷体,微软雅黑& lt;/prop>   & lt;支撑关键=" kaptcha.textproducer.char.string "在0123456789 abcefghijklmnopqrstuvwxyz   & lt;支撑关键=" kaptcha.obscurificator.impl "祝辞com.google.code.kaptcha.impl.WaterRipple   & lt;支撑关键=" kaptcha.noise.color "祝辞black   & lt;支撑关键=" kaptcha.noise.impl "祝辞com.google.code.kaptcha.impl.DefaultNoise   & lt;支撑关键=" kaptcha.background.clear.from "在185年,56213 & lt;/prop>   & lt;支撑关键=" kaptcha.background.clear.to "祝辞white   & lt;支撑关键=" kaptcha.textproducer.char.space "在3 & lt;/prop>   & lt;/props>   & lt;/constructor-arg>   & lt;/bean>   & lt;/property>   & lt;/bean>   & lt;/beans>之前      

在springboot启动类上引入这个文件

        @SpringBootApplication   @ImportResource(位置={"类路径:mykaptcha.xml})   公共类应用程序{   公共静态void main (String [] args) {   SpringApplication.run (Application.class, args);   }   }      

在控制器中使用:

        @ autowired   DefaultKaptcha DefaultKaptcha;   ……   @RequestMapping ("/defaultKaptcha”)   公共空defaultKaptcha (HttpServletRequest HttpServletRequest HttpServletResponse HttpServletResponse){抛出异常   byte [] captchaChallengeAsJpeg=零;   ByteArrayOutputStream jpegOutputStream=new ByteArrayOutputStream ();   尝试{//生产验证码字符串并保存到会话中   字符串createText=defaultKaptcha.createText ();   httpServletRequest.getSession ()。setAttribute (“vrifyCode createText);//使用生产的验证码字符串返回一个BufferedImage对象并转为字节写入到字节数组中   BufferedImage挑战=defaultKaptcha.createImage (createText);   ImageIO。写(挑战,“jpg”, jpegOutputStream);   }捕捉(IllegalArgumentException e) {   httpServletResponse.sendError (HttpServletResponse.SC_NOT_FOUND);   返回;   }//定义响应输出类型为图像/jpeg类型,使用响应输出流输出图片的字节数组   captchaChallengeAsJpeg=jpegOutputStream.toByteArray ();   httpServletResponse。setHeader (“cache - control”,“不是商店”);   httpServletResponse。setHeader(“杂注”,“no - cache”);   httpServletResponse。setDateHeader(“到期”,0);   httpServletResponse.setContentType(“图像/jpeg”);   ServletOutputStream responseOutputStream=httpServletResponse.getOutputStream ();   responseOutputStream.write (captchaChallengeAsJpeg);   responseOutputStream.flush ();   responseOutputStream.close ();   }

SpringBoot集成Kaptcha实现验证码功能实例详解