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