kaptcha 验证码组件结合springMVC示例

1 篇文章 0 订阅
1 篇文章 0 订阅

      kaptcha 是一个非常实用的验证码生成工具。有了它,你可以生成各种样式的验证码,因为它是可配置的。kaptcha工作的原理是调用 com.google.code.kaptcha.servlet.KaptchaServlet,生成一个图片。同时将生成的验证码字符串放到 HttpSession中。

    1.在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">no</prop>  
	                    <prop key="kaptcha.border.color">105,179,90</prop>  
	                    <prop key="kaptcha.textproducer.font.color">red</prop>  
	                    <prop key="kaptcha.image.width">80</prop>  
	                    <prop key="kaptcha.textproducer.font.size">30</prop>  
	                    <prop key="kaptcha.image.height">30</prop>  
	                    <prop key="kaptcha.session.key">code</prop>  
	                    <prop key="kaptcha.textproducer.char.length">4</prop>  
	                    <prop key="kaptcha.textproducer.font.names">Arial, Courier</prop> 
	                    <prop key="kaptcha.GimpyEngine">WaterRipple</prop> 
	                    <prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoise</prop>
	                </props>  
	            </constructor-arg>  
	        </bean>  
	    </property>  
	</bean>

 2.新建图片控制类CaptchaController.java

 

 

package com.ly.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 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;

@Controller
@RequestMapping("/captchaController")
public class CaptchaController extends BaseController {

	@Autowired
	private Producer captchaProducer = null;

	@RequestMapping("/image")
	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);
		response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		response.addHeader("Cache-Control", "post-check=0, pre-check=0");
		response.setHeader("Pragma", "no-cache");
		response.setContentType("image/jpeg");
		String capText = captchaProducer.createText();
		session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
		BufferedImage bi = captchaProducer.createImage(capText);
		ServletOutputStream out = response.getOutputStream();
		ImageIO.write(bi, "jpg", out);
		try {
			out.flush();
		} finally {
			out.close();
		}
		return null;
	}

}

 3.前台登录页面.jsp

<div id="ck">
	验证码&nbsp;<input type="text" id="kaptcha" name="kaptcha" style="width: 60px;"  class="easyui-validatebox"  maxlength="4" data-options="required:true"/>
	&nbsp;<img id="kaptchaImage" src="captchaController/image"  style="margin-bottom: -10px"/>
</div>
<script type="text/javascript">
	$('#kaptchaImage').click(function () {//生成验证码  
	      $(this).hide().attr('src', 'captchaController/image?' + Math.floor(Math.random()*100) ).fadeIn();  
	      event.cancelBubble=true;  
	});
</script>

 4.验证登录什么的功能,自己发挥吧!

 

以下另送配置属性参考值:

        <servlet>  
            <servlet-name>Kaptcha</servlet-name>  
            <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>  
            <init-param>  
                <description> Border around kaptcha. Legal values are yes or no. </description>  
                <param-name>kaptcha.border</param-name>  
                <param-value>no</param-value>  
            </init-param>  
            <init-param>  
                <description>Color of the border. Legal values are r,g,b (and optional alpha) or white,black,blue. </description>  
                <param-name>kaptcha.border.color</param-name>  
                <param-value>red</param-value>  
            </init-param>  
            <init-param>  
                <description>Thickness of the border around kaptcha. Legal values are > 0. </description>  
                <param-name>kaptcha.border.thickness</param-name>  
                <param-value>5</param-value>  
            </init-param>  
            <init-param>  
                <description>Width in pixels of the kaptcha image. </description>  
                <param-name>kaptcha.image.width</param-name>  
                <param-value>80</param-value>  
            </init-param>  
            <init-param>  
                <description>Height in pixels of the kaptcha image. </description>  
                <param-name>kaptcha.image.height</param-name>  
                <param-value>40</param-value>  
            </init-param>  
            <init-param>  
                <description>The image producer. </description>  
                <param-name>kaptcha.producer.impl</param-name>  
                <param-value>com.google.code.kaptcha.impl.DefaultKaptcha </param-value>  
            </init-param>  
            <init-param>  
                <description>The text producer. </description>  
                <param-name>kaptcha.textproducer.impl</param-name>  
                <param-value>com.google.code.kaptcha.text.impl.DefaultTextCreator</param-value>   
            </init-param>  
            <init-param>  
                <description>The characters that will create the kaptcha. </description>  
                <param-name>kaptcha.textproducer.char.string</param-name>  
                <param-value>abcde2345678gfynmnpwx </param-value>  
            </init-param>  
            <init-param>  
                <description>The number of characters to display. </description>  
                <param-name>kaptcha.textproducer.char.length</param-name>  
                <param-value>5</param-value>  
            </init-param>  
            <init-param>  
                <description>A list of comma separated font names.</description>  
                <param-name>kaptcha.textproducer.font.names</param-name>  
                <param-value>Arial, Courier</param-value>  
            </init-param>  
            <init-param>  
                <description>The size of the font to use. </description>  
                <param-name>kaptcha.textproducer.font.size</param-name>  
                <param-value>23</param-value>  
            </init-param>  
            <init-param>  
                <description>The color to use for the font. Legal values are r,g,b. </description>  
                <param-name>kaptcha.textproducer.font.color</param-name>  
                <param-value>black</param-value>  
            </init-param>  
            <init-param>  
                <description>The noise producer. </description>  
                <param-name>kaptcha.noise.impl</param-name>  
                <param-value>com.google.code.kaptcha.impl.NoNoise </param-value>  
            </init-param>  
            <init-param>  
                <description>The noise color. Legal values are r,g,b. </description>  
                 <param-name>kaptcha.noise.color</param-name>  
                 <param-value>black</param-value>  
            </init-param>  
            <init-param>  
                <description>The obscurificator implementation. </description>  
                <param-name>kaptcha.obscurificator.impl</param-name>  
                <param-value>com.google.code.kaptcha.impl.ShadowGimpy</param-value>  
            </init-param>  
            <init-param>  
                <description>The background implementation. </description>  
                <param-name>kaptcha.background.impl</param-name>  
                <param-value>com.google.code.kaptcha.impl.DefaultBackground</param-value>  
            </init-param>  
            <init-param>  
                <description>Ending background color. Legal values are r,g,b. </description>  
                <param-name>kaptcha.background.clear.to</param-name>  
                <param-value>white</param-value>  
             </init-param>  
            <init-param>  
                <description>The word renderer implementation. </description>  
                <param-name>kaptcha.word.impl</param-name>  
                <param-value>com.google.code.kaptcha.text.impl.DefaultWordRenderer</param-value>  
            </init-param>  
            <init-param>  
                <description>The value for the kaptcha is generated and is put into the HttpSession. This is the key value for that item in the session. </description>  
                <param-name>kaptcha.session.key</param-name>  
                <param-value>KAPTCHA_SESSION_KEY</param-value>  
            </init-param>  
            <init-param>  
                <description>The date the kaptcha is generated is put into the HttpSession. This is the key value for that item in the session. </description>  
                <param-name>kaptcha.session.date</param-name>  
                <param-value>KAPTCHA_SESSION_DATE</param-value>  
            </init-param>  
        </servlet>  

 

CCF大数据与计算智能大赛-面向电信行业存量用户的智能套餐个性化匹配模型联通赛-复赛第二名-【多分类,embedding】.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计,皆可应用在项目、毕业设计、课程设计、期末/期/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值