jcaptcha配置验证码

jcaptcha配置验证码

验证码实用工具Jtaptcha,实用工具jtaptcha

CAPTCHA是Completely Automated Public Turing Test to Tell Computers and
Humans Apart

全自动区分计算机和人类的图灵测试的简称

1.引入jar包

jcaptcha-1.0.jar

2.配置mvc整合文件 ***.xml

美高梅59599 1<!–
指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 –>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml,classpath:***.xml</param-value>
</context-param> <filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param> <param-name>encoding</param-name>
<param-value>UTF-8</param-value> </init-param>
</filter> View Code

 2.1 ***.xml 内容

美高梅59599 2<?xml
version=”1.0″ encoding=”UTF-8″?> <beans
xmlns=”http://www.springframework.org/schema/beans
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
default-lazy-init=”true”> <!–kaptcha验证码 <bean
id=”captchaProducer”
class=”com.google.code.kaptcha.impl.DefaultKaptcha”> <property
name=”config”> <bean
class=”com.google.code.kaptcha.util.Config”> <constructor-arg
type=”java.util.Properties”>
<value>kaptcha.border=yes</value> </constructor-arg>
</bean> </property> </bean> –> <bean
id=”captchaService”
class=”com.octo.captcha.service.multitype.GenericManageableCaptchaService”>
<constructor-arg index=”0″ ref=”imageEngine”/> <constructor-arg
type=”int” index=”1″ value=”180″/> <constructor-arg type=”int”
index=”2″ value=”100000″/> <constructor-arg type=”int” index=”3″
value=”75000″/> </bean> <bean id=”imageEngine”
class=”com.octo.captcha.engine.GenericCaptchaEngine”>
<constructor-arg index=”0″> <list> <ref
bean=”captchaFactory”/> </list> </constructor-arg>
</bean> <bean id=”captchaFactory”
class=”com.octo.captcha.image.gimpy.GimpyFactory”>
<constructor-arg> <ref bean=”wordgen”/>
</constructor-arg> <constructor-arg> <ref
bean=”wordtoimage”/> </constructor-arg> </bean> <bean
id=”wordgen” class=
“com.octo.captcha.component.word.wordgenerator.RandomWordGenerator”>
<!–可选字符–> <constructor-arg>
<value>aabbccddeefgghhkkmnnooppqqsstuuvvwxxyyzz</value>
</constructor-arg> </bean> <bean id=”wordtoimage”
class=”com.octo.captcha.component.image.wordtoimage.ComposedWordToImage”>
<constructor-arg index=”0″> <ref bean=”fontGenRandom”/>
</constructor-arg> <constructor-arg index=”1″> <ref
bean=”backGenUni”/> </constructor-arg> <constructor-arg
index=”2″> <ref bean=”decoratedPaster”/>
</constructor-arg> </bean> <bean id=”fontGenRandom”
class=”com.octo.captcha.component.image.fontgenerator.RandomFontGenerator”>
<!–最小字体–> <constructor-arg index=”0″>
<value>14</value> </constructor-arg>
<!–最大字体–> <constructor-arg index=”1″>
<value>20</value> </constructor-arg>
<constructor-arg index=”2″> <list> <bean
class=”java.awt.Font”> <constructor-arg
index=”0″><value>Arial</value></constructor-arg>
<constructor-arg
index=”1″><value>0</value></constructor-arg>
<constructor-arg
index=”2″><value>32</value></constructor-arg>
</bean> </list> </constructor-arg> </bean>
<bean id=”backGenUni”
class=”com.octo.captcha.component.image.backgroundgenerator.UniColorBackgroundGenerator”>
<!–背景宽度–> <constructor-arg index=”0″>
<value>80</value> </constructor-arg>
<!–背景高度–> <constructor-arg index=”1″>
<value>28</value> </constructor-arg> </bean>
<bean id=”decoratedPaster”
class=”com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster”>
<!–最大字符长度–> <constructor-arg type=”java.lang.Integer”
index=”0″> <value>4</value> </constructor-arg>
<!–最小字符长度–> <constructor-arg type=”java.lang.Integer”
index=”1″> <value>4</value> </constructor-arg>
<!–文本颜色–> <constructor-arg index=”2″> <ref
bean=”colorGen”/> </constructor-arg> <!–文本混淆–>
<constructor-arg index=”3″> <list> <!–<ref
bean=”baffleDecorator”/>–> </list> </constructor-arg>
</bean> <bean id=”baffleDecorator”
class=”com.octo.captcha.component.image.textpaster.textdecorator.BaffleTextDecorator”>
<constructor-arg type=”java.lang.Integer”
index=”0″><value>1</value></constructor-arg>
<constructor-arg type=”java.awt.Color” index=”1″><ref
bean=”colorWrite”/></constructor-arg> </bean> <bean
id=”colorGen”
class=”com.octo.captcha.component.image.color.SingleColorGenerator”>
<constructor-arg type=”java.awt.Color” index=”0″> <ref
bean=”colorBlack”/> </constructor-arg> </bean> <bean
id=”colorWrite” class=”java.awt.Color”> <constructor-arg
type=”int” index=”0″> <value>255</value>
</constructor-arg> <constructor-arg type=”int” index=”1″>
<value>255</value> </constructor-arg>
<constructor-arg type=”int” index=”2″>
<value>255</value> </constructor-arg> </bean>
<bean id=”colorBlack” class=”java.awt.Color”> <constructor-arg
type=”int” index=”0″> <value>50</value>
</constructor-arg> <constructor-arg type=”int” index=”1″>
<value>50</value> </constructor-arg>
<constructor-arg type=”int” index=”2″>
<value>50</value> </constructor-arg> </bean>
</beans> View Code

