Kaptcha是简单易用的Java库,用于生成验证码图像验证。 在本教程中,我们向您展示如何通过Spring将Kaptcha与Wicket框架集成 。
使用的库:
- Kaptcha v2.3.2
- 小票v1.4.17
- 检票口弹簧v1.4.17
- Spring v3.0.5.RELEASE
注意
本文主要介绍如何将Kaptcha与Wicket框架集成,对于Wicket + Spring,请参考此“ Wicket + Spring集成示例 ”。
1.获取Kaptcha
根据此线程 ,所有者不喜欢Maven,因此您必须手动将库安装到本地Maven存储库中。
1.在此处http://code.google.com/p/kaptcha/获取Kaptcha库
2.发出以下Maven命令以手动安装。
mvn install:install-file -Dfile=c:\kaptcha-2.3.2.jar -DgroupId=com.google.code
-DartifactId=kaptcha -Dversion=2.3.2 -Dpackaging=jar
3.稍后,您可以在您的pom.xml文件中包含kaptcha。
档案:pom.xml
<dependency>
<groupId>com.google.code</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
2.通过Spring的DefaultKaptcha
为“ DefaultKaptcha ”创建一个Spring bean,名为captchaProducer 。
文件:applicationContext.xml
<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.0.xsd">
<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="null">
</constructor-arg>
</bean>
</property>
</bean>
</beans>
3. CaptchaImage
创建一个扩展Wicket的NonCachingImage
的CaptchaImage类,并使用DynamicImageResource
类动态生成验证码图像。
文件:CaptchaImage.java
package com.mkyong.user;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.wicket.Request;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.markup.html.image.NonCachingImage;
import org.apache.wicket.markup.html.image.resource.DynamicImageResource;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.spring.injection.annot.SpringBean;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class CaptchaImage extends NonCachingImage {
private static final String CAPTCHA_PRODUCER = "captchaProducer";
// inject via Spring
@SpringBean
private DefaultKaptcha captchaProducer;
// private DefaultKaptcha captchaProducer;
public CaptchaImage(String id) {
super(id);
setImageResource(new DynamicImageResource() {
public byte[] getImageData() {
ByteArrayOutputStream os = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
try {
BufferedImage bi = getImageCaptchaService();
encoder.encode(bi);
return os.toByteArray();
} catch (Exception e) {
throw new RuntimeException(e);
}
};
private BufferedImage getImageCaptchaService() {
Request request = RequestCycle.get().getRequest();
HttpServletRequest httpRequest = ((WebRequest) request)
.getHttpServletRequest();
String capText = captchaProducer.createText();
// store the text in the session
httpRequest.getSession().setAttribute(
Constants.KAPTCHA_SESSION_KEY, capText);
// create the image with the text
BufferedImage bi = captchaProducer.createImage(capText);
return bi;
}
});
}
}
4. CaptchaValidator
创建一个名为“ CaptchaValidator ”的自定义验证器,用于验证用户输入并与Kaptcha生成的代码进行比较。
文件:CaptchaValidator.java
package com.mkyong.user;
import javax.servlet.http.HttpServletRequest;
import org.apache.wicket.Request;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.validator.AbstractValidator;
public class CaptchaValidator extends AbstractValidator<String> {
private static final long serialVersionUID = 1L;
private String INVALID_CODE = "captcha.invalid";
public void onValidate(IValidatable validatable) {
String kaptchaReceived = (String) validatable.getValue();
Request request = RequestCycle.get().getRequest();
HttpServletRequest httpRequest = ((WebRequest) request)
.getHttpServletRequest();
String kaptchaExpected = (String) httpRequest.getSession()
.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
if (kaptchaReceived == null
|| !kaptchaReceived.equalsIgnoreCase(kaptchaExpected)) {
error(validatable);
}
}
// validate on numm value as well
@Override
public boolean validateOnNullValue() {
return true;
}
@Override
protected String resourceKey() {
return INVALID_CODE;
}
}
文件:package.properties
captcha.invalid = Incorrect answer, type words in image again!
5.小门组件
将Kaptcha与Wicket组件集成。
package com.mkyong.user;
import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.PropertyModel;
public class KaptchaPage extends WebPage {
private String captchaInput;
public KaptchaPage(final PageParameters parameters) {
final CaptchaImage captchaImage = new CaptchaImage("kaptchaImage");
captchaImage.setOutputMarkupId(true);
TextField<String> captchaTF = new TextField<String>("captcha",
new PropertyModel<String>(this, "captchaInput"));
captchaTF.add(new CaptchaValidator());
Form<?> form = new Form<Void>("form") {
@Override
protected void onSubmit() {
info("Image words are correct!!!");
};
};
form.add(new AjaxFallbackLink("link") {
@Override
public void onClick(final AjaxRequestTarget target) {
captchaImage.detach();
if (target != null) {
target.addComponent(captchaImage);
} else {
// javascript is disable
}
}
}.add(captchaImage));
form.add(captchaTF);
add(form);
add(new FeedbackPanel("feedback"));
}
}
注意
captchaImage.detach();
是启用功能,当用户单击验证码图像时,会动态生成新的验证码图像。
<html>
<head>
<style>
.feedbackPanelINFO {
color: green;
}
.feedbackPanelERROR {
color: red;
}
</style>
</head>
<body>
<h1>Wicket + Kaptcha integration example</h1>
<div wicket:id="feedback"></div>
<form wicket:id="form">
<a wicket:id="link" title="Refresh image words">
<img wicket:id="kaptchaImage" /></a>
<br>
<label>Type the image words :</label>
<input type="text"wicket:id="captcha">
<input type="submit" value="Submit" />
</form>
</body>
</html>
6.演示
开始并访问-http:// localhost:8080 / WicketExamples /
如果输入错误:
如果输入正确:
下载它– Wicket-Kaptcha-Integration-Example.zip (10 KB)
参考文献
翻译自: https://mkyong.com/wicket/how-do-integrate-kaptcha-in-wicket-solution/