-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKaptcha.java
More file actions
100 lines (77 loc) · 3.07 KB
/
Copy pathKaptcha.java
File metadata and controls
100 lines (77 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package deep.controller;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.util.Config;
public class Kaptcha {
private Properties props = new Properties();
private Producer kaptchaProducer = null;
private String sessionKeyValue = null;
private String sessionKeyDateValue = null;
public Kaptcha() {
// Switch off disk based caching.
ImageIO.setUseCache(false);
this.props.put("kaptcha.border", "no");
this.props.put("kaptcha.textproducer.font.color", "blue");
this.props.put("kaptcha.textproducer.char.space", "5");
this.props.put("kaptcha.textproducer.char.length", "4");
this.props.put("kaptcha.textproducer.font.size", "30");
this.props.put("kaptcha.textproducer.char.string", "1234567890");
this.props.put("kaptcha.image.width", "90");
this.props.put("kaptcha.image.height", "30");
this.props.put("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
Config config = new Config(this.props);
this.kaptchaProducer = config.getProducerImpl();
this.sessionKeyValue = config.getSessionKey();
this.sessionKeyDateValue = config.getSessionDate();
}
/**
* map it to the /url/captcha.jpg
*
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
public void captcha(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// Set standard HTTP/1.1 no-cache headers.
resp.setHeader("Cache-Control", "no-store, no-cache");
// return a jpeg
resp.setContentType("image/jpeg");
// create the text for the image
String capText = this.kaptchaProducer.createText();
// store the text in the session
req.getSession().setAttribute(this.sessionKeyValue, capText);
// store the date in the session so that it can be compared
// against to make sure someone hasn't taken too long to enter
// their kaptcha
req.getSession().setAttribute(this.sessionKeyDateValue, new Date());
// create the image with the text
BufferedImage bi = this.kaptchaProducer.createImage(capText);
ServletOutputStream out = resp.getOutputStream();
// write the data out
ImageIO.write(bi, "jpg", out);
// fixes issue #69: set the attributes after we write the image in case
// the image writing fails.
// store the text in the session
req.getSession().setAttribute(this.sessionKeyValue, capText);
// store the date in the session so that it can be compared
// against to make sure someone hasn't taken too long to enter
// their kaptcha
req.getSession().setAttribute(this.sessionKeyDateValue, new Date());
}
public String getGeneratedKey(HttpServletRequest req) {
HttpSession session = req.getSession();
return (String) session
.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
}
}