jcaptcha验证码实现

maven 依赖

	
				com.octo.captcha
				jcaptcha
				1.0
             
applicationContext.xml
中引入captcha.xml放入config中


captcha.xml




	
		
		
		
		
	
	
		
			
				
			
		
	

	
		
			
		
		
			
		
	

			
		
		
			aabbccddeefgghhkkmnnooppqqsstuuvvwxxyyzz
		
	

	
		
			
		
		
			
		
		
			
		
	

	
		
		
			26
		
		
		
			34
		
		
			
				
					Arial
					0
					32
				
			
		
	
	
		
		
			110
		
		
		
			50
		
	

	
		
		
			4
		
		
		
			4
		
		
		
			
		
		
		
			
				
			
		
		
	
		1
		
	
	
		
			
		
	
	
		
			255
		
		
			255
		
		
			255
		
	
	
		
			50
		
		
			50
		
		
			50
		
	

jcaptchaServelt.java


package cn.qht.common.captcha;


import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
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.beans.factory.BeanFactoryUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.qht.common.Utils.Web.SessionProvider;

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

/**
 * 提供验证码图片的Servlet
 */
@SuppressWarnings("serial")
public class JcaptchaServlet extends HttpServlet {
	public static final String CAPTCHA_IMAGE_FORMAT = "jpeg";

	private ImageCaptchaService captchaService;
	private SessionProvider session;

	@Override
	public void init() throws ServletException {
		WebApplicationContext appCtx = WebApplicationContextUtils
				.getWebApplicationContext(getServletContext());
		captchaService = (ImageCaptchaService) BeanFactoryUtils
				.beanOfTypeIncludingAncestors(appCtx, ImageCaptchaService.class);
		session = (SessionProvider) BeanFactoryUtils
				.beanOfTypeIncludingAncestors(appCtx, SessionProvider.class);
	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) 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 = session.getSessionId(request);
			BufferedImage challenge = captchaService.getImageChallengeForID(
					captchaId, request.getLocale());
			// Jimi.putImage("image/jpeg", challenge, jpegOutputStream);
			ImageIO.write(challenge, CAPTCHA_IMAGE_FORMAT, jpegOutputStream);
		} catch (IllegalArgumentException e) {
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return;
		} catch (CaptchaServiceException e) {
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			return;
		}
		// catch (JimiException e) {
		// response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		// return;
		// }

		captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

		// flush it in the response
		response.setHeader("Cache-Control", "no-store");
		response.setHeader("Pragma", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/" + CAPTCHA_IMAGE_FORMAT);

		ServletOutputStream responseOutputStream = response.getOutputStream();
		responseOutputStream.write(captchaChallengeAsJpeg);
		responseOutputStream.flush();
		responseOutputStream.close();
	}
}


web.xml


  jcaptchaServlet
  cn.qht.common.captcha.JcaptchaServlet


jcaptchaServlet
/captcha.svl

jsp页面


	
	换一张
换一张

提交表单后检验

@RequestMapping(value="/shopping/login.shtml",method=RequestMethod.POST)
	public String login(String username, String password, String captcha,String returnUrl, HttpServletRequest request)
	{
		boolean isResponseCorrect = imageCaptchaService.validateResponseForID(request.getSession().getId(), captcha);
		if (isResponseCorrect) {
			System.out.println("验证码正确");
		}
		return "buyer/login";
	}


你可能感兴趣的:(j2ee)