SpringMVC项目中使用kaptcha生成验证码

来源:互联网 发布:今天美国非农数据 编辑:程序博客网 时间:2024/06/09 19:55

一、简介

kaptcha是一个基于SimpleCaptche的验证码开源项目

二、使用

1、添加jar包依赖

如果你使用maven来统一管理jar包,则在工程的pom.xml中添加dependency

<span style="font-size:14px;"><!-- kaptcha -->  <dependency>      <groupId>com.google.code.kaptcha</groupId>      <artifactId>kaptcha</artifactId>      <version>2.3.2</version>  </dependency> </span>

2、添加kaptcha的配置文件spring-kaptcha.xml并定义类captchaProducer

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsd"default-autowire="byName"><beanid="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.border">no</prop><prop key="kaptcha.border.color">105,179,90</prop><prop key="kaptcha.textproducer.font.color">blue</prop><prop key="kaptcha.image.width">250</prop><prop key="kaptcha.textproducer.font.size">90</prop><prop key="kaptcha.image.height">90</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">0123456789</prop></props></constructor-arg></bean></property></bean></beans>

3、使用注解的方式把定义的Bean注入到生成验证码的Controller中

@Autowiredprivate Producer captchaProducer;

4、生成验证码

@Controller@RequestMapping("/corp/portal")public class CorpPortalController extends BaseController {         /* 企业找回密码验证码 */     private static final String CORP_FIND_PWD_AUTH_CODE = "corpFindPwdAuthCode";          /** * 生成找回密码的验证码 */@RequestMapping("/findPwdAuthCode.htm")public void findPwdAuthCode(HttpServletRequest request, HttpServletResponse response, HttpSession session)throws IOException {/* Expires过时期限值,指浏览器或缓存服务器在该时间点后必须从真正的服务器中获取新的页面信息 */response.setDateHeader("Expires", 0);/* 浏览器和缓存服务器都不应该缓存页面信息 *///response.setHeader("Cache-Control", "no-cache");/* 请求和响应的信息都不应该被存储在对方的磁盘系统中 *///response.setHeader("Cache-Control", "no-store");/* 浏览器和缓存服务器都可以缓存页面信息 *///response.setHeader("Cache-Control", "public");/* 对于客户机的每次请求,代理服务器必须向服务器验证缓存是否过时 *///response.setHeader("Cache-Control", "must-revalidate");response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");/* 不让浏览器或中间缓存服务器缓存页面,配合Expires 置为0限定更保险 */response.setHeader("Pragma", "no-cache");/* * response.setContentType(MIME)的作用是使客户端浏览器区分不同类型的数据, * 并根据不同的MIME调用浏览器内部不同的程序嵌入模块来处理相应的数据 */response.setContentType("image/jpeg");/* 生成验证码 */String capText = captchaProducer.createText();/* 保存验证码到Session中 */request.getSession().setAttribute(CORP_FIND_PWD_AUTH_CODE, capText);/* 使用给定文字创建图片 */BufferedImage bi = captchaProducer.createImage(capText);/* 数据写入输出流 */ServletOutputStream out = response.getOutputStream();ImageIO.write(bi, "jpg", out);try {out.flush();} finally {out.close();}}}







0 0