今天无意间发现了一个很好用的验证码插件,java版的,很不错的一个插件,只用简单的在web.xml中设置几个属性,一个很漂亮的验证码就出来了。其他的参数都可以自己设置,最牛的就是提供了接口,可以自己定义哦。
怎么样,还不错吧,这个图是我设置长宽后生成的,有点点大,不过没关系,这个可以自己设置的。
多大概设置了边框,字体,个数,字的大小,长,宽,中文接口等这几个属性,然后很简单的就生成了这样一个验证码,呵呵,不错,很简单。
看看配置:
1.<init-param>
2. <param-name>kaptcha.border</param-name>
3. <param-value>yes</param-value>
4. </init-param>
5. <init-param>
6. <param-name>kaptcha.border.color</param-name>
7. <param-value>105,179,90</param-value>
8. </init-param>
9. <init-param>
10. <param-name>kaptcha.textproducer.impl</param-name>
11. <param-value>com.randcode.ChineseText</param-value>
12. </init-param>
13.
14. <init-param>
15. <param-name>kaptcha.textproducer.font.color</param-name>
16. <param-value>black</param-value>
17. </init-param>
18. <init-param>
19. <param-name>kaptcha.image.width</param-name>
20. <param-value>500</param-value>
21. </init-param>
22. <init-param>
23. <param-name>kaptcha.image.height</param-name>
24. <param-value>300</param-value>
25. </init-param>
26. <init-param>
27. <param-name>kaptcha.textproducer.font.size</param-name>
28. <param-value>90</param-value>
29. </init-param>
30. <init-param>
31. <param-name>kaptcha.session.key</param-name>
32. <param-value>code</param-value>
33. </init-param>
34. <init-param>
35. <param-name>kaptcha.textproducer.char.length</param-name>
36. <param-value>4</param-value>
37. </init-param>
38. <init-param>
39. <param-name>kaptcha.textproducer.font.names</param-name>
40. <param-value>宋体,楷体,微软雅黑</param-value>
41. </init-param>
<init-param>
<param-name>kaptcha.border</param-name>
<param-value>yes</param-value>
</init-param>
<init-param>
<param-name>kaptcha.border.color</param-name>
<param-value>105,179,90</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.impl</param-name>
<param-value>com.randcode.ChineseText</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.font.color</param-name>
<param-value>black</param-value>
</init-param>
<init-param>
<param-name>kaptcha.image.width</param-name>
<param-value>500</param-value>
</init-param>
<init-param>
<param-name>kaptcha.image.height</param-name>
<param-value>300</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.font.size</param-name>
<param-value>90</param-value>
</init-param>
<init-param>
<param-name>kaptcha.session.key</param-name>
<param-value>code</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.char.length</param-name>
<param-value>4</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.font.names</param-name>
<param-value>宋体,楷体,微软雅黑</param-value>
</init-param>
呵呵,还简洁吧。这个插件自带了个中文生成类,但是这个类做得很死,中文是手动输入进去的,我的天,太死了,然后leo就找啊找,发现API中有一个自定义接口,哈哈,太好了,正好我之前有一个生成中文的程序,直接COPY过来,写了个简单的类,测试,成功了,就是上面发的图。接口是这个:
1.<init-param>
2. <param-name>kaptcha.textproducer.impl</param-name>
3. <param-value>com.randcode.ChineseText</param-value>
4.</init-param>
<init-param>
<param-name>kaptcha.textproducer.impl</param-name>
<param-value>com.randcode.ChineseText</param-value>
</init-param>
如果有朋友不知道怎么用的,可以联系我。
生成中文的代码:
Java代码
1.package com.randcode;
2.
3.import java.util.Random;
4.
5.import com.google.code.kaptcha.text.TextProducer;
6.import com.google.code.kaptcha.util.Configurable;
7.
8.public class ChineseText extends Configurable implements TextProducer{
9.
10. public String getText() {
11. int length = getConfig().getTextProducerCharLength();
12. String finalWord="",firstWord="";
13. int tempInt=0;
14. String[] array={
15. "0","1","2","3",
16. "4","5","6","7",
17. "8","9","a","b",
18. "c","d","e","f"};
19.
20. Random rand = new Random();
21.
22. for(int i=0;i<length;i++){
23. switch(rand.nextInt(3)){
24. case 1:
25. tempInt=rand.nextInt(26)+65;
26. firstWord=String.valueOf((char)tempInt);
27. break;
28. case 2:
29. int r1,r2,r3,r4;
30. String strH,strL;//high&low
31. r1=rand.nextInt(3)+11; //前闭后开[11,14)
32. if(r1==13){
33. r2=rand.nextInt(7);
34. }else{
35. r2=rand.nextInt(16);
36. }
37.
38. r3=rand.nextInt(6)+10;
39. if(r3==10){
40. r4=rand.nextInt(15)+1;
41. }else if(r3==15){
42. r4=rand.nextInt(15);
43. }else{
44. r4=rand.nextInt(16);
45. }
46.
47. strH=array[r1]+array[r2];
48. strL=array[r3]+array[r4];
49.
50. byte[] bytes=new byte[2];
51. bytes[0]=(byte)(Integer.parseInt(strH,16));
52. bytes[1]=(byte)(Integer.parseInt(strL,16));
53.
54. firstWord=new String(bytes);
55. break;
56. default:
57. tempInt=rand.nextInt(10)+48;
58. firstWord=String.valueOf((char)tempInt);
59. break;
60. }
61. finalWord+=firstWord;
62. }
63. return finalWord;
64. }
65.}
原文:
http://www.iteye.com/topic/760483