Struts2整合Jcaptcha1.0实现异步验证码
验证码的英文CAPTCHA 这个词最早是在2000年由卡内基梅隆大学的Luis von Ahn、Manuel Blum、Nicholas J.Hopper以及IBM的John Langford所提出。CAPTCHA 是“Completely Automated Public Turing test to tell Computers and Humans Apart”(全自动区分计算机和人类的图灵测试)的缩写,是一种区分用户是计算机和人的公共全自动程序。这个问题可以由计算机生成并评判,但是必须只有人类才能解答。由于计算机无法解答CAPTCHA的问题,所以回答出问题的用户就可以被认为是人类。
----引自"百度百科"
首先,从sourceforge网站下载Download jcaptcha-1.0-bin.zip (9.2 MB)
jCaptcha官方网站是https://jcaptcha.atlassian.net/wiki/display/general/Home
如果机上装有SVN客户端,还可以下载源代码,SVN地址是:
https://jcaptcha.svn.sourceforge.net/svnroot/jcaptcha
以上地址从SVN上checkout出整个jcaptcha项目(包括trunk,tags,branch等)100多M,网速慢的比较耗时,建议先浏览仓库,checkout一个tags标签版本即可
当然,如果机上又装mvn,那就可以打包成源代码的jar文件了,方便我们学习,mvn命令是: mvn source:jar
确定环境中包含下列依赖:
其中 jcaptcha依赖于 commons-logging和commons-collections
集成struts2依赖于jcaptcha4struts2
strut2-sitemesh和sitemesh已本博文无关
1,查看jcaptcha4struts2插件配置struts-plugin.xml
<struts> <include file="struts-default.xml" /> <package name="jcaptcha4struts2-default" extends="struts-default"> <result-types> <result-type name="captchaImage" class="com.google.code.jcaptcha4struts2.core.actions.support.CaptchaImageResult" /> </result-types> <interceptors> <interceptor name="jcaptchaValidation" class="com.google.code.jcaptcha4struts2.core.interceptors.JCaptchaValidationIntercepter" /> <interceptor-stack name="jcaptchaDefaultStack"> <interceptor-ref name="defaultStack" /> <interceptor-ref name="jcaptchaValidation" /> </interceptor-stack> </interceptors> <action name="jcaptcha_image" class="com.google.code.jcaptcha4struts2.core.actions.JCaptchaImageAction"> <result type="captchaImage" /> </action> </package> </struts>
2.单纯使用内置的验证码功能
[...] <script type="text/javascript"> function changeImage() { var jcaptcha_image = document.getElementById("jcaptcha_image"); var timestamp = new Date().getTime(); jcaptcha_image.src = "ajax_captcha.action?timestamp=" + timestamp; var jcaptcha_input = document.getElementById("txtVerifyCode"); jcaptcha_input.value = ""; jcaptcha_input.focus(); } </script> </head> <body> <div class="form"> <form id="register" name="register" method="post" action="user/register"> [...] <table width="800px" style="border-collapse:collapse"> [...] <tr> <td class="label"><label for="verify_code">验证码:</label> </td> <td class="field"> <div class="jcaptcha_image"> <!-- 验证码 --> <img id="jcaptcha_image" src="jcaptcha_image.action" /> </div> <!-- 验证码输入框 --> <input name="jCaptchaResponse" type="text" id="txtVerifyCode" class="yzm_input clear-fix" /> <div class="text_left"> <p class="t1"> <span id="vcodeValidMsg">请输入图片中的七个字母。</span> <span id="number.info" style="color:red"></span> <a href="javascript:changeImage()">看不清楚?换个图片</a> </p> </div> </td> </tr> <tr> <td></td> <td class="btn-register"><button type="submit" id="btn_register" class="btn-register">同意协议并注册</button> </td> </tr> </table> </form> </div> </body> </html>
struts.xml配置文件添加拦截器
<action name="register" class="org.tarena.dang.action.RegisterAction" method="register"> <!-- 校验验证码拦截器 --> <interceptor-ref name="jcaptchaDefaultStack" /> <result name="verify">/user/verify_form.jsp</result> </action>
3.自定义验证码
- 首先,实现自己的CaptchaEngine
package com.taobao.utils; import java.awt.Color; import java.awt.Font; import java.awt.image.ImageFilter; import com.jhlabs.image.WaterFilter; import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator; import com.octo.captcha.component.image.backgroundgenerator.UniColorBackgroundGenerator; import com.octo.captcha.component.image.color.RandomListColorGenerator; import com.octo.captcha.component.image.deformation.ImageDeformation; import com.octo.captcha.component.image.deformation.ImageDeformationByFilters; import com.octo.captcha.component.image.fontgenerator.FontGenerator; import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator; import com.octo.captcha.component.image.textpaster.MySimpleTextParser; import com.octo.captcha.component.image.textpaster.TextPaster; import com.octo.captcha.component.image.wordtoimage.DeformedComposedWordToImage; import com.octo.captcha.component.image.wordtoimage.WordToImage; import com.octo.captcha.component.word.FileDictionary; import com.octo.captcha.component.word.wordgenerator.ComposeDictionaryWordGenerator; import com.octo.captcha.component.word.wordgenerator.WordGenerator; import com.octo.captcha.engine.image.ListImageCaptchaEngine; import com.octo.captcha.image.gimpy.GimpyFactory; public class MySimpleWaterCaptchaEngine extends ListImageCaptchaEngine{ @Override protected void buildInitialFactories() { WaterFilter waterFilter = new WaterFilter(); waterFilter.setAmplitude(5d);//振幅 waterFilter.setAntialias(true);//锯齿或平滑 waterFilter.setPhase(-90);//相位 waterFilter.setWavelength(50d);//波长 int minWordLength = 4; //最少字符数 int maxWordLength = 5; //最大字符数 int fontSize = 50; //字体大小 int imageWidth = 150; //背景图长度 int imageHeight = 50; //背景图宽度 //背景图生成 BackgroundGenerator backGround = new UniColorBackgroundGenerator(imageWidth, imageHeight, Color.white); //生成单词 WordGenerator dictionnaryWords = new ComposeDictionaryWordGenerator(new FileDictionary("toddlist")); //字体生成 FontGenerator font = new RandomFontGenerator(fontSize, fontSize, new Font[] { new Font("nyala", Font.BOLD, fontSize), new Font("Bell MT", Font.PLAIN, fontSize), new Font("Credit valley", Font.BOLD, fontSize) }); //单词转化成图片 TextPaster randomPaster = new MySimpleTextParser( minWordLength, maxWordLength, new RandomListColorGenerator(new Color[]{Color.black, Color.pink, Color.gray}), true); //backgroundDeformation 背景图变形 ImageDeformation backDef = new ImageDeformationByFilters(new ImageFilter[] {}); //textDeformation 字符图层转变形 ImageDeformation textDef = new ImageDeformationByFilters(new ImageFilter[] {}); //finalDeformation 最终图片变形 ImageDeformation postDef = new ImageDeformationByFilters(new ImageFilter[] {waterFilter}); //转化图片 WordToImage word2image = new DeformedComposedWordToImage(font, backGround, randomPaster, backDef, textDef, postDef); addFactory(new GimpyFactory(dictionnaryWords, word2image)); } }
- 由于jcaptcha4struts2插件内置生成的图片文字是从背景的X坐标为background.getHeight()/20 , Y轴坐标为background.getWidth()/2开始画图的,所以看上去文字会走动比较大,我们可以自己修改一下,实现自己的验证码文字生成,由于AbstractTextPaster所在包访问权限有所限制,故我这里把实现放在与AbstractTextPaster同一个包内
/* * JCaptcha, the open source java framework for captcha definition and integration * Copyright (c) 2007 jcaptcha.net. All Rights Reserved. * See the LICENSE.txt file distributed with this package. */ package com.octo.captcha.component.image.textpaster; import com.octo.captcha.CaptchaException; import com.octo.captcha.component.image.color.ColorGenerator; import java.awt.*; import java.awt.image.BufferedImage; import java.text.AttributedString; /** * <p/> * Pastes the text at width/20 and height/2 </p> * * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue </a> * @version 1.0 */ public class MySimpleTextParser extends AbstractTextPaster { public MySimpleTextParser(Integer minAcceptedWordLength, Integer maxAcceptedWordLength, Color textColor) { super(minAcceptedWordLength, maxAcceptedWordLength, textColor); } public MySimpleTextParser(Integer minAcceptedWordLength, Integer maxAcceptedWordLength, ColorGenerator colorGenerator) { super(minAcceptedWordLength, maxAcceptedWordLength, colorGenerator); } public MySimpleTextParser(Integer minAcceptedWordLength, Integer maxAcceptedWordLength, ColorGenerator colorGenerator, Boolean manageColorPerGlyph) { super(minAcceptedWordLength, maxAcceptedWordLength, colorGenerator, manageColorPerGlyph); } /** * Pastes the attributed string on the backround image and return the final image. Implementation must take into * account the fact that the text must be readable by human and non by programs. Pastes the text at width/20 and * height/2 * * @return the final image * * @throws com.octo.captcha.CaptchaException * if any exception accurs during paste routine. */ public BufferedImage pasteText(final BufferedImage background, final AttributedString attributedWord) throws CaptchaException { int x = (background.getWidth()) / 20; int bgWidth=background.getHeight(); int y = bgWidth-(bgWidth/5); //创建一个与背景图片同尺寸的图层 BufferedImage out = copyBackground(background); Graphics2D g2 = pasteBackgroundAndSetTextColor(out, background); //pie.drawString(attributedWord.getIterator(), x, y); //pie.dispose(); // convert string into a series of glyphs we can work with ChangeableAttributedString newAttrString = new ChangeableAttributedString(g2, attributedWord, 2); // space out the glyphs with a little kerning newAttrString.useMinimumSpacing(1); //newAttrString.useMonospacing(0); // shift string to a random spot in the output imge newAttrString.moveTo(x, y); // now draw each glyph at the appropriate spot on the image. if (isManageColorPerGlyph()) newAttrString.drawString(g2, getColorGenerator()); else newAttrString.drawString(g2); g2.dispose(); return out; } }
- 继承jcaptcha4struts2的JCaptchaImageAction
package com.taobao.action; import com.google.code.jcaptcha4struts2.core.actions.JCaptchaImageAction; import com.google.code.jcaptcha4struts2.core.beans.JC4S2Config; import com.octo.captcha.service.captchastore.FastHashMapCaptchaStore; import com.octo.captcha.service.image.DefaultManageableImageCaptchaService; import com.taobao.utils.MySimpleWaterCaptchaEngine; public class MyJcaptchaAction extends JCaptchaImageAction { /** * */ private static final long serialVersionUID = 1340725347539598783L; private static MySimpleWaterCaptchaEngine imageCaptchaEngine = null; private static FastHashMapCaptchaStore fastHashMapCaptchaStore = null; private static DefaultManageableImageCaptchaService defaultManageableImageCaptchaService = null; static { imageCaptchaEngine=new MySimpleWaterCaptchaEngine(); fastHashMapCaptchaStore = new FastHashMapCaptchaStore(); defaultManageableImageCaptchaService=new DefaultManageableImageCaptchaService(fastHashMapCaptchaStore, imageCaptchaEngine, 180, 100000, 75000); JC4S2Config.getInstance().setImageCaptchaService(defaultManageableImageCaptchaService); } @Override public String execute(){ return super.execute(); } }
- 配置页面action
<package name="ajax-valid" extends="jcaptcha4struts2-default"> <action name="ajax_captcha" class="com.taobao.action.MyJcaptchaAction"> <interceptor-ref name="jcaptchaDefaultStack" /> <result type="captchaImage" /> </action> </package>
<tr> <td class="label"><label for="verify_code">验证码:</label> </td> <td class="field"> <div class="jcaptcha_image"> <!-- 验证吗 --> <img id="jcaptcha_image" src="ajax_captcha.action" /> </div> <!-- 验证码输入框 --> <input name="jCaptchaResponse" type="text" id="txtVerifyCode" class="yzm_input clear-fix" /> <div class="text_left"> <p class="t1"> <span id="vcodeValidMsg">请输入图片中的七个字母。</span> <span id="number.info" style="color:red"></span> <a href="javascript:changeImage()">看不清楚?换个图片</a> </p> </div> </td> </tr>
效果如下
相关推荐
基于java的开发源码-Struts验证码插件 JCaptcha4Struts2.zip 基于java的开发源码-Struts验证码插件 JCaptcha4Struts2.zip 基于java的开发源码-Struts验证码插件 JCaptcha4Struts2.zip 基于java的开发源码-Struts...
Struts验证码插件JCaptcha4Struts2是一个用于增强Web应用程序安全性的工具,它整合了Jcaptcha库,为Struts2框架提供了强大的验证码功能。在Web应用中,验证码是一种防止恶意自动化程序(如机器人)进行非法操作的...
JCaptcha4Struts2 是一个专门为Struts2框架集成JCaptcha验证码服务的插件。JCaptcha本身是一种强大的、可自定义的图像验证码库,旨在防止自动机器人和恶意软件对网站进行非法操作。在Struts2项目中添加JCaptcha验证...
JAVA源码Struts验证码插件JCaptcha4Struts2
Struts验证码插件JCaptcha4Struts2是一个用于Java Web应用的安全组件,旨在防止自动化的机器人或恶意软件通过表单提交进行欺诈性操作。在Web开发中,验证码(CAPTCHA)是一种常用的方法,用于区分人类用户和计算机...
Struts验证码插件JCaptcha4Struts2是一个用于Java Web应用的安全组件,旨在防止自动机器人或恶意用户通过自动化脚本进行非法操作,如批量注册、恶意登录等。这个插件结合了Struts2框架和JCaptcha库,为Struts2应用...
java资源Struts验证码插件 JCaptcha4Struts2提取方式是百度网盘分享地址
通过以上步骤,你可以利用`JCaptcha4Struts2`在Struts2应用中实现强大的验证码功能,增强应用的安全性。值得注意的是,验证码的使用虽然可以防止自动化的恶意行为,但也可能给部分视力障碍或者认知障碍的用户提供...
将JCaptcha集成到Struts2中,可以轻松实现验证码功能,防止未经授权的表单提交。 在JCaptcha4Struts2这个插件中,主要包含以下几个关键部分: 1. **Action**:这是Struts2中的核心组件,负责处理用户请求。在这个...
Struts验证码插件 JCaptcha4Struts2.7z
1. **源代码**:包含了实现JCaptcha与Struts2框架集成的Java源文件。这些源码可能包括Action类,用于处理用户提交的验证码,以及验证用户输入是否与生成的验证码匹配的逻辑。 2. **配置文件**:可能包含`struts.xml...
《Struts2环境下的Jcaptcha4Struts2验证码组件详解》 在Web开发中,验证码是一种常见的安全机制,用于防止自动化的恶意攻击,如机器人填写表单或进行非法操作。Struts2作为Java Web开发中广泛应用的MVC框架,与...
2. **创建Action类**: Struts1.0中的每个业务操作对应一个Action类。这个类需要继承自`org.apache.struts.action.Action`,并覆盖`execute`方法。当用户请求被Struts接收后,它会调用Action类的`execute`方法,根据...
Jcaptcha,全称为Java CAPTCHA,是一款用Java语言实现的开源验证码库,它为开发者提供了创建强大且安全的图像验证码的工具。本文将深入探讨Jcaptcha 1.0源码,揭示其内部工作机制,以及如何利用这些源码进行自定义...
在本案例"Struts2之struts2自带插件实现异步调用案例struts015"中,我们将探讨如何利用Struts2的插件机制和AJAX技术来实现异步数据交互。 首先,我们要了解AJAX(Asynchronous JavaScript and XML)的核心概念,它...
Struts2 和 Spring 的整合是Java Web开发中的常见实践,这两种框架的结合可以极大地提高应用的可维护性和灵活性。在本文中,我们将深入探讨如何实现Struts2与Spring的集成,以及利用拦截器来增强应用的功能。 首先...
Struts1.0和Struts2是两种不同的Java EE Web应用程序框架,虽然它们都属于Apache Struts项目,但在设计和实现上有显著的区别。Struts2是基于WebWork框架的升级版,它吸取了Struts1的优点并进行了诸多改进,提高了...
### Struts2整合jQuery实现Ajax功能 #### 一、引言 随着Web应用程序的发展,用户对交互性和响应速度的要求越来越高。传统的Web表单提交方式已无法满足这些需求,因此Ajax技术逐渐成为提升用户体验的重要手段之一。...
maven-struts-plugin-1.0.jar