kaptcha 验证码在spring mvc 中的使用

kaptcha的配置是通过web.xml完成的,所有的参数都有默认值。

     所需要的jar包:kaptcha-2.0.jar

一、Kaptcha输出验证码的Demo:

二、配置参数(web.xml)

Xml代码   收藏代码
  1. <servlet>  
  2.     <servlet-name>Kaptcha</servlet-name>  
  3.     <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>  
  4.     <init-param>  
  5.         <param-name>kaptcha.border</param-name>  
  6.         <param-value>yes</param-value>  
  7.     </init-param>  
  8. </servlet>  
  9. <servlet-mapping>  
  10.     <servlet-name>Kaptcha</servlet-name>  
  11.     <url-pattern>*.code</url-pattern>  
  12. </servlet-mapping>  

 

三、在页面上的调用

Html代码   收藏代码
  1. <form action="submit.action">  
  2.     <img src="validate.code" /> <input type="text" name="validateCode" value="" />  
  3. </form>  

四、验证码的验证

Java代码   收藏代码
  1. HttpSession session = request.getSession();  
  2. String makeValidateCode = (String) session  
  3.         .getAttribute(Constants.KAPTCHA_SESSION_KEY);  
  4. if (validateCode != null && makeValidateCode != null) {  
  5.     return makeValidateCode.equalsIgnoreCase(validateCode);  
  6. else {  
  7.     return false;  
  8. }  

 五、实现刷新图片

       可以在请求URL后添加随机参数,避免浏览器的缓存。
Html代码   收藏代码
  1. <img src="validate.code" id="kaptchaImage" />  
  2. <script type="text/javascript">  
  3.     $(function(){  
  4.         $('#kaptchaImage').click(function () {   
  5.             $(this).attr('src', 'validate.code?' + Math.floor(Math.random()*100) ); })  
  6.     });  
  7. </script>  
  8. <br /><small>Can't read the image? Click it to get a new one.</small>  

 

六、与spring MVC 的结合 

     在applicationContext.xml中添加
Java代码   收藏代码
  1. <bean id="captchaProducer"  
  2.         class="com.google.code.kaptcha.impl.DefaultKaptcha">  
  3.         <property name="config">  
  4.                 <bean class="com.google.code.kaptcha.util.Config">  
  5.                         <constructor-arg type="java.util.Properties">  
  6.                                 <value>kaptcha.border=yes</value>  
  7.                         </constructor-arg>  
  8.                 </bean>  
  9.         </property>  
  10. </bean>  

 

源码:

Java代码   收藏代码
  1. import java.awt.image.BufferedImage;  
  2.    
  3. import javax.imageio.ImageIO;  
  4. import javax.servlet.ServletOutputStream;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7.    
  8. import org.springframework.beans.factory.annotation.Autowired;  
  9. import org.springframework.stereotype.Controller;  
  10. import org.springframework.web.bind.annotation.RequestMapping;  
  11. import org.springframework.web.servlet.ModelAndView;  
  12.    
  13. import com.google.code.kaptcha.Constants;  
  14. import com.google.code.kaptcha.Producer;  
  15.    
  16. @Controller  
  17. public class CaptchaImageCreateController {  
  18.         private Producer captchaProducer = null;  
  19.    
  20.         @Autowired  
  21.         public void setCaptchaProducer(Producer captchaProducer) {  
  22.                 this.captchaProducer = captchaProducer;  
  23.         }  
  24.    
  25.         @RequestMapping("/captcha-image.html")  
  26.         public ModelAndView handleRequest(  
  27.                         HttpServletRequest request,  
  28.                         HttpServletResponse response) throws Exception {  
  29.                 // Set to expire far in the past.  
  30.                 response.setDateHeader("Expires"0);  
  31.                 // Set standard HTTP/1.1 no-cache headers.  
  32.                 response.setHeader("Cache-Control""no-store, no-cache, must-revalidate");  
  33.                 // Set IE extended HTTP/1.1 no-cache headers (use addHeader).  
  34.                 response.addHeader("Cache-Control""post-check=0, pre-check=0");  
  35.                 // Set standard HTTP/1.0 no-cache header.  
  36.                 response.setHeader("Pragma""no-cache");  
  37.    
  38.                 // return a jpeg  
  39.                 response.setContentType("image/jpeg");  
  40.    
  41.                 // create the text for the image  
  42.                 String capText = captchaProducer.createText();  
  43.    
  44.                 // store the text in the session  
  45.                 request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);  
  46.    
  47.                 // create the image with the text  
  48.                 BufferedImage bi = captchaProducer.createImage(capText);  
  49.    
  50.                 ServletOutputStream out = response.getOutputStream();  
  51.    
  52.                 // write the data out  
  53.                 ImageIO.write(bi, "jpg", out);  
  54.                 try  
  55.                 {  
  56.                         out.flush();  
  57.                 }  
  58.                 finally  
  59.                 {  
  60.                         out.close();  
  61.                 }  
  62.                 return null;  
  63.         }  
  64. }   

 

ConstantDescriptionDefault
kaptcha.borderBorder around kaptcha. Legal values are yes or no.yes
kaptcha.border.colorColor of the border. Legal values are r,g,b (and optional alpha) or white,black,blue.black
kaptcha.border.thicknessThickness of the border around kaptcha. Legal values are > 0.1
kaptcha.image.widthWidth in pixels of the kaptcha image.200
kaptcha.image.heightHeight in pixels of the kaptcha image.50
kaptcha.producer.implThe image producer.com.google.code.kaptcha.impl.DefaultKaptcha
kaptcha.textproducer.implThe text producer.com.google.code.kaptcha.text.impl.DefaultTextCreator
kaptcha.textproducer.char.stringThe characters that will create the kaptcha.abcde2345678gfynmnpwx
kaptcha.textproducer.char.lengthThe number of characters to display.5
kaptcha.textproducer.font.namesA list of comma separated font names.Arial, Courier
kaptcha.textproducer.font.sizeThe size of the font to use.40px.
kaptcha.textproducer.font.colorThe color to use for the font. Legal values are r,g,b.black
kaptcha.noise.implThe noise producer.com.google.code.kaptcha.impl.DefaultNoise
kaptcha.noise.colorThe noise color. Legal values are r,g,b.black
kaptcha.obscurificator.implThe obscurificator implementation.com.google.code.kaptcha.impl.WaterRipple
kaptcha.background.implThe background implementation.com.google.code.kaptcha.impl.DefaultBackground
kaptcha.background.clear.fromStarting background color. Legal values are r,g,b.light grey
kaptcha.background.clear.toEnding background color. Legal values are r,g,b.white
kaptcha.word.implThe word renderer implementation.com.google.code.kaptcha.text.impl.DefaultWordRenderer
kaptcha.session.keyThe value for the kaptcha is generated and is put into the HttpSession. This is the key value for that item in the session.KAPTCHA_SESSION_KEY
kaptcha.session.dateThe date the kaptcha is generated is put into the HttpSession. This is the key value for that item in the session.KAPTCHA_SESSION_DATE

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值