以前没看到过Kaptcha这种东东, 更别说拿来用了,虽然也不知道怎么样,在看完Maven教程的时候,有提到kaptcha这种生成验证码的第三方库,于是好奇会是神马样子,在加之Maven,小白的我拿来先用用吧 。。。。
不过可怜的我 ,刚接触Maven 甚至是第一次接触Kaptcha,连Maven中的那些文件, 包结构都跟以前看到的不一样 ,然后有点各种懵 。。。。懵。。。懵。。。 ( 附上源码 : )
2. pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.yc.ssm</groupId> <artifactId>SSM_001</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SSM_001 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>oracle-driver</artifactId> <version>11g</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5-BETA2</version> </dependency> <dependency> <groupId>com.google.code</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-json-plugin</artifactId> <version>2.3.24</version> </dependency> </dependencies> <build> <finalName>SSM_001</finalName> </build> </project>
然后拿到了一堆包:
3.之前就写过,想使用kaptcha生成验证码,只需要在web.xml中配置 :
来看看web.xml:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 登陆验证码Kaptcha 2--> <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> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
需注意几处地方:
4. 再来看看 struts.xml 中内容:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 关闭动态方法调用 actionid --> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="json-default"> <action name="userLoginAction" class="com.yc.web.actions.UserLoginAction"> <result type="json" name="success"> <param name="root">jsonModel</param> <param name="contentType"> text/html </param> </result> </action> </package> </struts>
5. UserLogin.java:
public class UserLogin implements Serializable { private static final long serialVersionUID = 1L; private String uname; private String pwd; private String checkCode; //input code private String ackCode; // img code public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getCheckCode() { return checkCode; } public void setCheckCode(String checkCode) { this.checkCode = checkCode; } public String getAckCode() { return ackCode; } public void setAckCode(String ackCode) { this.ackCode = ackCode; } @Override public String toString() { return "UserLogin [uname=" + uname + ", pwd=" + pwd + ", checkCode=" + checkCode + ", ackCode=" + ackCode + "]"; } public UserLogin(String uname, String pwd, String checkCode, String ackCode) { super(); this.uname = uname; this.pwd = pwd; this.checkCode = checkCode; this.ackCode = ackCode; } public UserLogin() { super(); } }
接下一篇: http://my.oschina.net/gently/blog/631769 《 Kaptcha的简单小例(二)》