| 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 |
3 |
public class CaptchaValidationProcessingFilter implements InitializingBean, Filter { |
| 45 |
|
|
| 46 |
|
|
| 47 |
2 |
protected static final Log logger = LogFactory.getLog(CaptchaValidationProcessingFilter.class); |
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
private CaptchaServiceProxy captchaService; |
| 52 |
3 |
private String captchaValidationParameter = "_captcha_parameter"; |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
public void afterPropertiesSet() throws Exception { |
| 57 |
3 |
if (this.captchaService == null) { |
| 58 |
1 |
throw new IllegalArgumentException("CaptchaServiceProxy must be defined "); |
| 59 |
|
} |
| 60 |
|
|
| 61 |
2 |
if ((this.captchaValidationParameter == null) || "".equals(captchaValidationParameter)) { |
| 62 |
1 |
throw new IllegalArgumentException("captchaValidationParameter must not be empty or null"); |
| 63 |
|
} |
| 64 |
1 |
} |
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
0 |
public void destroy() {} |
| 70 |
|
|
| 71 |
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
| 72 |
|
throws IOException, ServletException { |
| 73 |
4 |
String captchaResponse = request.getParameter(captchaValidationParameter); |
| 74 |
|
|
| 75 |
4 |
if ((request != null) && request instanceof HttpServletRequest && (captchaResponse != null)) { |
| 76 |
2 |
logger.debug("captcha validation parameter found"); |
| 77 |
|
|
| 78 |
|
|
| 79 |
2 |
boolean valid = false; |
| 80 |
|
|
| 81 |
2 |
logger.debug("try to validate"); |
| 82 |
|
|
| 83 |
|
|
| 84 |
2 |
HttpSession session = ((HttpServletRequest) request).getSession(); |
| 85 |
|
|
| 86 |
2 |
if (session != null) { |
| 87 |
2 |
String id = session.getId(); |
| 88 |
2 |
valid = this.captchaService.validateReponseForId(id, captchaResponse); |
| 89 |
2 |
logger.debug("captchaServiceProxy says : request is valid = " + valid); |
| 90 |
|
|
| 91 |
2 |
if (valid) { |
| 92 |
1 |
logger.debug("update the context"); |
| 93 |
1 |
((CaptchaSecurityContext) SecurityContextHolder.getContext()).setHuman(); |
| 94 |
|
|
| 95 |
|
|
| 96 |
|
} else { |
| 97 |
1 |
logger.debug("captcha test failed"); |
| 98 |
|
} |
| 99 |
2 |
} else { |
| 100 |
0 |
logger.debug("no session found, user don't even ask a captcha challenge"); |
| 101 |
|
} |
| 102 |
2 |
} else { |
| 103 |
2 |
logger.debug("captcha validation parameter not found, do nothing"); |
| 104 |
|
} |
| 105 |
|
|
| 106 |
4 |
if (logger.isDebugEnabled()) { |
| 107 |
0 |
logger.debug("chain ..."); |
| 108 |
|
} |
| 109 |
|
|
| 110 |
4 |
chain.doFilter(request, response); |
| 111 |
4 |
} |
| 112 |
|
|
| 113 |
|
public CaptchaServiceProxy getCaptchaService() { |
| 114 |
0 |
return captchaService; |
| 115 |
|
} |
| 116 |
|
|
| 117 |
|
public String getCaptchaValidationParameter() { |
| 118 |
1 |
return captchaValidationParameter; |
| 119 |
|
} |
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 123 |
|
|
| 124 |
|
|
| 125 |
|
|
| 126 |
|
|
| 127 |
|
|
| 128 |
0 |
public void init(FilterConfig filterConfig) throws ServletException {} |
| 129 |
|
|
| 130 |
|
public void setCaptchaService(CaptchaServiceProxy captchaService) { |
| 131 |
3 |
this.captchaService = captchaService; |
| 132 |
3 |
} |
| 133 |
|
|
| 134 |
|
public void setCaptchaValidationParameter(String captchaValidationParameter) { |
| 135 |
1 |
this.captchaValidationParameter = captchaValidationParameter; |
| 136 |
1 |
} |
| 137 |
|
} |