验证码是抵抗批量操作和恶意登录最有效的方式之一。

验证码从产生到现在已经衍生出了很多分支、方式。google kaptcha 是一个非常实用的验证码生成类库。

通过灵活的配置生成各种样式的验证码,并将生成的验证码字符串放到 HttpSession 中,方便获取进行比较。

本文描述在 spring mvc 下快速的将 google kaptcha 集成到项目中(单独使用的话在 web.xml 中配置 KaptchaServlet)。

1.maven 依赖

官方提供的 pom 无法正常使用,使用阿里云仓库对应 kaptcha。

<!-- google 验证码 -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>${kaptcha.version}</version>
</dependency>

2.前端

<img id="kaptchaImage" src="${pageContext.request.contextPath}/captcha-image" width="116" height="36">
    $(function(){
$('#kaptchaImage').click(function () {
$(this).hide().attr('src', '${ctx}/captcha-image?' + Math.floor(Math.random()*100) ).fadeIn();
event.cancelBubble=true;
});
});

3.mvc-context 配置

 <!--goole captcha 验证码配置-->
<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.border">no</prop>
<prop key="kaptcha.textproducer.font.size">45</prop>
<prop key="kaptcha.textproducer.font.color">blue</prop>
<prop key="kaptcha.textproducer.char.length">4</prop>
<prop key="kaptcha.session.key">code</prop>
</props>
</constructor-arg>
</bean>
</property>
</bean>

更多参数:http://www.cnblogs.com/louis80/p/5230507.html

4.服务端

@Controller
public class CaptchaController { private final Producer captchaProducer; @Autowired
public CaptchaController(Producer captchaProducer) {
this.captchaProducer = captchaProducer;
} @RequestMapping(value = "captcha-image")
public String getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg"); String capText = captchaProducer.createText();
request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi, "jpg", out); try {
out.flush();
} finally {
out.close();
}
return null;
}
}

5.session 中获取验证码

request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);

Spring MVC 中使用 Google kaptcha 验证码的更多相关文章

  1. Spring mvc 中使用 kaptcha 验证码

    生成验证码的方式有很多,个人认为较为灵活方便的是Kaptcha ,他是基于SimpleCaptcha的开源项目.使用Kaptcha 生成验证码十分简单并且参数可以进行自定义.只需添加jar包配置下就可 ...

  2. Spring mvc中@RequestMapping 6个基本用法

    Spring mvc中@RequestMapping 6个基本用法 spring mvc中的@RequestMapping的用法.  1)最基本的,方法级别上应用,例如: Java代码 @Reques ...

  3. spring mvc中使用freemark的一点心得

    参考文档: FreeMarker标签与使用 连接http://blog.csdn.net/nengyu/article/details/6829244 freemarker学习笔记--指令参考: ht ...

  4. Http请求中Content-Type讲解以及在Spring MVC中的应用

    引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值 ...

  5. Spring mvc中@RequestMapping 6个基本用法小结(转载)

    小结下spring mvc中的@RequestMapping的用法. 1)最基本的,方法级别上应用,例如: @RequestMapping(value="/departments" ...

  6. Spring MVC中处理静态资源的多种方法

    处理静态资源,我想这可能是框架搭建完成之后Web开发的”头等大事“了. 因为一个网站的显示肯定会依赖各种资源:脚本.图片等,那么问题来了,如何在页面中请求这些静态资源呢? 还记得Spring MVC中 ...

  7. Spring MVC 中的基于注解的 Controller【转】

    原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...

  8. spring mvc中的文件上传

    使用commons-fileupload上传文件所需要的架包有:commons-fileupload 和common-io两个架包支持,可以到Apache官网下砸. 在配置文件spring-mvc.x ...

  9. spring mvc中的valid

    当你希望在spring mvc中直接校验表单参数时,你可以采用如下操作: 声明Validator的方式: 1.为每一个Controller声明一个Validator @Controller publi ...

随机推荐

  1. python 全栈开发,Day100(restful 接口,DRF组件,DRF跨域(cors组件))

    昨日内容回顾 1. 为什么要做前后端分离? - 前后端交给不同的人来编写,职责划分明确.方便快速开发 - 针对pc,手机,ipad,微信,支付宝... 使用同一个接口 2. 简述http协议? - 基 ...

  2. 集合Arraylist的方法的使用和打印

    package chapter090; import java.util.ArrayList;import java.util.List; public class TestList01 { publ ...

  3. Visual Studio强大的帮助工具--Resharper安装与使用

    ReSharper是一个JetBrains公司出品的代码生成工具,其能帮助Microsoft Visual Studio成为一个更佳的IDE.使用ReSharper,你可以进行深度代码分析,智能代码协 ...

  4. 获取table行列

    var table =document.getElementById("add_purchaseOrderDetailList_table"); var rows = table. ...

  5. <sdoi2017>树点涂色

    题解: 首先,按照原树,构建出一个全部都是虚边的LCTLCT ,并用树剖维护每个点到根节点的路径权值valval.可以发现,每个点到根节点的路径权值就是每个点到根节点的路径上实链的个数. 我们发现,操 ...

  6. python全栈开发day18-模块和导入

    1.昨日内容回顾 2.模块和模块导入 1.什么是模块,为什么要模块? py文件就是模块, 把相似的功能放到一个文件,要用的时候 引入就可以直接调用了. import py文件名,导入模块就是执行他的代 ...

  7. 【LeetCode】161. One Edit Distance

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given two strings S and T, determine if the ...

  8. Redis数据结构之set

    一:介绍 1.set结构 没有顺序 并且,不允许出现重复的元素. 二:Redis客户端的常用命令 1.添加 2.查看数据 3.删除数据 4.是否存在某个值 1代表有,0代表无. 5.查看差值 有key ...

  9. Spring bean加载多个配置文件

    除了写很简单的加载一个xml,加载多个的情况一直没用到,在公司里也不会由自己处理这个问题,现在需要用到了,就研究验证一下. 使用的案例还是上面的例子. 只有,将原来的beans.xml分成两个部分. ...

  10. 091实战 Nginx配置(日志服务器中关于日志的产生)

    一:概括 1.需要配置的概括 定义日志格式 日志的分割字段:^A 日志格式:IP地址^A服务器时间^A请求参数 配置location,记录请求日志到本地磁盘 将数据按照给定的日志格式存储到本地磁盘 二 ...