Maven(四):定制库到Mave本地资源库 (Kaptcha)

  1. 首先简单介绍下Kaptcha:

    kaptcha是一个扩展子simplecaptcha的验证码库,在java编程中是一个非常实用的验证码生成工具。可以利用这个工具生成各种样式的验证码。虽说它是个流行的第三那方Java库,但它不在Maven的中央仓库中。

  2. kaptcha 的工作原理是调用 com.google.code.kaptcha.servlet.KaptchaServlet ,生成一个验证码图片,响应到客户端,同时将生成的验证码字符串放到 HttpSession 中。 

  3. 接下就可定制库到Maven本地资源库:

    1. 首先下载jar包, 链接地址(吐槽很多次都没进去成功。):http://code.google.com/p/kaptcha/downloads/list

      将其解压后将文件中 kaptcha-version.jar  kaptcha-2.3.2.jar 复制到其他地方 (比如  C盘根目录下), 然后添加到本地Maven库中命令(可根据自己情况修改对应路径) 如下: 

    mvn install:install-file -DgroupId=com.google.code -DartifactId=kaptcha -Dversion=2.3.2 -Dfile=C:\kaptcha-2.3.jar -Dpackaging=jar -DgeneratePom=true
      Maven(四):定制库到Mave本地资源库 (Kaptcha)_第1张图片

        然后在  pom.xml 中配置,注意要和上面的groupId、artifactId和version保持一致:

<dependency>
			<groupId>com.google.code</groupId>
			<artifactId>kaptcha</artifactId>
			<version>2.3.2</version>
		</dependency>

     最后便可在Libraries中看到如下:

    Maven(四):定制库到Mave本地资源库 (Kaptcha)_第2张图片

   

4. 使用Kaptcha生成的验证码:

        在web.xml中配置(使用Kaptcha 生成验证码十分简单并且参数可以进行自定义。只需添加jar包配置下就可以使用。kaptcha所有配置都可以通过web.xml来完成)

<servlet>
        <servlet-name>Kaptcha</servlet-name>
        <servlet-class>
          com.google.code.kaptcha.servlet.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>图片宽 160</description>
            <param-name>kaptcha.image.width</param-name>
            <param-value>180</param-value>
        </init-param>
        <init-param>
            <description>图片高 32</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.google.code.kaptcha.text.impl.DefaultTextCreator
            </param-value>
        </init-param>
        <init-param>
            <description>文本集合,验证码值从此集合中获取</description>
            <param-name>kaptcha.textproducer.char.string</param-name>
            <param-value>1234567890abcdefghijklmnopqrstuvwxyz</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>32</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 kaptcha is generated is put into the
                HttpSession. This is the 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>

.......

你可能感兴趣的:(Maven(四):定制库到Mave本地资源库 (Kaptcha))