|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.captcha; |
|
17 |
| |
|
18 |
| import org.acegisecurity.context.SecurityContextHolder; |
|
19 |
| |
|
20 |
| import org.apache.commons.logging.Log; |
|
21 |
| import org.apache.commons.logging.LogFactory; |
|
22 |
| |
|
23 |
| import org.springframework.beans.factory.InitializingBean; |
|
24 |
| |
|
25 |
| import java.io.IOException; |
|
26 |
| |
|
27 |
| import javax.servlet.*; |
|
28 |
| import javax.servlet.http.HttpServletRequest; |
|
29 |
| import javax.servlet.http.HttpSession; |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| public class CaptchaValidationProcessingFilter implements InitializingBean, |
|
48 |
| Filter { |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| protected static final Log logger = LogFactory.getLog(CaptchaValidationProcessingFilter.class); |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| private CaptchaServiceProxy captchaService; |
|
60 |
| private String captchaValidationParameter = "_captcha_parameter"; |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
3
| public void setCaptchaService(CaptchaServiceProxy captchaService) {
|
|
65 |
3
| this.captchaService = captchaService;
|
|
66 |
| } |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
0
| public CaptchaServiceProxy getCaptchaService() {
|
|
71 |
0
| return captchaService;
|
|
72 |
| } |
|
73 |
| |
|
74 |
1
| public void setCaptchaValidationParameter(String captchaValidationParameter) {
|
|
75 |
1
| this.captchaValidationParameter = captchaValidationParameter;
|
|
76 |
| } |
|
77 |
| |
|
78 |
1
| public String getCaptchaValidationParameter() {
|
|
79 |
1
| return captchaValidationParameter;
|
|
80 |
| } |
|
81 |
| |
|
82 |
3
| public void afterPropertiesSet() throws Exception {
|
|
83 |
3
| if (this.captchaService == null) {
|
|
84 |
1
| throw new IllegalArgumentException(
|
|
85 |
| "CaptchaServiceProxy must be defined "); |
|
86 |
| } |
|
87 |
| |
|
88 |
2
| if ((this.captchaValidationParameter == null)
|
|
89 |
| || "".equals(captchaValidationParameter)) { |
|
90 |
1
| throw new IllegalArgumentException(
|
|
91 |
| "captchaValidationParameter must not be empty or null"); |
|
92 |
| } |
|
93 |
| } |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
0
| public void destroy() {}
|
|
99 |
| |
|
100 |
4
| public void doFilter(ServletRequest request, ServletResponse response,
|
|
101 |
| FilterChain chain) throws IOException, ServletException { |
|
102 |
4
| String captcha_reponse = request.getParameter(captchaValidationParameter);
|
|
103 |
| |
|
104 |
4
| if ((request != null) && request instanceof HttpServletRequest
|
|
105 |
| && (captcha_reponse != null)) { |
|
106 |
2
| logger.debug("captcha validation parameter found");
|
|
107 |
| |
|
108 |
| |
|
109 |
2
| boolean valid = false;
|
|
110 |
| |
|
111 |
2
| logger.debug("try to validate");
|
|
112 |
| |
|
113 |
| |
|
114 |
2
| HttpSession session = ((HttpServletRequest) request).getSession();
|
|
115 |
| |
|
116 |
2
| if (session != null) {
|
|
117 |
2
| String id = session.getId();
|
|
118 |
2
| valid = this.captchaService.validateReponseForId(id,
|
|
119 |
| captcha_reponse); |
|
120 |
2
| logger.debug("captchaServiceProxy says : request is valid = "
|
|
121 |
| + valid); |
|
122 |
| |
|
123 |
2
| if (valid) {
|
|
124 |
1
| logger.debug("update the context");
|
|
125 |
1
| ((CaptchaSecurityContext) SecurityContextHolder.getContext())
|
|
126 |
| .setHuman(); |
|
127 |
| |
|
128 |
| |
|
129 |
| } else { |
|
130 |
1
| logger.debug("captcha test failed");
|
|
131 |
| } |
|
132 |
| } else { |
|
133 |
0
| logger.debug(
|
|
134 |
| "no session found, user don't even ask a captcha challenge"); |
|
135 |
| } |
|
136 |
| } else { |
|
137 |
2
| logger.debug("captcha validation parameter not found, do nothing");
|
|
138 |
| } |
|
139 |
| |
|
140 |
4
| if (logger.isDebugEnabled()) {
|
|
141 |
0
| logger.debug("chain ...");
|
|
142 |
| } |
|
143 |
| |
|
144 |
4
| chain.doFilter(request, response);
|
|
145 |
| } |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
0
| public void init(FilterConfig filterConfig) throws ServletException {}
|
|
155 |
| } |