kaptcha随机验证码的使用详解,超实用

效果图:

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

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

2、配置web.xml增加servlet

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

  

3、在jsp页面中
1
2
3
4
5
< form  action="submit.action" method="post">
     < img  src="kaptcha.jpg" id="kaptchaImage" /> < input  type="text"
         name="kaptcha" value="" /> < input  type="submit" name="submit"
         value="submit" />
</ form >

  

其中src="kaptcha.jpg"会被定位到servlet上

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

  

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

  

5、如果是struts2的action,可以如下方式获取
1
2
String kaptchaExpected = (String)ActionContext.getContext().getSession()
.get(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);

  

6、如果想设置点击图片更换验证码,可以加上如下js,需要jquery
1
2
3
4
5
<script type= "text/javascript" >
     $( function (){
         $( '#kaptchaImage' ).click( function  () { $( this ).attr( 'src' '/kaptcha.jpg?'  + Math.floor(Math.random()*100) ); })
     });
</script>

  

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

  

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

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

1
2
3
4
<init-param>
     <param-name>kaptcha.border</param-name>
     <param-value>no</param-value>
</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、

水纹效果

鱼眼效果

 阴影效果

效果图:

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

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

2、配置web.xml增加servlet

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

  

3、在jsp页面中
1
2
3
4
5
< form  action="submit.action" method="post">
     < img  src="kaptcha.jpg" id="kaptchaImage" /> < input  type="text"
         name="kaptcha" value="" /> < input  type="submit" name="submit"
         value="submit" />
</ form >

  

其中src="kaptcha.jpg"会被定位到servlet上

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

  

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

  

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

猜你喜欢

转载自blog.csdn.net/gavid0124/article/details/79084616
今日推荐