Spring MVC uses Kaptcha to generate verification codes

Source: Internet
Author: User
Tags border color constructor generator
1.spring configuration file Applicationcontext.xml

<bean id= "Captchaproducer" class= "Com.google.code.kaptcha.impl.DefaultKaptcha" >
		<property name= " Config ">
			<bean class=" Com.google.code.kaptcha.util.Config ">
				<constructor-arg>
					< props>
						<prop key= "Kaptcha.border" >yes</prop>
						<prop key= "Kaptcha.border.color" > 105,179,90</prop>
						<prop key= "Kaptcha.textproducer.font.color" >blue</prop>
						<prop Key = "Kaptcha.image.width" >125</prop>
						<prop key= "Kaptcha.image.height" >45</prop>
						< Prop key= "Kaptcha.textproducer.font.size" >45</prop>
						<prop key= "Kaptcha.session.key" >code< /prop>
						<prop key= "kaptcha.textproducer.char.length" >4</prop>
						<prop key= " Kaptcha.textproducer.font.names "> Arial, Italic, Microsoft Black </prop>
					</props>
				</constructor-arg>
			</bean>
		</property>
</bean>

2. Controller implementation

Package Com.vopzoon.app.base.captcha;

Import Java.awt.image.BufferedImage;
Import Javax.imageio.ImageIO;
Import Javax.servlet.ServletOutputStream;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import javax.servlet.http.HttpSession;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.servlet.ModelAndView;
Import com.google.code.kaptcha.Constants;

Import Com.google.code.kaptcha.Producer; 
	
	/** * Prevent captcha bots from landing * @author Liuwang * * */@Controller @RequestMapping ("/kaptcha/*") public class Captchacontroller {

	@Autowired private Producer captchaproducer = null; @RequestMapping public Modelandview getkaptchaimage (httpservletrequest request, httpservletresponse response) throws
		Exception {HttpSession session = Request.getsession (); String code = (string) session.getattribute (CoNstants.
		Kaptcha_session_key);
		
		System.out.println ("****************** Verification Code is:" + code + "******************");
		
		Response.setdateheader ("Expires", 0);
		Set standard http/1.1 No-cache headers.
		
		Response.setheader ("Cache-control", "No-store, No-cache, must-revalidate");
		Set IE Extended http/1.1 no-cache headers (use AddHeader).
		
		Response.AddHeader ("Cache-control", "post-check=0, pre-check=0");
		Set Standard http/1.0 No-cache header.
		
		Response.setheader ("Pragma", "No-cache");
		
		return a JPEG response.setcontenttype ("image/jpeg");
		
		Create the text for the image String Captext = Captchaproducer.createtext ();
		
		Store the text in the session session.setattribute (Constants.kaptcha_session_key, Captext);
		Create the image with the text bufferedimage bi = captchaproducer.createimage (captext);
		
		Servletoutputstream out = Response.getoutputstream ();
		Write the data out Imageio.write (bi, "JPG", out);
		try {out.flush (); } Finally {out.close ();
	} return null; }

}
3. JSP Code

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >  
check code obtained in 4.controller 

String kaptchaexpected = (string) request.getsession (). getattribute (com.google.code.kaptcha.Constants.KAPTCHA_ Session_key);
5.kaptcha Configurable Items

Kaptcha.border whether there is a border default to True we can set the Yes,no kaptcha.border.color border color by default to Color.Black kaptcha.border.thickness border thickness The default is 1 Kaptcha.producer.impl captcha generator default to Defaultkaptcha kaptcha.textproducer.impl captcha text Generator default to Defaulttextcreator kapt Cha.textproducer.char.string Verification Code text character content range default to abcde2345678gfynmnpwx kaptcha.textproducer.char.length captcha text character length default to 5 Ka Ptcha.textproducer.font.names captcha text font style defaults to new font ("Arial", 1, fontSize), New Font ("Courier", 1, FontSize) kaptcha.te Xtproducer.font.size captcha text character size defaults to Kaptcha.textproducer.font.color captcha text character color default to Color.Black kaptcha.textproducer.c 
Har.space captcha text character spacing defaults to 2 Kaptcha.noise.impl Authenticode Noise Generation object default to Defaultnoise kaptcha.noise.color captcha noise color default to Color.Black Kaptcha.obscurificator.impl Verification Code style engine defaults to waterripple Kaptcha.word.impl captcha text character rendering defaults to Defaultwordrenderer Kaptcha.back Ground.impl Verification Code Background generator defaults to defaultbackground kaptcha.background.clear.from captcha background color progressive default to Color.light_gray Kaptcha.back Ground.clear.to Verification Code Background colorProgressive default is Color.White kaptcha.image.width captcha picture width default is 50 kaptcha.image.height captcha picture Height default  

Reprint Address: http://blog.csdn.net/rambo_china/article/details/7720181

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.