- 浏览: 361127 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
mylaughing:
请问你的getCount是怎么处理的呢?
hibernate 使用SQL 分页写法 -
evening_xxxy:
[flash=200,200][url][img][list] ...
jquery 简单2级联动 -
wangcl011:
多谢楼主,参考后个人实现实例代码:http://www.itd ...
extjs的ComboBox 2级联动 -
ideaf:
secondeForm
引用引用引用引用引用引用
extjs的ComboBox 2级联动 -
gxdr:
你好,看了您的博客很有收获,但是在操作中遇到了一些问题,我想把 ...
tsung压力测试多服务器(跨域)之间发送消息和状态变更
继续,上篇jcaptcha学习,现在项目中用SPRING 比较多所以整合了一下。其中的部分代码是参考一个jeecms项目的,讲其中的jcaptcha验证码这块剥离出来。
项目在上篇基础上编写的,部分代码是上篇中的代码(偷懒了)
1.用到得JAR
commons-logging.jar,jcaptcha-all-1.0-RC6.jar,spring-beans-2.5.6.jar
spring-context-2.5.6.jar,spring-core-2.5.6.jar,spring-web-2.5.6.jar
2.applicationContext.xml
3. web.xml
4.java 代码
自定义生成代码部分
CaptchaEngineEx.java
生成图片的servlet
ImageCaptchaServlet.JAVA
验证的单列类 CaptchaService.java
最后测试验证的类
ValidationServlet.java
5 页面HTML
login2.html
6 最后调用成功失败的页面success.html failture.html(略)
最后发布程序,启动TOMCAT 输入
http://localhost:8081/jcaptcha/login2.html
看到生成验证码了
项目在上篇基础上编写的,部分代码是上篇中的代码(偷懒了)
1.用到得JAR
commons-logging.jar,jcaptcha-all-1.0-RC6.jar,spring-beans-2.5.6.jar
spring-context-2.5.6.jar,spring-core-2.5.6.jar,spring-web-2.5.6.jar
2.applicationContext.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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" > <!--验证码生成器--> <bean id="imageCaptchaService" class="com.spring.CaptchaService"> <constructor-arg type="com.octo.captcha.service.captchastore.CaptchaStore" index="0"> <ref bean="fastHashMapCaptchaStore"/> </constructor-arg> <!--which captcha Engine you use--> <constructor-arg type="com.octo.captcha.engine.CaptchaEngine" index="1"> <ref bean="captchaEngineEx"/> </constructor-arg> <constructor-arg index="2"> <value>180</value> </constructor-arg> <constructor-arg index="3"> <value>100000</value> </constructor-arg> <constructor-arg index="4"> <value>75000</value> </constructor-arg> </bean> <bean id="fastHashMapCaptchaStore" class="com.octo.captcha.service.captchastore.FastHashMapCaptchaStore"/> <!--you can define more than one captcha engine here --> <bean id="captchaEngineEx" class="com.spring.CaptchaEngineEx"/> </beans>
3. web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- <servlet> <servlet-name>jcaptcha</servlet-name> <servlet-class>com.code.ImageCaptchaServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jcaptcha</servlet-name> <url-pattern>/jcaptcha</url-pattern> </servlet-mapping> --> <servlet> <servlet-name>jcaptcha2</servlet-name> <servlet-class>com.spring.ImageCaptchaServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jcaptcha2</servlet-name> <url-pattern>/jcaptcha</url-pattern> </servlet-mapping> <!-- <servlet> <servlet-name>checkjcaptcha</servlet-name> <servlet-class>com.code.ValidationServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>checkjcaptcha</servlet-name> <url-pattern>/validateAction</url-pattern> </servlet-mapping> --> <servlet> <servlet-name>checkjcaptcha2</servlet-name> <servlet-class>com.spring.ValidationServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>checkjcaptcha2</servlet-name> <url-pattern>/validateAction</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
4.java 代码
自定义生成代码部分
CaptchaEngineEx.java
package com.spring; import java.awt.Color; import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator; import com.octo.captcha.component.image.backgroundgenerator.GradientBackgroundGenerator; import com.octo.captcha.component.image.color.SingleColorGenerator; import com.octo.captcha.component.image.fontgenerator.FontGenerator; import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator; import com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster; import com.octo.captcha.component.image.textpaster.TextPaster; import com.octo.captcha.component.image.textpaster.textdecorator.BaffleTextDecorator; import com.octo.captcha.component.image.textpaster.textdecorator.LineTextDecorator; import com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator; import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage; import com.octo.captcha.component.image.wordtoimage.WordToImage; import com.octo.captcha.component.word.wordgenerator.RandomWordGenerator; import com.octo.captcha.component.word.wordgenerator.WordGenerator; import com.octo.captcha.engine.image.ListImageCaptchaEngine; import com.octo.captcha.image.gimpy.GimpyFactory; /*********************************************************************** * * CaptchaEngineEx.java * @copyright Copyright: 2009-2012 * @creator 周辉<br/> * @create-time Jun 18, 2009 2:41:12 PM * @revision $Id: * ***********************************************************************/ public class CaptchaEngineEx extends ListImageCaptchaEngine { protected void buildInitialFactories() { // Set Captcha Word Length Limitation which should not over 6 Integer minAcceptedWordLength = new Integer(4); Integer maxAcceptedWordLength = new Integer(5); // Set up Captcha Image Size: Height and Width Integer imageHeight = new Integer(40); Integer imageWidth = new Integer(100); // Set Captcha Font Size Integer minFontSize = new Integer(20); Integer maxFontSize = new Integer(22); // We just generate digit for captcha source char Although you can use // abcdefghijklmnopqrstuvwxyz WordGenerator wordGenerator = new RandomWordGenerator("0123456789"); // cyt and unruledboy proved that backgroup not a factor of Security. A // captcha attacker won't affaid colorful backgroud, so we just use // white // color, like google and hotmail. BackgroundGenerator backgroundGenerator = new GradientBackgroundGenerator( imageWidth, imageHeight, Color.white, Color.white); // font is not helpful for security but it really increase difficultness // for // attacker FontGenerator fontGenerator = new RandomFontGenerator(minFontSize, maxFontSize); // Note that our captcha color is Blue SingleColorGenerator scg = new SingleColorGenerator(Color.blue); // decorator is very useful pretend captcha attack. we use two line text // decorators. LineTextDecorator lineDecorator = new LineTextDecorator(1, Color.blue); // LineTextDecorator line_decorator2 = new LineTextDecorator(1, // Color.blue); TextDecorator[] textdecorators = new TextDecorator[1]; textdecorators[0] = lineDecorator; // textdecorators[1] = line_decorator2; TextPaster textPaster = new DecoratedRandomTextPaster( minAcceptedWordLength, maxAcceptedWordLength, scg, new TextDecorator[] { new BaffleTextDecorator(new Integer(1), Color.white) }); // ok, generate the WordToImage Object for logon service to use. WordToImage wordToImage = new ComposedWordToImage(fontGenerator, backgroundGenerator, textPaster); addFactory(new GimpyFactory(wordGenerator, wordToImage)); } }
生成图片的servlet
ImageCaptchaServlet.JAVA
package com.spring; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.octo.captcha.service.CaptchaServiceException; import com.octo.captcha.service.image.ImageCaptchaService; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; /*********************************************************************** * * ImageCaptchaServlet.java * @copyright Copyright: 2009-2012 * @creator 周辉<br/> * @create-time Jun 18, 2009 2:44:15 PM * @revision $Id: * ***********************************************************************/ @SuppressWarnings("serial") public class ImageCaptchaServlet extends HttpServlet { private ImageCaptchaService imageCaptchaService; private String beanName = "imageCaptchaService"; public void init(ServletConfig servletConfig) throws ServletException { super.init(servletConfig); WebApplicationContext wac = WebApplicationContextUtils .getRequiredWebApplicationContext(servletConfig .getServletContext()); imageCaptchaService = (ImageCaptchaService) wac.getBean(beanName, ImageCaptchaService.class); } protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { byte[] captchaChallengeAsJpeg = null; // the output stream to render the captcha image as jpeg into ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream(); try { // get the session id that will identify the generated captcha. // the same id must be used to validate the response, the session id // is a good candidate! String captchaId = httpServletRequest.getSession().getId(); // call the ImageCaptchaService getChallenge method BufferedImage challenge = imageCaptchaService .getImageChallengeForID(captchaId, httpServletRequest .getLocale()); // a jpeg encoder JPEGImageEncoder jpegEncoder = JPEGCodec .createJPEGEncoder(jpegOutputStream); jpegEncoder.encode(challenge); } catch (IllegalArgumentException e) { httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND); return; } catch (CaptchaServiceException e) { httpServletResponse .sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } captchaChallengeAsJpeg = jpegOutputStream.toByteArray(); // flush it in the response 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(); } }
验证的单列类 CaptchaService.java
package com.spring; import com.octo.captcha.engine.CaptchaEngine; import com.octo.captcha.service.captchastore.CaptchaStore; import com.octo.captcha.service.image.DefaultManageableImageCaptchaService; /*********************************************************************** * * CaptchaService.java * @copyright Copyright: 2009-2012 * @creator 周辉<br/> * @create-time Jun 18, 2009 2:42:53 PM * @revision $Id: * ***********************************************************************/ public class CaptchaService extends DefaultManageableImageCaptchaService { public CaptchaService() { super(); } public CaptchaService(int minSeconds, int maxStoreSize, int loadBefore) { super(minSeconds, maxStoreSize, loadBefore); } public CaptchaService(CaptchaStore captchaStore, CaptchaEngine captchaEngine, int minSeconds, int maxStoreSize, int loadBefore) { super(captchaStore, captchaEngine, minSeconds, maxStoreSize, loadBefore); } public Boolean validateResponseForID(String ID, Object response) { Boolean isHuman; try { isHuman = super.validateResponseForID(ID, response); } catch (Exception e) { isHuman = false; } return isHuman; } }
最后测试验证的类
ValidationServlet.java
package com.spring; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.octo.captcha.service.CaptchaServiceException; import com.octo.captcha.service.image.ImageCaptchaService; /*********************************************************************** * * ValidationServlet.java * @copyright Copyright: 2009-2012 * @creator 周辉<br/> * @create-time Jun 18, 2009 3:16:50 PM * @revision $Id: * ***********************************************************************/ public class ValidationServlet extends HttpServlet { private ImageCaptchaService imageCaptchaService; private String beanName = "imageCaptchaService"; public void init(ServletConfig servletConfig) throws ServletException { super.init(servletConfig); WebApplicationContext wac = WebApplicationContextUtils .getRequiredWebApplicationContext(servletConfig .getServletContext()); imageCaptchaService = (ImageCaptchaService) wac.getBean(beanName, ImageCaptchaService.class); } protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { Boolean isResponseCorrect = Boolean.FALSE; //remenber that we need an id to validate! String captchaId = httpServletRequest.getSession().getId(); //retrieve the response String response = httpServletRequest.getParameter("j_captcha_response"); // Call the Service method try { isResponseCorrect=imageCaptchaService.validateResponseForID(captchaId, response); } catch (CaptchaServiceException e) { //should not happen, may be thrown if the id is not valid } System.out.println(isResponseCorrect); // httpServletResponse.encodeUrl("sucess.html"); if(isResponseCorrect.booleanValue()){ httpServletResponse.sendRedirect("success.html"); } else { httpServletResponse.sendRedirect("failture.html"); } } }
5 页面HTML
login2.html
<html> <head> <title>MyHtml.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <form name="f1" id="f1" action="/jcaptcha/validateAction" method="get"> <table border="0"> <tr> <td>验证码:</td> <td><img src="jcaptcha"><input type='text' name='j_captcha_response' value=''></td> </tr> <tr> <td colspan="2" align="center"><input type="submit"></td> </tr> </table> </form> </body> </html>
6 最后调用成功失败的页面success.html failture.html(略)
最后发布程序,启动TOMCAT 输入
http://localhost:8081/jcaptcha/login2.html
看到生成验证码了
- jcaptcha_spring.rar (2.3 MB)
- 下载次数: 285
发表评论
-
jquery.validate 使用
2011-06-02 14:05 9580整理了一下jquery.validate 方面的东西,简化了 ... -
jstl escapeXml 属性设置
2011-06-01 17:06 4094最近修改了一个老系统。系统用STURTS1 做的 服务端A ... -
Tomcat Manager登录的用户名、密码及用户角色配置
2011-01-11 09:03 6585配置文件位置:$TOMCAT_ROOT\conf\tomc ... -
jquery 简单2级联动
2010-12-28 09:20 18478复习了一下JQUERY,做 ... -
简单JAVA版本淘宝客程序上线
2010-12-15 15:48 5276很久没更新博客了,最近抽了个时间写了一个淘宝客程序,利用了 ... -
dwr 实现2级联动
2010-06-29 13:41 1912整理了一下之前写的一写代码,之前写的DWR 实现下拉控件 ... -
java判断某个进程是否在启用
2009-08-04 15:15 2550前段时间测试的时候发现客户端,双击图标多次回生成多次XX ... -
让系统管理数据库序列生成(系统生成代理主键)
2009-07-16 11:18 12741.我们知道oracle 中有sequence做代理主键(自动 ... -
jcaptcha学习
2009-06-18 14:35 1430最近学习了一下验证码实现,学习了一下jcaptcha开源验证技 ...
相关推荐
在"通过spring整合jcaptcha来处理验证码"这个主题中,我们需要理解以下几个关键知识点: 1. **jCaptcha介绍**: jCaptcha是一个基于Java的验证码解决方案,它能够生成包括字母、数字、甚至是图片元素在内的复杂...
springsecurity3和JCaptcha的整合 验证码
在Web开发中,Spring框架是广泛应用的Java后端开发框架,而Jcaptcha则是一个基于Java的验证码库,它能够与Spring集成,提供图像验证码服务。 Spring框架提供了强大的依赖注入(DI)和面向切面编程(AOP)功能,使得...
3. **Java实现**:JCaptcha是用Java语言开发的,因此它与Java应用程序和Web应用框架(如Spring、Struts等)有很好的兼容性,可以方便地集成到Java项目中。 4. **图像生成**:JCaptcha的核心功能是生成复杂的图像...
`integrations`可能包含了与Spring、Struts等流行框架的整合示例。 要在基于`Acegi`的安全框架中使用`jCaptcha`,首先需要将`jcaptcha-1.0-all.jar`添加到项目类路径中,然后配置`Acegi`的安全设置以启用验证码验证...
5. 与Web框架的集成:jCaptcha可以很容易地与各种Web应用框架(如Spring、Struts等)结合,提供便捷的API和标签库,便于在Servlet中实现验证码的生成和验证。 在实际应用中,使用jCaptcha的步骤大致如下: 1. 引入...
下面将详细介绍如何在Spring MVC中集成Jcaptcha,并讨论其优缺点。 首先,我们需要理解验证码的基本工作原理。验证码通常由一组随机字符组成,这些字符可能包括数字、字母甚至符号,它们以一种难以被计算机程序识别...
在实际应用中,JCAPTCHA可以轻松集成到各种Java Web框架中,如Spring、Struts等。对于导入后的使用,开发者需要按照JCAPTCHA的文档进行配置,这可能涉及到Maven或Gradle的依赖管理,以及在Web应用的配置文件中设定...
在Java项目中集成JCaptcha,首先需要将jcaptcha.jar文件添加到项目的类路径中。在Maven项目中,可以通过在pom.xml文件中添加相应的依赖来完成。接着,需要配置JCaptcha的生成器和验证器,这通常在Web应用的初始化...
3. **易于集成**:Jcaptcha提供Java API,可以方便地集成到各种Java Web应用中,支持Servlet容器,如Tomcat、Jetty等。 4. **高性能**:Jcaptcha设计时考虑了性能问题,能够在高并发环境下快速生成验证码,不影响...
4. **Web 应用集成**:在 Spring MVC 或其他 Web 框架中,你可以将 `jCaptcha` 集成到控制器中,处理验证码的生成和验证。这通常涉及到在请求处理方法中生成验证码,在表单提交时验证验证码。 5. **安全存储与清理*...
本文档将详细介绍如何通过整合Spring Security、Jcaptcha和MyBatis框架来构建一个具备安全性验证功能的Web应用。此项目旨在创建一个能够处理用户登录认证、验证码验证等功能的安全系统,并通过MyBatis进行数据持久化...
对于验证码功能,可以集成jCaptcha生成验证码,并在Spring MVC控制器中处理验证逻辑。最后,通过Nginx进行流量管理和负载平衡,以优化整个系统的性能。 在实际开发中,理解Redis的数据结构(如字符串、哈希、列表、...
本篇文章将详细讲解基于Spring、SpringMVC、MyBatis、SpringSecurity、EhCache和JCaptcha这六大组件构建的Web框架。 1. **Spring**: Spring作为整个框架的核心,提供了依赖注入(DI)和面向切面编程(AOP)等特性...
5. **易于集成**:Jcaptcha可以轻松地集成到各种Java Web框架中,如Spring、Struts等。 6. **安全性**:Jcaptcha采用安全的哈希算法来存储和验证验证码,确保每次请求的验证码都是唯一的,并且在使用后立即失效。 ...
8. **Web 应用集成**:在 Spring 或其他 Web 框架中,可以将验证码服务注册为 Spring Bean,然后在控制器中注入并使用。在 JSP 或者其他视图技术中,可以将验证码图像作为响应的一部分发送给客户端。 9. **无障碍性...
5. **易于集成**:通过简单的API调用,开发者可以将JCAPTCHA无缝集成到他们的Web应用中,无论是基于Servlet、Spring MVC还是其他框架。 6. **可配置性**:开发者可以根据安全性、用户体验等需求调整验证码的生成参数...
- Jcaptcha可以与各种Java Web框架(如Spring MVC、Struts2等)无缝集成。例如,在Spring MVC中,可以通过拦截器或控制器方法处理验证码的生成和验证。 9. **可访问性**: - 对于视力障碍的用户,Jcaptcha还支持...
4. **易于集成**:JCaptcha支持多种Web框架,如Servlet、Spring MVC等,方便开发者快速集成到自己的项目中。 ### 二、JCaptcha工作原理 1. **生成验证码**:当用户请求验证码时,JCaptcha会随机生成一个字符串,...