`
smilease
  • 浏览: 85967 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

kaptcha,spring mvc ,easyui 验证码

阅读更多

1、前台网页代码

 

$('#kaptchaImage').click(function () {//生成验证码
	$(this).hide().attr('src', 'CaptchaController.do?image' + Math.floor(Math.random()*100) ).fadeIn();
	event.cancelBubble=true;
});

<tr id="captchaInput" style="display:none">
	<th>验证码</th>
	<td> <input id="kaptcha" name="kaptcha" class="easyui-validatebox"  maxlength="4"/></td>
</tr> 
<div id="captchaDiv" style="display: none">
	<img id="kaptchaImage" src="CaptchaController.do?image"  style="margin-bottom: -3px"/>
</div>
 

 

2、后台控制器

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 com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;

/**
 * 防止Captcha机器人登陆
 * @author
 *
 */
@Controller
@RequestMapping("/CaptchaController")
public class CaptchaController extends BaseController{
	
	@Autowired
	private Producer captchaProducer = null;

	@RequestMapping
	public String getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();
		String code = (String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
		System.out.println("******************验证码是: " + 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、配置文件

<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">200</prop>
					<prop key="kaptcha.textproducer.font.size">75</prop>
					<prop key="kaptcha.image.height">75</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>
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics