在Spring中使用jcaptcha实现图片验证
转载来自http://sha0k.iteye.com/blog/1364950
在Spring中使用jcaptcha首先要添加jcaptcha的包 我使用的是jcaptcha-1.0-all.jar 官网http://jcaptcha.sourceforge.net/jcaptcha/index.html
还要添加commons-collections-*.*.jar 版本自己控制
首先是ApplicationContext.xml的配置,这里主要是对验证码生成器服务的注入,控制类就是Spring Web中的Controller,没有什么特别的
Xml代码
<bean id="imageCaptchaService" index="0">
<ref bean="fastHashMapCaptchaStore"/>
</constructor-arg>
<!--which captcha Engine you use-->
<constructor-arg type="com.octo.captcha.engine.CaptchaEngine" index="1">
<ref bean="captchaEngineEx"/>
</constructor-arg>
<constructor-arg index="2">
<value>180</value>
</constructor-arg>
<constructor-arg index="3">
<value>100000</value>
</constructor-arg>
<constructor-arg index="4">
<value>75000</value>
</constructor-arg>
</bean>
<bean id="fastHashMapCaptchaStore"
content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<img alt="在Spring中施用jcaptcha实现图片验证" src="generatImage">
</body>
</html>
验证用户输入的验证码的控制类
Java代码
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
@Controller
public class ImageValidationController {
@Resource(name = "imageCaptchaService")
ImageCaptchaService imageCaptchaService;
@RequestMapping(value = "/validateImage")
public void ValidateCaptchaImage(HttpServletRequest request,
HttpServletResponse response)throws ServletException , IOException {
Boolean isResponseCorrect = Boolean.FALSE;
//remenber that we need an id to validate!
String captchaId = request.getSession().getId();
//retrieve the response
String captcha_value = request.getParameter("captcha_value");
// Call the Service method
try {
isResponseCorrect = imageCaptchaService.validateResponseForID(captchaId, captcha_value);
} catch (CaptchaServiceException e) {
//should not happen, may be thrown if the id is not valid
}
System.out.println(isResponseCorrect);
// httpServletResponse.encodeUrl("sucess.html");
if(isResponseCorrect.booleanValue()){
response.sendRedirect("success.html");
}
else {
response.sendRedirect("failture.html");
}
}
}
验证页面:
Html代码
<html>
<head>
<base href="<%=basePath%>">
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript"
src="http://jquery-json.googlecode.com/files/jquery.json-2.2.min.js"></script>
<script type="text/javascript">
function ajaxRefresh(){
document.getElementById("image").contentWindow.location.reload();
}
</script>
<title>My JSP 'image.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<iframe src="getimg.jsp" id="image"></iframe>
<a href="javascript:void(0)" onclick="ajaxRefresh()">ajaxRefresh</a>
<form action="validateImage">
<input type='text' name='captcha_value'>
<input type="submit" value="提交">
</form>
</body>
</html>