2009年6月17日 星期三

整合JCaptcha

JCaptcha是個用來產生圖形驗證碼的開源組件




Captcha class
主要是字型,出現的字元,驗證碼的字數等初始化設定
public class Captcha extends ListImageCaptchaEngine
{
protected void buildInitialFactories() {

WordGenerator wgen = new RandomWordGenerator("abcdefghijklmnopqrstuvwxyz123456789");
RandomRangeColorGenerator cgen = new RandomRangeColorGenerator(
new int[] { 0, 100 }, new int[] { 0, 100 },
new int[] { 0, 100 });

TextPaster textPaster = new RandomTextPaster(new Integer(4),
new Integer(4), cgen, true);

BackgroundGenerator backgroundGenerator = new FunkyBackgroundGenerator(
new Integer(150), new Integer(50));

Font[] fontsList = new Font[] { new Font("Arial", 0, 10),
new Font("Tahoma", 0, 10), new Font("Verdana", 0, 10), };

FontGenerator fontGenerator = new RandomFontGenerator(new Integer(30),
new Integer(30), fontsList);

WordToImage wordToImage = new ComposedWordToImage(fontGenerator,
backgroundGenerator, textPaster);
this.addFactory(new GimpyFactory(wgen, wordToImage));
}
}
servlet
public class DynaImageServlet extends HttpServlet {
public static class CaptchaServiceSingleton {
private static DefaultManageableImageCaptchaService instance =
new DefaultManageableImageCaptchaService(
new FastHashMapCaptchaStore(),
new Captcha(),
180,
100000,
75000);

public static ImageCaptchaService getInstance() {
return instance;
}
}

public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);}

protected void doGet(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) throws ServletException, IOException {

byte[] captchaChallengeAsJpeg = null;
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
try {
String captchaId = httpServletRequest.getSession().getId();
BufferedImage challenge = CaptchaServiceSingleton.getInstance().getImageChallengeForID(captchaId,httpServletRequest.getLocale());
JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(jpegOutputStream);
jpegEncoder.encode(challenge);
} catch (IllegalArgumentException e) {
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
} catch (CaptchaServiceException e) {
httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
httpServletResponse.setHeader("Cache-Control", "no-store");
httpServletResponse.setHeader("Pragma", "no-cache");
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/jpeg");
ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
responseOutputStream.write(captchaChallengeAsJpeg);
responseOutputStream.flush();
responseOutputStream.close();
}
}
然後在web.xnl裡加入這個servlet的設定


至於比對使用者所填入的驗證碼是否正確的部份
boolean isResponseCorrect = false;
String captchaId = ServletActionContext.getRequest().getSession().getId();
String sImageNumber = dp.getAttribute("imageNumber", "");
isResponseCorrect = CaptchaServiceSingleton.getInstance().validateResponseForID(captchaId, sImageNumber);

captchaId是session的id
主要是用來比對使用者送出表單時跟產生驗證碼兩者之間的session是否一致
sImageNumber是使用者輸入的值

然後就可以藉由isResponseCorrect的boolean值得知
使用者輸入的跟產生驗證碼是否一樣了!

沒有留言:

張貼留言