使用kaptcha生成验证码

听说kaptcha 能被破解
暂时只使用了spring mvc + kaptcha ,servlet暂时没有使用。待续

jar下载地址  http://code.google.com/p/kaptcha/
因为是用maven开发的,在网上找了一下 这个jar的GAV,没有找到,懒得找了,直接放nexus中了。


spring配置文件
<?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:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 	
				http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
				http://www.springframework.org/schema/context 
				http://www.springframework.org/schema/context/spring-context-3.0.xsd
				http://www.springframework.org/schema/aop   
				http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
				http://www.springframework.org/schema/tx 
				http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
				http://www.springframework.org/schema/task  
                http://www.springframework.org/schema/task/spring-task-3.0.xsd">

   <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">no</prop>
						<prop key="kaptcha.border.color">105,179,90</prop>
						<prop key="kaptcha.textproducer.font.color">red</prop>
						<prop key="kaptcha.image.width">250</prop>
						<prop key="kaptcha.textproducer.font.size">90</prop>
						<prop key="kaptcha.image.height">90</prop>
						<prop key="kaptcha.session.key">code</prop>
						<prop key="kaptcha.textproducer.char.length">4</prop>
						<prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑
</prop>
					</props>
				</constructor-arg>
			</bean>
		</property>
	</bean>
</beans>


controller代码

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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;

@Controller
@RequestMapping("/code")
public class CodeController {
	
	private Producer captchaProducer = null;

	@Autowired
	public void setCaptchaProducer(Producer captchaProducer) {
		this.captchaProducer = captchaProducer;
	}

	@RequestMapping("/captcha-image")
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

		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
		request.getSession().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;
	}

}


后台拿到这个验证码的字符串
String sessionCode =(String)request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);



前台调用,引入的jquery
	$(function(){
		$("#imgCode").click(function(){
			$("#imgCode").attr("src","/code/captcha-image?a="+Math.floor(Math.random() * 100));
		})
	})


html
<img id="imgCode" src="/code/captcha-image" width="100" height="40"  />


猜你喜欢

转载自2819272.iteye.com/blog/1994818