jcaptcha验证码使⽤(⼀)
jcaptcha使⽤默认样式⽣成的验证码⽐较难以识别,所以需要⾃定义验证码的样式,包括,背景⾊、背景⼤⼩、字体、字体⼤⼩、⽣成的字符数等。相对与kaptcha⽐较复杂。
创建⼀个JavaWeb⼯程,引⼊相关依赖jar包
创建⼀个jcaptcha单例的Service类,这⾥是设置验证码样式的关键部分
package cn.util;
import java.awt.Font;
aptcha.CaptchaFactory;
aptchaponent.image.backgroundgenerator.UniColorBackgroundGenerator;
lor.RandomRangeColorGenerator;
aptchaponent.image.fontgenerator.RandomFontGenerator;
paster.RandomTextPaster;
aptchaponent.image.wordtoimage.ComposedWordToImage;
aptchaponent.word.FileDictionary;
aptchaponent.word.wordgenerator.ComposeDictionaryWordGenerator;
ine.GenericCaptchaEngine;
aptcha.image.gimpy.GimpyFactory;
aptcha.service.image.ImageCaptchaService;
aptcha.service.multitype.GenericManageableCaptchaService;
public class CaptchaServiceSingleton {
private static ImageCaptchaService service = null;
public static ImageCaptchaService getService(){
if(service == null)
service = generatorCaptchaService();
return service;
}
/**
* 根据SpringBean的配置⽂件编写的代码实现
* */
public static ImageCaptchaService generatorCaptchaService(){
//⽣成随机颜⾊,参数分别表⽰RGBA的取值范围
RandomRangeColorGenerator textColor = new RandomRangeColorGenerator(new int[]{0,255},new int[]{0,180},new int[]{0,210},new int[]{255,255});
//随机⽂字多少和颜⾊,参数1和2表⽰最少⽣成多少个⽂字和最多⽣成多少个
RandomTextPaster randomTextPaster = new RandomTextPaster(4, 5, textColor,true);
//⽣成背景的⼤⼩这⾥是宽85⾼40的图⽚,也可以设置背景颜⾊和随机背景颜⾊,这⾥使⽤默认的⽩⾊
UniColorBackgroundGenerator colorbgGen = new UniColorBackgroundGenerator(85,40);
//随机⽣成的字体⼤⼩和字体类型,参数1和2表⽰最⼩和最⼤的字体⼤⼩,第三个表⽰随机的字体
RandomFontGenerator randomFontGenerator = new RandomFontGenerator(20, 25, new Font[]{new Font("Arial", 0, 10),new Font("Tahoma",0,10)});
//结合上⾯的对象构件⼀个从⽂本⽣成图⽚的对象
ComposedWordToImage cwti = new ComposedWordToImage(randomFontGenerator,colorbgGen,randomTextPaster);
//随机⽂本的字典,这⾥是使⽤jcaptcha-1.0-all.jar中的⽂本字典,字典名称为toddlist.properties
ComposeDictionaryWordGenerator cdwg = new ComposeDictionaryWordGenerator(new FileDictionary("toddlist"));
GimpyFactory gf = new GimpyFactory(cdwg, cwti);
GenericCaptchaEngine gce = new GenericCaptchaEngine(new CaptchaFactory[]{gf});
//返回⼀个Service对象,这⾥180是验证码存在的时间,单位是秒,200000是最⼤存储⼤⼩
return new GenericManageableCaptchaService(gce,180,200000,75000);
}
}
创建⼀个⽣成验证码的Servlet
package cn.servlet;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dec.jpeg.JPEGCodec;
import com.dec.jpeg.JPEGImageEncoder;
import cn.util.CaptchaServiceSingleton;
public class CustomServlet extends HttpServlet {
private static final long serialVersionUID = 169310818225761427L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
byte[] captChallengeAsJpeg = null;
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
String captchaId = Session().getId();
BufferedImage challenge = Service().getImageChallengeForID(Locale());
JPEGImageEncoder jpegEncoder = ateJPEGEncoder(jpegOutputStream);
captChallengeAsJpeg = ByteArray();
resp.setHeader("Cache-Control", "no-store");
resp.setHeader("Pragma", "no-cache");
jquery怎么进行验证resp.setDateHeader("Expires", 0);
resp.setContentType("image/jpeg");
ServletOutputStream respOutputStream = OutputStream();
respOutputStream.write(captChallengeAsJpeg);
respOutputStream.flush();
respOutputStream.close();
}
}
在l中注册这个Servlet
<servlet>
<servlet-name>generateValidateCode</servlet-name>
<servlet-class>cn.servlet.CustomServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>generateValidateCode</servlet-name>
<url-pattern>/cgvc</url-pattern>
</servlet-mapping>
创建html页⾯,引⼊jquery.js⽂件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript" src="statics/js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
//验证码刷新功能
function refreshCaptcha() {
//Math.floor();向下取整
//jQuery效果,fadeIn() ⽅法使⽤淡⼊效果来显⽰被选元素,假如该元素是隐藏的。
$('#vcode').hide().attr(
'src',
'cgvc?'+ Math.floor(Math.random() * 100)).fadeIn();
}
</script>
<body>
<form action="valiServlet" method="post">
<input type="text" name="customgvc">
<img onclick="refreshCaptcha()" id="vcode" title="点击更换" alt="验证图⽚" src="cgvc" height="30" width="80"> <input type="submit" value="提交">
</form>
</body>
</html>
编写⼀个验证Servlet并在l中注册。(success.html和fail.html就不创建了)
package cn.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.util.CaptchaServiceSingleton;
public class ValidateServlet extends HttpServlet {
private static final long serialVersionUID = -7173743572400866269L;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String captchaId = Session().getId();
String validateCode = Parameter("customgvc");
boolean validateResult = Service().validateResponseForID(captchaId, validateCode);
if(validateResult)
resp.sendRedirect("success.html");
else
resp.sendRedirect("fail.html");
}
}
<servlet>
<servlet-name>validatServlet</servlet-name>
<servlet-class>cn.servlet.ValidateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>validatServlet</servlet-name>
<url-pattern>/valiServlet</url-pattern>
</servlet-mapping>
输⼊验证码点击提交,看是否可以验证通过,如果⼀切正常的话Session中的和提交的是⼀致的
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论