验证码例子(jcaptcha插件的使用)

 

 

和上一篇验证码的例子(kaptcha插件的使用)类似,同样加进了spring mvc、jquery

 

 

项目目录结构:


验证码例子(jcaptcha插件的使用)_第1张图片

 

 

pom.xml

 


  4.0.0

  com.royal
  verifyCode_jcaptcha
  0.0.1-SNAPSHOT
  jar

  verifyCode_jcaptcha
  http://maven.apache.org

  
    UTF-8
  

  
    
      junit
      junit
      3.8.1
      test
    
    
    
     
      com.octo.captcha
      jcaptcha-all
      1.0-RC6
    
    
     
     
		commons-logging
		commons-logging
		1.1.1
	
	
		org.springframework
		spring-aop
		3.1.0.RELEASE
	
	
		org.springframework
		spring-asm
		3.1.0.RELEASE
	
	
		org.springframework
		spring-aspects
		3.1.0.RELEASE
	
	
		org.springframework
		spring-beans
		3.1.0.RELEASE
	
	
		org.springframework
		spring-context
		3.1.0.RELEASE
	
	
		org.springframework
		spring-context-support
		3.1.0.RELEASE
	
	
		org.springframework
		spring-core
		3.1.0.RELEASE
	
	
		org.springframework
		spring-expression
		3.1.0.RELEASE
	
	
		org.springframework
		spring-instrument
		3.1.0.RELEASE
	
	
		org.springframework
		spring-jdbc
		3.1.0.RELEASE
	
	
		org.springframework
		spring-jms
		3.1.0.RELEASE
	
	
		org.springframework
		spring-orm
		3.1.0.RELEASE
	
	
		org.springframework
		spring-oxm
		3.1.0.RELEASE
	
	
		org.springframework
		spring-test
		3.1.0.RELEASE
	
	
		org.springframework
		spring-tx
		3.1.0.RELEASE
	
	
		org.springframework
		spring-web
		3.1.0.RELEASE
	
	 
		org.springframework
		spring-webmvc
		3.1.0.RELEASE
	
	
		org.springframework
		spring-webmvc-portlet
		3.1.0.RELEASE  
	
	
		org.apache.tomcat
		jsp-api
		6.0.35  
	
  

 

 

web.xml

 



	
    
        jcaptcha
        com.royal.servlet.ImageCaptchaServlet
        0
    
    
    
        mvc-dispatcher
        org.springframework.web.servlet.DispatcherServlet
        1
        
            contextConfigLocation
            /WEB-INF/spring-mvc.xml
        
    
    
    
      
        default  
        *.css  
      
      
        default  
        *.gif  
      
      
        default  
        *.jpg  
      
      
        default  
        *.js  
      
    
    
    
	
	        jcaptcha
	        /jcaptcha
	
	
	
        mvc-dispatcher
        /
    
	
  
    index.jsp
  
  
 

 

spring-mvc.xml

 


        
    
    
    

    
        
        
    
    
 

 

CaptchaServiceSingleton.java

 

package com.royal.verifyCode_jcaptcha;

import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
import com.octo.captcha.service.image.ImageCaptchaService;

/**
 * 实例一个jcaptcha服务,注意,必须是单例模式
 */
public class CaptchaServiceSingleton {
	
	private static ImageCaptchaService instance = new DefaultManageableImageCaptchaService();
	
	public static ImageCaptchaService getInstance(){
		return instance;
	}

}

 

 

ImageCaptchaServlet.java

 

package com.royal.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 com.royal.verifyCode_jcaptcha.CaptchaServiceSingleton;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * 一个产生图片的servlet
 */
public class ImageCaptchaServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		byte[] captchaChallengeAsJpeg = null;
		//the output stream to render the captcha image as jpeg into
		ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
		//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 = req.getSession().getId();
		//call the ImageCaptchaService getChallenge method
		BufferedImage challenge = CaptchaServiceSingleton.getInstance().getImageChallengeForID(captchaId, req.getLocale());
		//a jpeg encoder
		JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(jpegOutputStream);
		jpegEncoder.encode(challenge);
		
		captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
		
		// flush it in the response
		resp.setHeader("Cache-Control", "no-store");
		resp.setHeader("Pragma", "no-cache");
		resp.setDateHeader("Expires", 0);
		resp.setContentType("image/jpeg");
		
		ServletOutputStream responseOutputStream = resp.getOutputStream();
		responseOutputStream.write(captchaChallengeAsJpeg);
		responseOutputStream.flush();
        responseOutputStream.close();
	}
	
}

 

也就是当你点击图片进行刷新的时候,访问的就是这个servlet(用于生成验证码图片)

 

这里可能有个小问题:

 

当你要用 JPEGImageEncoder 和 JPEGCodec,你会发现无法导入rt.jar里面的 com.sun.image.codec.jpeg.JPEGCodec

com.sun.image.codec.jpeg.JPEGImageEncoder

这两个类

 

解决办法:deprecated and restricted API--- Forbidden 选项改为warning


验证码例子(jcaptcha插件的使用)_第2张图片

 

参考:http://blog.csdn.net/oney139/article/details/6539624

 

 

mvc 控制类

 

VerifyJcaptchaController.java

 

package com.royal.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.royal.verifyCode_jcaptcha.CaptchaServiceSingleton;


public class VerifyJcaptchaController extends AbstractController{

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Boolean isResponseCorrect = Boolean.FALSE;
		//remenber that we need an id to validate!
		String captchaId = request.getSession().getId();
		System.out.println("captchaId---" + captchaId);
		
		 //retrieve the response
		String resp = request.getParameter("j_captcha_response");
		System.out.println("j_captcha_response---" + resp);
		
		// Call the Service method
		isResponseCorrect = CaptchaServiceSingleton.getInstance().validateResponseForID(captchaId, resp);
		System.out.println("isResponseCorrect---" + isResponseCorrect);
		
		if(isResponseCorrect == true){
			return new ModelAndView("success");
		}else{
			System.out.println("FAIL..");
			return null;
		}
	}

}
 

同样别忘了maven build...


验证码例子(jcaptcha插件的使用)_第3张图片

 

 

启动tomcat,访问:http://localhost:8080/verifyCode_jcaptcha/


验证码例子(jcaptcha插件的使用)_第4张图片
 
验证码例子(jcaptcha插件的使用)_第5张图片

 

 

demo算是demo完了,但总觉得变扭,图片好像不怎么好看,图片大小不知道怎么改。API没看。

 

官方:http://jcaptcha.sourceforge.net/

 

 

千篇一律的这个不完整例子 参考:http://www.blogjava.net/alex/archive/2006/06/11/51976.html

 

你可能感兴趣的:(java)