1
2
3
4
5
6
7 package com.octo.captcha.module.servlet.image;
8
9 import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
10 import com.octo.captcha.service.image.ImageCaptchaService;
11 import com.octo.captcha.service.CaptchaServiceException;
12
13 import javax.imageio.ImageIO;
14 import javax.servlet.Servlet;
15 import javax.servlet.ServletException;
16 import javax.servlet.ServletOutputStream;
17 import javax.servlet.http.HttpServlet;
18 import javax.servlet.http.HttpServletRequest;
19 import javax.servlet.http.HttpServletResponse;
20 import java.awt.image.BufferedImage;
21 import java.io.IOException;
22
23 /***
24 * @author mag
25 * @Date 14 févr. 2009
26 */
27 public class SimpleImageCaptchaServlet extends HttpServlet implements Servlet
28 {
29 public static ImageCaptchaService service= new DefaultManageableImageCaptchaService();
30
31
32
33 @Override
34 protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
35
36 httpServletResponse.setDateHeader("Expires", 0);
37
38 httpServletResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
39
40 httpServletResponse.addHeader("Cache-Control", "post-check=0, pre-check=0");
41
42 httpServletResponse.setHeader("Pragma", "no-cache");
43
44
45 httpServletResponse.setContentType("image/jpeg");
46
47
48
49 BufferedImage bi = service.getImageChallengeForID(httpServletRequest.getSession(true).getId());
50
51 ServletOutputStream out = httpServletResponse.getOutputStream();
52
53
54 ImageIO.write(bi, "jpg", out);
55 try
56 {
57 out.flush();
58 }
59 finally
60 {
61 out.close();
62 }
63 }
64
65
66 public static boolean validateResponse(HttpServletRequest request, String userCaptchaResponse){
67
68 if(request.getSession(false)==null)return false;
69
70 boolean validated = false;
71 try {
72 validated = service.validateResponseForID(request.getSession().getId(),userCaptchaResponse);
73 } catch (CaptchaServiceException e) {
74
75 }
76 return validated;
77 }
78 }