1. 程式人生 > >Web驗證碼的實現方法(3)---開源元件Kaptcha

Web驗證碼的實現方法(3)---開源元件Kaptcha

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- 隨機驗證碼的類路徑  com.google.code.kaptcha.servlet.KaptchaServlet 當改為中文驗證碼時候要設定為此 -->
  <servlet>
  	<servlet-name>Kaptcha</servlet-name>
  	<servlet-class>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>圖片寬 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>
<span style="white-space:pre">				</span><!-- 當為自定義中文驗證碼設定為servle.ChineseText -->
<span style="white-space:pre">				</span>servlet.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>2</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 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>
</web-app>

總結:

kaptcha元件使用更加方便,自定義功能很多,這次實現了普通驗證碼功能和加法驗證碼功能,還可以設定自定義中文驗證碼,非常方便。