验证码Kaptcha快速入门

标签: 验证码 kaptcha
524人阅读 评论(0) 收藏 举报
分类:

验证码Kaptcha简单使用方法

引入jar包

<dependency>

          <groupId>com.google.code.kaptcha</groupId>

          <artifactId>kaptcha</artifactId>

          <version>2.3</version>

       </dependency>

配置web.xml

<!-- 登陆验证码Kaptcha 2-->

       <servlet>

              <servlet-name>Kaptcha</servlet-name>

              <servlet-class>

                     <!--默认的servlet -->

                     com.google.code.kaptcha.servlet.KaptchaServlet

                     <!--自己的算式验证码

                            com.th.mykaptcha.KaptchaServlet

                      -->

              </servlet-class>

              <init-param>

                     <description>图片边框,合法值:yes , no</description>

                     <param-name>kaptcha.border</param-name>

                     <param-value>yes</param-value>

              </init-param>

              <init-param>

                     <description>

                            边框颜色,合法值: r,g,b (and optional alpha) 或者

                            white,black,blue.

                     </description>

                     <param-name>kaptcha.border.color</param-name>

                     <param-value>black</param-value>

              </init-param>

              <init-param>

                     <description>边框厚度,合法值:>0</description>

                     <param-name>kaptcha.border.thickness</param-name>

                     <param-value>1</param-value>

              </init-param>

              <init-param>

                     <description>图片宽 200</description>

                     <param-name>kaptcha.image.width</param-name>

                     <param-value>200</param-value>

              </init-param>

              <init-param>

                     <description>图片高 50</description>

                     <param-name>kaptcha.image.height</param-name>

                     <param-value>50</param-value>

              </init-param>

              <init-param>

                     <description>图片实现类</description>

                     <param-name>kaptcha.producer.impl</param-name>

                     <param-value>

                            com.google.code.kaptcha.impl.DefaultKaptcha

                     </param-value>

              </init-param>

              <init-param>

                     <description>文本实现类</description>

                     <param-name>kaptcha.textproducer.impl</param-name>

                     <param-value>

                            <!-- 自己实现的中文实现类

                            com.th.mykaptcha.ChineseText

                             -->

                            <!-- 默认的实现类 -->

                            com.google.code.kaptcha.text.impl.DefaultTextCreator

                     </param-value>

              </init-param>

              <init-param>

                   <description>文本集合,验证码值从此集合中获取</description>

                   <param-name>kaptcha.textproducer.char.string</param-name>

                     <param-value>1234567890</param-value>

                       <!--<param-value>abcde2345678gfynmnpwx</param-value>-->

                   <!--<param-value>慕课网教程验证码实例</param-value> -->

              </init-param>

              <init-param>

                     <description>验证码长度 5</description>

                     <param-name>kaptcha.textproducer.char.length</param-name>

                     <param-value>4</param-value>

              </init-param>

              <init-param>

                     <description>字体Arial, Courier</description>

                     <param-name>kaptcha.textproducer.font.names</param-name>

                     <param-value>Arial, Courier</param-value>

              </init-param>

              <init-param>

                     <description>字体大小 40px.</description>

                     <param-name>kaptcha.textproducer.font.size</param-name>

                     <param-value>40</param-value>

              </init-param>

              <init-param>

                     <description>

                            字体颜色,合法值: r,g,b 或者white,black,blue.

                     </description>

                   <param-name>kaptcha.textproducer.font.color</param-name>

                     <param-value>black</param-value>

              </init-param>

              <init-param>

                     <description>文字间隔 2</description>

                   <param-name>kaptcha.textproducer.char.space</param-name>

                     <param-value>2</param-value>

              </init-param>

              <init-param>

                     <description>干扰实现类</description>

                     <param-name>kaptcha.noise.impl</param-name>

                     <param-value>

                            <!-- com.google.code.kaptcha.impl.NoNoise -->

                            com.google.code.kaptcha.impl.DefaultNoise

                     </param-value>

              </init-param>

              <init-param>

                     <description>

                            干扰颜色,合法值: r,g,b 或者white,black,blue.

                     </description>

                     <param-name>kaptcha.noise.color</param-name>

                     <param-value>black</param-value>

              </init-param>

              <init-param>

                     <description>

                            图片样式: 水纹com.google.code.kaptcha.impl.WaterRipple

                            鱼眼com.google.code.kaptcha.impl.FishEyeGimpy

                            阴影com.google.code.kaptcha.impl.ShadowGimpy

                     </description>

                     <param-name>kaptcha.obscurificator.impl</param-name>

                     <param-value>

                            com.google.code.kaptcha.impl.WaterRipple

                     </param-value>

              </init-param>

              <init-param>

                     <description>背景实现类</description>

                     <param-name>kaptcha.background.impl</param-name>

                     <param-value>

                            com.google.code.kaptcha.impl.DefaultBackground

                     </param-value>

              </init-param>

              <init-param>

                     <description>背景颜色渐变,开始颜色</description>

                     <param-name>kaptcha.background.clear.from</param-name>

                     <param-value>green</param-value>

              </init-param>

              <init-param>

                     <description>背景颜色渐变,结束颜色</description>

                     <param-name>kaptcha.background.clear.to</param-name>

                     <param-value>white</param-value>

              </init-param>

              <init-param>

                     <description>文字渲染器</description>

                     <param-name>kaptcha.word.impl</param-name>

                     <param-value>

                            com.google.code.kaptcha.text.impl.DefaultWordRenderer

                     </param-value>

              </init-param>

              <init-param>

                     <description>

                            session中存放验证码的key键

                     </description>

                     <param-name>kaptcha.session.key</param-name>

                     <param-value>KAPTCHA_SESSION_KEY</param-value>

              </init-param>

              <init-param>

                     <description>

                            The date the kaptchais generated is put into the

                            HttpSession. This isthe 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>

       <servlet-mapping>

              <servlet-name>Kaptcha</servlet-name>

              <url-pattern>/randomcode.jpg</url-pattern>

       </servlet-mapping>

 

