Kaptcha 是一个可高度配置的实用验证码生成工具。个人认为还是挺好用的,虽然现有更加安全的验证方式。
此次是使用springboot+ssm,使用maven管理。直接上步骤:
- pom文件添加jar包依赖:
<!-- kaptcha验证码 --> <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
- 配置Kaptcha验证码的样式:在src/main/resources编写一个kaptchaXML.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:gfe="http://www.springframework.org/schema/gemfire" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd" default-lazy-init="true"> <bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha"> <property name="config"> <bean class="com.google.code.kaptcha.util.Config"> <constructor-arg> <props> <!-- 验证码宽度 --> <prop key="kaptcha.image.width">150</prop> <!-- 验证码高度 --> <prop key="kaptcha.image.height">40</prop> <!-- 生成验证码内容范围 --> <prop key="kaptcha.textproducer.char.string">0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ</prop> <!-- 验证码个数 --> <prop key="kaptcha.textproducer.char.length">5</prop> <!-- 是否有边框 --> <prop key="kaptcha.border">no</prop> <!-- 边框颜色 --> <prop key="kaptcha.border.color">105,179,90</prop> <!-- 边框厚度 --> <prop key="kaptcha.border.thickness">1</prop> <!-- 验证码字体颜色 --> <prop key="kaptcha.textproducer.font.color">black</prop> <!-- 验证码字体大小 --> <prop key="kaptcha.textproducer.font.size">30</prop> <!-- 验证码所属字体样式 --> <prop key="kaptcha.textproducer.font.names">微软雅黑</prop> <!-- 干扰线颜色 --> <prop key="kaptcha.noise.color">black</prop> <!-- 验证码文本字符间距 --> <prop key="kaptcha.textproducer.char.space">5</prop> <!-- 图片样式 :阴影--> <prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.ShadowGimpy</prop> <!-- --> </props> </constructor-arg> </bean> </property> </bean> </beans>
- 在启动类用@ImportResource来导入xml配置文件
@SpringBootApplication @ImportResource(locations = { "classpath:KaptchaXML.xml" }) @MapperScan("com.p.mapper") public class MyPApplication { public static void main(String[] args) { SpringApplication.run(MyPApplication.class, args); } }
- 生成验证码
package com.p.controller; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.google.code.kaptcha.impl.DefaultKaptcha; @Controller @RequestMapping("/code") public class Codecontroller { @Autowired private DefaultKaptcha captchaProducer; /** * @Title: defaultKaptcha * @Description: 生成验证码,并存到session中 * @param @param httpServletRequest * @param @param httpServletResponse * @param @throws Exception 参数 * @return void 返回类型 * @throws */ @RequestMapping("/defaultKaptcha") public void defaultKaptcha(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws Exception{ byte[] captchaChallengeAsJpeg = null; ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream(); try { //生产验证码字符串并保存到session中 String createText = captchaProducer.createText(); //打印验证码 System.out.println(createText); httpServletRequest.getSession().setAttribute("code", createText); //使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中 BufferedImage challenge = captchaProducer.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(); } }
- 在前端使用:
<div class="layui-tab-content" > <div class="layui-tab-item layui-show"> <form class="layui-form hidePage" id="form2" action="" method="post"> <div class="layui-form-item"> <label class="layui-form-label">用户名</label> <div class="layui-input-block"> <input type="text" name="username" required lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input un"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">密 码</label> <div class="layui-input-inline"> <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input pw"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">验证码</label> <input type="text" id="login-code" name="code" required lay-verify="required" placeholder="请输入验证码" autocomplete="off" class="layui-input verity" style="display: inline-block;"> <img alt="验证码" onclick = "this.src='/code/defaultKaptcha?d='+new Date()*1" th:src="@{./code/defaultKaptcha}" id="l-changeimgId" class="v-code"/> </div> <div class="layui-form-item"> <div class="layui-input-block"> <button class="layui-btn login" lay-submit lay-filter="login_submit">登陆</button> </div> </div> <!-- <a href="" class="font-set">忘记密码?</a> <a href="javascript:;" class="font-set reg-show">立即注册</a> --> </form>
- 效果图样
最后就此完成Kaptcha生成验证码的整个过程了。