3.web.xml整合***.xml

美高梅59599 3<servlet>
<servlet-name>Jcaptcha</servlet-name>
<servlet-class>com.rbao.east.servlet.JcaptchaServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>Jcaptcha</servlet-name>
<url-pattern>/captcha.svl</url-pattern>
</servlet-mapping> View
Code

4.建立提供验证码的servlet

美高梅59599 4import
java.awt.image.BufferedImage; import java.io.IOException; import
javax.imageio.ImageIO; import javax.servlet.ServletException; import
javax.servlet.ServletOutputStream; import
javax.servlet.http.HttpServlet; import
javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse; import
org.apache.commons.io.output.ByteArrayOutputStream; import
org.springframework.beans.factory.BeanFactoryUtils; import
org.springframework.web.context.WebApplicationContext; import
org.springframework.web.context.support.WebApplicationContextUtils;
import com.octo.captcha.service.CaptchaServiceException; import
com.octo.captcha.service.image.ImageCaptchaService; /** *
提供验证码图片的Servlet */ @SuppressWarnings(“serial”) public class
JcaptchaServlet extends HttpServlet { public static final String
CAPTCHA_IMAGE_FORMAT = “jpeg”; private ImageCaptchaService
captchaService; @Override public void init() throws ServletException {
WebApplicationContext appCtx = WebApplicationContextUtils
.getWebApplicationContext(getServletContext()); captchaService =
(ImageCaptchaService) BeanFactoryUtils
.beanOfTypeIncludingAncestors(appCtx, ImageCaptchaService.class); }
@Override protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
byte[] captchaChallengeAsJpeg = null; // the output stream to render
the captcha image as jpeg into ByteArrayOutputStream jpegOutputStream =
new ByteArrayOutputStream(); try { // get the session id that will
identify the generated captcha. // the same id must be used to validate
the response, the session id // is a good candidate! String captchaId =
request.getSession().getId(); BufferedImage challenge =
captchaService.getImageChallengeForID( captchaId, request.getLocale());
// Jimi.putImage(“image/jpeg”, challenge, jpegOutputStream);
ImageIO.write(challenge, CAPTCHA_IMAGE_FORMAT, jpegOutputStream); }
catch (IllegalArgumentException e) {
response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } catch
(CaptchaServiceException e) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return; } // catch (JimiException e) { //
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); //
return; // } captchaChallengeAsJpeg = jpegOutputStream.toByteArray(); //
flush it in the response response.setHeader(“Cache-Control”,
“no-store”); response.setHeader(“Pragma”, “no-cache”);
response.setDateHeader(“Expires”, 0); response.setContentType(“image/” +
CAPTCHA_IMAGE_FORMAT); ServletOutputStream responseOutputStream =
response.getOutputStream();
responseOutputStream.write(captchaChallengeAsJpeg);
responseOutputStream.flush(); responseOutputStream.close(); } } View Code