页面使用方法

<img alt="验证码" src="randomcode.jpg" onclick="changeR(this)"style="cursor: pointer;">

 

 

 

 

 点击验证码图片刷新验证码:

 <script type="text/javascript">

       functionchangeR(node){

              // 用于点击时产生不同的验证码

              node.src= "randomcode.jpg?time="+new Date().getTime() ;

       }

</script>

 

扩展:显示中文验证码

       将上面的web.xml的文本实现类修改为圈中的类

 

ChineseText类具体代码

 

import java.util.Random;

import com.google.code.kaptcha.text.TextProducer;

import com.google.code.kaptcha.util.Configurable;

 

public class ChineseText extends Configurableimplements TextProducer {

 

       public String getText() {

              int length= getConfig().getTextProducerCharLength();

              //char[] charS = getConfig().getTextProducerCharString();

             

              String[]s = newString[]{"我","是","验","证","码"};

 

              Randomrand = newRandom();

              StringBuffersb = newStringBuffer();

              for(inti = 0;i< length; i++){

                     int ind = rand.nextInt(s.length);

                     sb.append(s[ind]);

              }

              return sb.toString();

       }

       /**

        * 自定义数字加字母验证码

        * @return

        */

       public String getText1() {

              int length= getConfig().getTextProducerCharLength();

              StringfinalWord = "",firstWord = "";

              int tempInt= 0;

              String[]array = { "0","1", "2","3","4","5", "6","7", "8","9",

                            "a", "b","c", "d","e","f"};

 

              Randomrand = newRandom();

 

              for (inti = 0;i< length; i++){

                     switch (rand.nextInt(array.length)){

                     case 1:

                            tempInt = rand.nextInt(26)+ 65;

                            firstWord = String.valueOf((char)tempInt);

                            break;

                     case 2:

                            intr1,r2,r3,r4;

                            StringstrH,

                            strL;// high&low

                            r1 = rand.nextInt(3)+ 11; // ǰպ[11,14)

                            if (r1 ==13) {

                                   r2 = rand.nextInt(7);

                            }else {

                                   r2 = rand.nextInt(16);

                            }

 

                            r3 = rand.nextInt(6)+ 10;

                            if (r3 ==10) {

                                   r4 = rand.nextInt(15)+ 1;

                            }else if(r3 == 15) {

                                   r4 = rand.nextInt(15);

                            }else {

                                   r4 = rand.nextInt(16);

                            }

 

                            strH = array[r1] + array[r2];

                            strL = array[r3] + array[r4];

 

                            byte[]bytes= newbyte[2];

                            bytes[0] = (byte)(Integer.parseInt(strH, 16));

                            bytes[1] = (byte)(Integer.parseInt(strL, 16));

 

                            firstWord = newString(bytes);

                            break;

                     default:

                            tempInt = rand.nextInt(10)+ 48;

                            firstWord = String.valueOf((char)tempInt);

                            break;

                     }

                     finalWord += firstWord;

              }

              return finalWord;

       }

}

 

算式验证码:

       修改web.xml配置:

 

KaptchaServlet代码:

 

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.util.Enumeration;

import java.util.Properties;

import javax.imageio.ImageIO;

import javax.servlet.Servlet;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.google.code.kaptcha.Producer;

