| 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.ConfigAttribute; |
| 19 |
|
import org.acegisecurity.ConfigAttributeDefinition; |
| 20 |
|
|
| 21 |
|
import org.acegisecurity.context.SecurityContextHolder; |
| 22 |
|
|
| 23 |
|
import org.acegisecurity.intercept.web.FilterInvocation; |
| 24 |
|
|
| 25 |
|
import org.acegisecurity.securechannel.ChannelEntryPoint; |
| 26 |
|
import org.acegisecurity.securechannel.ChannelProcessor; |
| 27 |
|
|
| 28 |
|
import org.apache.commons.logging.Log; |
| 29 |
|
import org.apache.commons.logging.LogFactory; |
| 30 |
|
|
| 31 |
|
import org.springframework.beans.factory.InitializingBean; |
| 32 |
|
|
| 33 |
|
import org.springframework.util.Assert; |
| 34 |
|
|
| 35 |
|
import java.io.IOException; |
| 36 |
|
|
| 37 |
|
import java.util.Iterator; |
| 38 |
|
|
| 39 |
|
import javax.servlet.ServletException; |
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
14 |
public abstract class CaptchaChannelProcessorTemplate implements ChannelProcessor, InitializingBean { |
| 61 |
|
|
| 62 |
|
|
| 63 |
|
private ChannelEntryPoint entryPoint; |
| 64 |
14 |
protected Log logger = LogFactory.getLog(this.getClass()); |
| 65 |
14 |
private String keyword = null; |
| 66 |
14 |
private int thresold = 0; |
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
public void afterPropertiesSet() throws Exception { |
| 76 |
4 |
Assert.notNull(entryPoint, "entryPoint required"); |
| 77 |
1 |
Assert.hasLength(keyword, "keyword required"); |
| 78 |
1 |
} |
| 79 |
|
|
| 80 |
|
public void decide(FilterInvocation invocation, ConfigAttributeDefinition config) |
| 81 |
|
throws IOException, ServletException { |
| 82 |
9 |
if ((invocation == null) || (config == null)) { |
| 83 |
1 |
throw new IllegalArgumentException("Nulls cannot be provided"); |
| 84 |
|
} |
| 85 |
|
|
| 86 |
8 |
CaptchaSecurityContext context = null; |
| 87 |
8 |
context = (CaptchaSecurityContext) SecurityContextHolder.getContext(); |
| 88 |
|
|
| 89 |
8 |
Iterator iter = config.getConfigAttributes(); |
| 90 |
|
|
| 91 |
16 |
while (iter.hasNext()) { |
| 92 |
8 |
ConfigAttribute attribute = (ConfigAttribute) iter.next(); |
| 93 |
|
|
| 94 |
8 |
if (supports(attribute)) { |
| 95 |
5 |
logger.debug("supports this attribute : " + attribute); |
| 96 |
|
|
| 97 |
5 |
if (!isContextValidConcerningHumanity(context)) { |
| 98 |
2 |
logger.debug("context is not allowed to access ressource, redirect to captcha entry point"); |
| 99 |
2 |
redirectToEntryPoint(invocation); |
| 100 |
|
} else { |
| 101 |
3 |
logger.debug("has been successfully checked this keyword, increment request count"); |
| 102 |
3 |
context.incrementHumanRestrictedRessoucesRequestsCount(); |
| 103 |
|
} |
| 104 |
|
} else { |
| 105 |
3 |
logger.debug("do not support this attribute"); |
| 106 |
|
} |
| 107 |
8 |
} |
| 108 |
8 |
} |
| 109 |
|
|
| 110 |
|
public ChannelEntryPoint getEntryPoint() { |
| 111 |
2 |
return entryPoint; |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
public String getKeyword() { |
| 115 |
3 |
return keyword; |
| 116 |
|
} |
| 117 |
|
|
| 118 |
|
public int getThresold() { |
| 119 |
26 |
return thresold; |
| 120 |
|
} |
| 121 |
|
|
| 122 |
|
abstract boolean isContextValidConcerningHumanity(CaptchaSecurityContext context); |
| 123 |
|
|
| 124 |
|
private void redirectToEntryPoint(FilterInvocation invocation) |
| 125 |
|
throws IOException, ServletException { |
| 126 |
2 |
if (logger.isDebugEnabled()) { |
| 127 |
0 |
logger.debug("context is not valid : redirecting to entry point"); |
| 128 |
|
} |
| 129 |
|
|
| 130 |
2 |
entryPoint.commence(invocation.getRequest(), invocation.getResponse()); |
| 131 |
2 |
} |
| 132 |
|
|
| 133 |
|
public void setEntryPoint(ChannelEntryPoint entryPoint) { |
| 134 |
5 |
this.entryPoint = entryPoint; |
| 135 |
5 |
} |
| 136 |
|
|
| 137 |
|
public void setKeyword(String keyword) { |
| 138 |
16 |
this.keyword = keyword; |
| 139 |
16 |
} |
| 140 |
|
|
| 141 |
|
public void setThresold(int thresold) { |
| 142 |
10 |
this.thresold = thresold; |
| 143 |
10 |
} |
| 144 |
|
|
| 145 |
|
public boolean supports(ConfigAttribute attribute) { |
| 146 |
12 |
if ((attribute != null) && (keyword.equals(attribute.getAttribute()))) { |
| 147 |
7 |
return true; |
| 148 |
|
} else { |
| 149 |
5 |
return false; |
| 150 |
|
} |
| 151 |
|
} |
| 152 |
|
} |