刚开始写,也不知道怎么样,就先这样吧。记录一下自己在工作中遇到的一些技术或问题。

http://www.bkjia.com/Javabc/1095131.htmlwww.bkjia.comtruehttp://www.bkjia.com/Javabc/1095131.htmlTechArticle验证码实用工具Jtaptcha,实用工具jtaptcha
CAPTCHA是Completely Automated Public Turing Test to Tell Computers and
Humans Apart 全自动区分计算机和人类的图灵测…

第三步:创建classpath:config/utils.xml

<!–   session 提供类  –>

<bean id=”sessionProvider”  class=”cn.itcast.common.web.session.HttpSessionProvider”/>

第七步:配置web.xml

<!– 配置Jcaptcha 验证码–>

<servlet>

<servlet-name>jcaptcha</servlet-name>

<servlet-class>cn.itcast.common.captcha.JcaptchaServlet</servlet-class>

</servlet>

 

<servlet-mapping>

<servlet-name>jcaptcha</servlet-name>

<url-pattern>/captcha.svl</url-pattern>

</servlet-mapping>

第一步:创建SessionProvider接口

cn.itcast.common.web.session.SessionProvider

public interface SessionProvider {

public void setAttribute(HttpServletRequest request,String name ,Serializable value);

//获取Session中的值

public Serializable getAttribute(HttpServletRequest request,String name);

//退出登陆

public void logout(HttpServletRequest request,HttpServletResponse response);

//获取Session Id

public String getSessionId(HttpServletRequest request);

}

第六步:编写JcaptchaServlet

/**

 * 提供验证码图片的Servlet

 */

@SuppressWarnings(“serial”)

public class JcaptchaServlet extends HttpServlet {

public static final String CAPTCHA_IMAGE_FORMAT = “jpeg”;

 

private ImageCaptchaService captchaService;

private SessionProvider session;

 

@Override

public void init() throws ServletException {

WebApplicationContext appCtx = WebApplicationContextUtils

.getWebApplicationContext(getServletContext());

captchaService = (ImageCaptchaService) BeanFactoryUtils

.beanOfTypeIncludingAncestors(appCtx, ImageCaptchaService.class);

session = (SessionProvider) BeanFactoryUtils

.beanOfTypeIncludingAncestors(appCtx, SessionProvider.class);

}

 

@Override

protected void doGet(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

byte[] captchaChallengeAsJpeg = null;

美高梅59599,// the output stream to render the captcha image as jpeg into

ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();

try {

// get the session id that will identify the generated captcha.

// the same id must be used to validate the response, the session id

// is a good candidate!

 

String captchaId = session.getSessionId(request);

BufferedImage challenge = captchaService.getImageChallengeForID(

captchaId, request.getLocale());

// Jimi.putImage(“image/jpeg”, challenge, jpegOutputStream);

ImageIO.write(challenge, CAPTCHA_IMAGE_FORMAT, jpegOutputStream);

} catch (IllegalArgumentException e) {

response.sendError(HttpServletResponse.SC_NOT_FOUND);

return;

} catch (CaptchaServiceException e) {

response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

return;

}

// catch (JimiException e) {

// response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

// return;

// }

 

captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

 

// flush it in the response

response.setHeader(“Cache-Control”, “no-store”);

response.setHeader(“Pragma”, “no-cache”);

response.setDateHeader(“Expires”, 0);

response.setContentType(“image/” + CAPTCHA_IMAGE_FORMAT);

 

ServletOutputStream responseOutputStream = response.getOutputStream();

responseOutputStream.write(captchaChallengeAsJpeg);

responseOutputStream.flush();

responseOutputStream.close();

}

}

第八步:编写登陆页面front_page/buyer/login.jsp

<li>

<label for=”captcha”>验证码:</label>

<span class=”bg_text small”>

<input type=”text” id=”captcha” name=”captcha” maxLength=”7″/>

</span>

<img src=”/captcha.svl” onclick=”this.src=’/captcha.svl?d=’+new Date()” class=”code” alt=”换一张” /><a href=”javascript:void(0);” onclick=”$(‘.code’).attr(‘src’,’/captcha.svl?d=’+new Date())” title=”换一张”>换一张</a>

</li>

admin

网站地图xml地图