import com.google.code.kaptcha.util.Config;

 

public class KaptchaServletextends HttpServletimplements Servlet {

       private Properties props;

       private Producer kaptchaProducer;

       private String sessionKeyValue;

 

       public KaptchaServlet() {

              this.props=new Properties();

              this.kaptchaProducer=null;

              this.sessionKeyValue=null;

       }

 

       public voidinit(ServletConfig conf) throws ServletException {

              super.init(conf);

              ImageIO.setUseCache(false);

              EnumerationinitParams =conf.getInitParameterNames();

              while (initParams.hasMoreElements()){

                     Stringkey = (String) initParams.nextElement();

                     Stringvalue = conf.getInitParameter(key);

                     this.props.put(key,value);

              }

 

              Configconfig = newConfig(this.props);

              this.kaptchaProducer=config.getProducerImpl();

              this.sessionKeyValue=config.getSessionKey();

       }

 

       public voiddoGet(HttpServletRequest req,HttpServletResponseresp)

                     throws ServletException, IOException {

              resp.setDateHeader("Expires",0L);

              resp.setHeader("Cache-Control","no-store, no-cache, must-revalidate");

              resp.addHeader("Cache-Control","post-check=0, pre-check=0");

              resp.setHeader("Pragma","no-cache");

              resp.setContentType("image/jpeg");

 

              StringcapText = this.kaptchaProducer.createText();

              Strings1 = capText.substring(0,1);

              Strings2 = capText.substring(1,2);

              int r =Integer.valueOf(s1).intValue() +Integer.valueOf(s2).intValue();

              req.getSession().setAttribute(this.sessionKeyValue,String.valueOf(r));

             BufferedImagebi = this.kaptchaProducer.createImage(s1+"+"+s2+"=?");

              ServletOutputStreamout = resp.getOutputStream();

              ImageIO.write(bi, "jpg",out);

              try {

                     out.flush();

              }finally {

                     out.close();

              }

       }

}

后台获取验证码值:

(String)request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);

 

查看评论

Spring、SpringMVC和Mybatis快速入门

老彭根据目前互联网主流开发企业级架构,整合SSM 框架,基于框架研究了一套视频教程,本教程浅显易懂,新学员易上学。解决了复杂、难以操作的框架搭建问题,让大家非常清楚地明白框架的运行机制,理解SSM 之间的调用关系,通过学生系统案例让大家掌握SSM 项目开发,让大家具备企业级开发的能力,该框架可以应用到实际工作中。
  • 2016年08月16日 20:19

开源的验证码框架Kaptcha实现验证码效果

本项目是通过maven创建的,首先需要导入Kaptcha的jar包,然后在spring中配置Kaptcha的属性,还需要实现一个生成验证码的controller类,最后在前端显示。 pom.xml导...
  • dulonghao
  • dulonghao
  • 2017-11-25 12:37:14
  • 209

(spring mvc)使用google的kaptcha生成验证码

1、添加kaptcha依赖包: com.github.penggle kaptcha 2.3.2 ...
  • javaloveiphone
  • javaloveiphone
  • 2017-02-10 10:51:10
  • 579

kaptcha验证码组件使用简介

Kaptcha是一个基于SimpleCaptcha的验证码开源项目。 官网地址:http://code.google.com/p/kaptcha/   kaptcha的使用比较方便,...
  • qq_38397969
  • qq_38397969
  • 2018-01-16 12:50:55
  • 398

spring boot项目引用kaptcha

pom 文件引用依赖 com.google.code kaptcha 2.3.2 kaptcha.jar在maven库里没找到...
  • danfly1010
  • danfly1010
  • 2016-06-24 17:17:23
  • 5400

kaptcha 验证码技术

1、添加依赖: com.github.penggle kaptcha 2.3.2 2、配置web.xml Kaptcha ...
  • qq_24949727
  • qq_24949727
  • 2016-12-21 15:56:22
  • 1352

Kaptchar详细配置表

kaptchar
  • ElephantBoy
  • ElephantBoy
  • 2016-10-12 11:01:39
  • 4261

Kaptcha参数介绍

Servlet页面package com.zzu.edu.servlet;import java.awt.Color; import java.awt.Graphics; import java.aw...
  • qq_21120027
  • qq_21120027
  • 2016-08-05 14:47:46
  • 3528

kaptcha-2.3.jar

  • 2015年07月03日 18:57
  • 380KB
  • 下载

kaptcha-2.3.2.jar

  • 2013年01月30日 11:34
  • 421KB
  • 下载
    个人资料
    持之以恒
    等级:
    访问量: 1万+
    积分: 307
    排名: 25万+
    最新评论