zoukankan      html  css  js  c++  java
  • Google的kaptcha验证码使用

    效果图:


    官方地址:https://code.google.com/p/kaptcha/w/list

    1、把下载的kaptcha-2.3.2.jar添加到lib中

    2、配置web.xml增加servlet

    1. <servlet>  
    2.         <servlet-name>Kaptcha</servlet-name>  
    3.         <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>  
    4. </servlet>  
    5. <servlet-mapping>  
    6.         <servlet-name>Kaptcha</servlet-name>  
    7.         <url-pattern>/kaptcha.jpg</url-pattern>  
    8. </servlet-mapping>  

    3、在jsp页面中

    1. <form action="submit.action" method="post">  
    2.     <img src="kaptcha.jpg" id="kaptchaImage" /> <input type="text"  
    3.         name="kaptcha" value="" /> <input type="submit" name="submit"  
    4.         value="submit" />  
    5. </form>  
    其中src="kaptcha.jpg"会被定位到servlet上

    4、KaptchaServlet会把验证码设置到session中,可以如下方式获取

    1. String kaptchaExpected = (String)request.getSession()  
    2.     .getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);  

    5、如果是struts2的action,可以如下方式获取

    1. String kaptchaExpected = (String)ActionContext.getContext().getSession()  
    2. .get(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);  
    6、如果想设置点击图片更换验证码,可以加上如下js,需要jquery

    1. <script type="text/javascript">  
    2.     $(function(){  
    3.         $('#kaptchaImage').click(function () { $(this).attr('src', '/kaptcha.jpg?' + Math.floor(Math.random()*100) ); })  
    4.     });  
    5. </script>  

    7、或者来点fade效果

    1. <script type="text/javascript">  
    2.     $(function() {  
    3.         $('#kaptchaImage').click(  
    4.                 function() {  
    5.                     $(this).hide().attr('src',  
    6.                             'kaptcha.jpg?' + Math.floor(Math.random() * 100)).fadeIn();  
    7.                 });  
    8.     });  
    9. </script>  

    8、验证码图片还有很多参数设置

    设置方法,在web.xml的servlet中

    1. <init-param>  
    2.     <param-name>kaptcha.border</param-name>  
    3.     <param-value>no</param-value>  
    4. </init-param>  
    Constant 描述 默认值
    kaptcha.border 图片边框,合法值:yes , no yes
    kaptcha.border.color 边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue. black
    kaptcha.border.thickness 边框厚度,合法值:>0 1
    kaptcha.image.width 图片宽 200
    kaptcha.image.height 图片高 50
    kaptcha.producer.impl 图片实现类 com.google.code.kaptcha.impl.DefaultKaptcha
    kaptcha.textproducer.impl 文本实现类 com.google.code.kaptcha.text.impl.DefaultTextCreator
    kaptcha.textproducer.char.string 文本集合,验证码值从此集合中获取 abcde2345678gfynmnpwx
    kaptcha.textproducer.char.length 验证码长度 5
    kaptcha.textproducer.font.names 字体 Arial, Courier
    kaptcha.textproducer.font.size 字体大小 40px.
    kaptcha.textproducer.font.color 字体颜色,合法值: r,g,b  或者 white,black,blue. black
    kaptcha.textproducer.char.space 文字间隔 2
    kaptcha.noise.impl 干扰实现类 com.google.code.kaptcha.impl.DefaultNoise
    kaptcha.noise.color 干扰颜色,合法值: r,g,b 或者 white,black,blue. black
    kaptcha.obscurificator.impl 图片样式:
    水纹com.google.code.kaptcha.impl.WaterRipple
    鱼眼com.google.code.kaptcha.impl.FishEyeGimpy
    阴影com.google.code.kaptcha.impl.ShadowGimpy
    com.google.code.kaptcha.impl.WaterRipple
    kaptcha.background.impl 背景实现类 com.google.code.kaptcha.impl.DefaultBackground
    kaptcha.background.clear.from 背景颜色渐变,开始颜色 light grey
    kaptcha.background.clear.to 背景颜色渐变,结束颜色 white
    kaptcha.word.impl 文字渲染器 com.google.code.kaptcha.text.impl.DefaultWordRenderer
    kaptcha.session.key session key KAPTCHA_SESSION_KEY
    kaptcha.session.date session date KAPTCHA_SESSION_DATE

    9、

    水纹效果


    鱼眼效果


    阴影效果

  • 相关阅读:
    【leetcode】1562. Find Latest Group of Size M
    【leetcode】1561. Maximum Number of Coins You Can Get
    【leetcode】1560. Most Visited Sector in a Circular Track
    Python中用format函数格式化字符串的用法
    机器学习最常用优化之一——梯度下降优化算法综述
    python学习之argparse模块
    Ubuntu14.04安装NVIDIA显卡驱动
    Anaconda使用教程
    TensorFlow0.8代码目录结构讲解
    cmake编译opencv程序
  • 原文地址:https://www.cnblogs.com/yangkai-cn/p/4016715.html
Copyright © 2011-2022 走看看