浏览 7031 次
锁定老帖子 主题:jcaptcha 之 图片验证码
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2012-01-07
最后修改:2012-01-07
我这边是添加的 jcaptcha源码 ,喜欢看源码的JE朋友可以通过svn https://jcaptcha.svn.sourceforge.net/svnroot/jcaptcha 这里是声音验证码 http://www.iteye.com/topic/1119618 主要提供页面 Index.jsp <html> <head> <title>jcaptcha example</title> </head> <script type="text/javascript" src="<%=path%>/js/jquery/jquery.js"></script> <script type="text/javascript"> function ajaxRefresh(){ //兼容 IE Firefox Safari document.getElementById('image').contentWindow.location.reload(); //$("#image").get(0).src="ImageCaptchaServlet.servlet"; } function ajaxValidateCapt(){ var captchaId ; captchaId = jQuery('#captchaId').val(); jQuery.ajax({ type: "POST", url: "ValidateCaptcha.servlet", data: {"captchaId":captchaId}, success: function(msg){ jQuery('#_msg').html(msg); } }); } </script> <body> <div id="_msg"></div> 请填写验证码:<br/> <iframe width="120" height="70" id="image" src="image.jsp" frameborder="0"></iframe><br> <a href="javascript:void(0)" onclick="ajaxRefresh()">ajaxRefresh</a> <br> <input type='text' name='captchaId' id='captchaId' value=''><br> <input type="button" onclick="ajaxValidateCapt()" value="提交"> </body> </html> image.jsp <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <img width="85" height="41" src="ImageCaptchaServlet.servlet" onclick="ajaxRefresh()" ><br/> </body> </html> web.xml 以下是配置的Servlet <web-app xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 验证码生成 --> <servlet> <javaee:description></javaee:description> <javaee:display-name>ImageCaptchaServlet</javaee:display-name> <servlet-name>ImageCaptchaServlet</servlet-name> <servlet-class>captcha.servlet.ImageCaptchaServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ImageCaptchaServlet</servlet-name> <url-pattern>/ImageCaptchaServlet.servlet</url-pattern> </servlet-mapping> <!-- 验证 验证码 --> <servlet> <javaee:description></javaee:description> <javaee:display-name>ValidateCaptcha</javaee:display-name> <servlet-name>ValidateCaptcha</servlet-name> <servlet-class>captcha.servlet.ValidateCaptchaServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ValidateCaptcha</servlet-name> <url-pattern>/ValidateCaptcha.servlet</url-pattern> </servlet-mapping> </web-app> CaptchaServiceSingleton.java 获取ImageCaptchaService 对象 package captcha; import com.octo.captcha.service.image.DefaultManageableImageCaptchaService; import com.octo.captcha.service.image.ImageCaptchaService; /** * 获取ImageCaptchaService 对象 * 如果是SPRING 可以通过注入进行获取对象 * @author Administrator * */ public class CaptchaServiceSingleton { private static ImageCaptchaService instance = null; private CaptchaServiceSingleton(){ } //单利实现 public static ImageCaptchaService getInstance() { if(null!=instance){ return instance; } return instance = new DefaultManageableImageCaptchaService(); } } ImageCaptchaServlet.java 验证码生成 package captcha.servlet; 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 captcha.CaptchaServiceSingleton; import com.octo.captcha.service.CaptchaServiceException; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class ImageCaptchaServlet extends HttpServlet { /** * simple-servlet-image-sample * @Title: ImageCaptchaServlet.java * @Package captcha.servlet * @Description: TODO * @author liazhou * @date 2012-1-6 下午03:03:54 * @version V1.0 */ private static final long serialVersionUID = -4590982813167593710L; public void init(ServletConfig servletConfig) throws ServletException { super.init(servletConfig); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doPost(request, response); } protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { byte[] captchaChallengeAsJpeg = null; ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream(); try { //相关联的 HttpSession 得到会话id来识别,产生相应的验证码 String captchaId = httpServletRequest.getSession().getId(); // Method to retrive the image challenge corresponding to the given ticket. BufferedImage challenge = CaptchaServiceSingleton.getInstance().getImageChallengeForID(captchaId, httpServletRequest.getLocale()); 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(); 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(); } } ValidateCaptchaServlet.java 验证码的验证 package captcha.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import captcha.CaptchaServiceSingleton; /** * Servlet implementation class ValidateCaptcha */ public class ValidateCaptchaServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ValidateCaptchaServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub boolean bool = false; String message = ""; String captchaId = request.getParameter("captchaId"); if(null!=captchaId && !"".equals(captchaId)){ //通过session id 获取 对应的验证码对象并验证是否正确 bool = CaptchaServiceSingleton.getInstance().validateResponseForID(request.getSession().getId(), captchaId); } if(bool){ message = "验证成功"; }else{ message= "验证失败"; } response.setCharacterEncoding("UTF-8"); response.getWriter().print(message); // request.getRequestDispatcher("").forward(request, response); } } 页面及依赖jar截图 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2012-01-17
其实大家更关注的是如何把验证码解析成字符
|
|
返回顶楼 | |
发表时间:2012-01-18
jiasky 写道 其实大家更关注的是如何把验证码解析成字符
想法不错... |
|
返回顶楼 | |
发表时间:2012-01-19
你这个太容易被人刷了
|
|
返回顶楼 | |
发表时间:2012-01-19
shishi 写道 你这个太容易被人刷了
你可以自定义验证码引擎 需要继承ListImageCaptchaEngine类 将会生成彩色的验证码。 当然,你还可以继承其它的类来生成不同的验证码。 还是看看api 吧 毕竟是开源的.http://jcaptcha.sourceforge.net/ |
|
返回顶楼 | |