Tu dis :
"je genere un captchaID que je stocke en session
et je genere une image avec."
Comment fais tu ?
Moi j'ai fait une classe Java (ce n'est pas un singleton) :
public static String doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) 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 = httpServletRequest.getSession().getId();
// call the ImageCaptchaService getChallenge method
BufferedImage challenge =
CaptchaServiceSingleton.getInstance().getImageChallengeForID(captchaId,
httpServletRequest.getLocale());
// a jpeg encoder
JPEGImageEncoder jpegEncoder =
JPEGCodec.createJPEGEncoder(jpegOutputStream);
jpegEncoder.encode(challenge);
} catch (IllegalArgumentException e) {
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
return "error";
} catch (CaptchaServiceException e) {
httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return "error";
}
captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
// flush it in the response
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();
return "success";
}
Ma ftl :
<input type="text" name="imageText" maxlength="6"/>
<img id="imageCaptcha" name="imageCaptcha" src="/jcaptcha" >
Et ensuite, je fais normalement :
Boolean isResponseCorrect =Boolean.FALSE;
//remenber that we need an id to validate!
String captchaId = request.getSession().getId();
//retrieve the response
String responseCaptcha= request.getParameter("imageText");
// Call the Service method
try {
isResponseCorrect = CaptchaServiceSingleton.getInstance().validateResponseForID(captchaId,responseCaptcha);
} catch (CaptchaServiceException e) {
//should not happen, may be thrown if the id is not valid
}
Mais si je met ici :
isResponseCorrect = CaptchaServiceSingleton.getInstance().validateResponseForID(captchaId,null);
Ca ne plante même pas !
:-(