|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.context; |
|
17 |
| |
|
18 |
| import org.apache.commons.logging.Log; |
|
19 |
| import org.apache.commons.logging.LogFactory; |
|
20 |
| |
|
21 |
| import org.springframework.beans.factory.InitializingBean; |
|
22 |
| |
|
23 |
| import java.io.IOException; |
|
24 |
| |
|
25 |
| import javax.servlet.Filter; |
|
26 |
| import javax.servlet.FilterChain; |
|
27 |
| import javax.servlet.FilterConfig; |
|
28 |
| import javax.servlet.ServletException; |
|
29 |
| import javax.servlet.ServletRequest; |
|
30 |
| import javax.servlet.ServletResponse; |
|
31 |
| import javax.servlet.http.HttpServletRequest; |
|
32 |
| import javax.servlet.http.HttpSession; |
|
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 |
| public class HttpSessionContextIntegrationFilter implements InitializingBean, |
|
99 |
| Filter { |
|
100 |
| |
|
101 |
| |
|
102 |
| protected static final Log logger = LogFactory.getLog(HttpSessionContextIntegrationFilter.class); |
|
103 |
| private static final String FILTER_APPLIED = "__acegi_session_integration_filter_applied"; |
|
104 |
| public static final String ACEGI_SECURITY_CONTEXT_KEY = "ACEGI_SECURITY_CONTEXT"; |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| private Class context = SecurityContextImpl.class; |
|
109 |
| private Object contextObject; |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| private boolean allowSessionCreation = true; |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
0
| public void setAllowSessionCreation(boolean allowSessionCreation) {
|
|
121 |
0
| this.allowSessionCreation = allowSessionCreation;
|
|
122 |
| } |
|
123 |
| |
|
124 |
0
| public boolean isAllowSessionCreation() {
|
|
125 |
0
| return allowSessionCreation;
|
|
126 |
| } |
|
127 |
| |
|
128 |
7
| public void setContext(Class secureContext) {
|
|
129 |
7
| this.context = secureContext;
|
|
130 |
| } |
|
131 |
| |
|
132 |
1
| public Class getContext() {
|
|
133 |
1
| return context;
|
|
134 |
| } |
|
135 |
| |
|
136 |
7
| public void afterPropertiesSet() throws Exception {
|
|
137 |
7
| if ((this.context == null)
|
|
138 |
| || (!SecurityContext.class.isAssignableFrom(this.context))) { |
|
139 |
2
| throw new IllegalArgumentException(
|
|
140 |
| "context must be defined and implement SecurityContext (typically use org.acegisecurity.context.SecurityContextImpl; existing class is " |
|
141 |
| + this.context + ")"); |
|
142 |
| } |
|
143 |
| |
|
144 |
5
| this.contextObject = generateNewContext();
|
|
145 |
| } |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
4
| public void destroy() {}
|
|
151 |
| |
|
152 |
5
| public void doFilter(ServletRequest request, ServletResponse response,
|
|
153 |
| FilterChain chain) throws IOException, ServletException { |
|
154 |
5
| if ((request != null) && (request.getAttribute(FILTER_APPLIED) != null)) {
|
|
155 |
| |
|
156 |
0
| chain.doFilter(request, response);
|
|
157 |
| } else { |
|
158 |
5
| if (request != null) {
|
|
159 |
5
| request.setAttribute(FILTER_APPLIED, Boolean.TRUE);
|
|
160 |
| } |
|
161 |
| |
|
162 |
5
| HttpSession httpSession = null;
|
|
163 |
5
| boolean httpSessionExistedAtStartOfRequest = false;
|
|
164 |
| |
|
165 |
5
| try {
|
|
166 |
5
| httpSession = ((HttpServletRequest) request).getSession(false);
|
|
167 |
| } catch (IllegalStateException ignored) {} |
|
168 |
| |
|
169 |
5
| if (httpSession != null) {
|
|
170 |
3
| httpSessionExistedAtStartOfRequest = true;
|
|
171 |
| |
|
172 |
3
| Object contextFromSessionObject = httpSession.getAttribute(ACEGI_SECURITY_CONTEXT_KEY);
|
|
173 |
| |
|
174 |
3
| if (contextFromSessionObject != null) {
|
|
175 |
3
| if (contextFromSessionObject instanceof SecurityContext) {
|
|
176 |
2
| if (logger.isDebugEnabled()) {
|
|
177 |
0
| logger.debug(
|
|
178 |
| "Obtained from ACEGI_SECURITY_CONTEXT a valid SecurityContext and set to SecurityContextHolder: '" |
|
179 |
| + contextFromSessionObject + "'"); |
|
180 |
| } |
|
181 |
| |
|
182 |
2
| SecurityContextHolder.setContext((SecurityContext) contextFromSessionObject);
|
|
183 |
| } else { |
|
184 |
1
| if (logger.isWarnEnabled()) {
|
|
185 |
1
| logger.warn(
|
|
186 |
| "ACEGI_SECURITY_CONTEXT did not contain a SecurityContext but contained: '" |
|
187 |
| + contextFromSessionObject |
|
188 |
| + "'; are you improperly modifying the HttpSession directly (you should always use SecurityContextHolder) or using the HttpSession attribute reserved for this class? - new SecurityContext instance associated with SecurityContextHolder"); |
|
189 |
| } |
|
190 |
| |
|
191 |
1
| SecurityContextHolder.setContext(generateNewContext());
|
|
192 |
| } |
|
193 |
| } else { |
|
194 |
0
| if (logger.isDebugEnabled()) {
|
|
195 |
0
| logger.debug(
|
|
196 |
| "HttpSession returned null object for ACEGI_SECURITY_CONTEXT - new SecurityContext instance associated with SecurityContextHolder"); |
|
197 |
| } |
|
198 |
| |
|
199 |
0
| SecurityContextHolder.setContext(generateNewContext());
|
|
200 |
| } |
|
201 |
| } else { |
|
202 |
2
| if (logger.isDebugEnabled()) {
|
|
203 |
0
| logger.debug(
|
|
204 |
| "No HttpSession currently exists - new SecurityContext instance associated with SecurityContextHolder"); |
|
205 |
| } |
|
206 |
| |
|
207 |
2
| SecurityContextHolder.setContext(generateNewContext());
|
|
208 |
| } |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
5
| httpSession = null;
|
|
214 |
| |
|
215 |
| |
|
216 |
5
| int contextWhenChainProceeded = SecurityContextHolder.getContext()
|
|
217 |
| .hashCode(); |
|
218 |
| |
|
219 |
5
| try {
|
|
220 |
5
| chain.doFilter(request, response);
|
|
221 |
| } catch (IOException ioe) { |
|
222 |
1
| throw ioe;
|
|
223 |
| } catch (ServletException se) { |
|
224 |
0
| throw se;
|
|
225 |
| } finally { |
|
226 |
| |
|
227 |
| |
|
228 |
5
| try {
|
|
229 |
5
| httpSession = ((HttpServletRequest) request).getSession(false);
|
|
230 |
| } catch (IllegalStateException ignored) {} |
|
231 |
| |
|
232 |
5
| if ((httpSession == null) && httpSessionExistedAtStartOfRequest) {
|
|
233 |
0
| if (logger.isDebugEnabled()) {
|
|
234 |
0
| logger.debug(
|
|
235 |
| "HttpSession is now null, but was not null at start of request; session was invalidated, so do not create a new session"); |
|
236 |
| } |
|
237 |
| } |
|
238 |
| |
|
239 |
| |
|
240 |
5
| if ((httpSession == null)
|
|
241 |
| && !httpSessionExistedAtStartOfRequest) { |
|
242 |
2
| if (!allowSessionCreation) {
|
|
243 |
0
| if (logger.isDebugEnabled()) {
|
|
244 |
0
| logger.debug(
|
|
245 |
| "The HttpSession is currently null, and the HttpSessionContextIntegrationFilter is prohibited from creating a HttpSession (because the allowSessionCreation property is false) - SecurityContext thus not stored for next request"); |
|
246 |
| } |
|
247 |
2
| } else if (!contextObject.equals(
|
|
248 |
| SecurityContextHolder.getContext())) { |
|
249 |
1
| if (logger.isDebugEnabled()) {
|
|
250 |
0
| logger.debug(
|
|
251 |
| "HttpSession being created as SecurityContextHolder contents are non-default"); |
|
252 |
| } |
|
253 |
| |
|
254 |
1
| try {
|
|
255 |
1
| httpSession = ((HttpServletRequest) request)
|
|
256 |
| .getSession(true); |
|
257 |
| } catch (IllegalStateException ignored) {} |
|
258 |
| } else { |
|
259 |
1
| if (logger.isDebugEnabled()) {
|
|
260 |
0
| logger.debug(
|
|
261 |
| "HttpSession is null, but SecurityContextHolder has not changed from default: ' " |
|
262 |
| + SecurityContextHolder.getContext() |
|
263 |
| + "'; not creating HttpSession or storing SecurityContextHolder contents"); |
|
264 |
| } |
|
265 |
| } |
|
266 |
| } |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
5
| if ((httpSession != null)
|
|
271 |
| && (SecurityContextHolder.getContext().hashCode() != contextWhenChainProceeded)) { |
|
272 |
3
| httpSession.setAttribute(ACEGI_SECURITY_CONTEXT_KEY,
|
|
273 |
| SecurityContextHolder.getContext()); |
|
274 |
| |
|
275 |
3
| if (logger.isDebugEnabled()) {
|
|
276 |
0
| logger.debug("SecurityContext stored to HttpSession: '"
|
|
277 |
| + SecurityContextHolder.getContext() + "'"); |
|
278 |
| } |
|
279 |
| } |
|
280 |
| |
|
281 |
| |
|
282 |
5
| SecurityContextHolder.setContext(generateNewContext());
|
|
283 |
| |
|
284 |
5
| if (logger.isDebugEnabled()) {
|
|
285 |
0
| logger.debug(
|
|
286 |
| "SecurityContextHolder set to new context, as request processing completed"); |
|
287 |
| } |
|
288 |
| } |
|
289 |
| } |
|
290 |
| } |
|
291 |
| |
|
292 |
13
| public SecurityContext generateNewContext() throws ServletException {
|
|
293 |
13
| try {
|
|
294 |
13
| return (SecurityContext) this.context.newInstance();
|
|
295 |
| } catch (InstantiationException ie) { |
|
296 |
0
| throw new ServletException(ie);
|
|
297 |
| } catch (IllegalAccessException iae) { |
|
298 |
0
| throw new ServletException(iae);
|
|
299 |
| } |
|
300 |
| } |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
5
| public void init(FilterConfig filterConfig) throws ServletException {}
|
|
310 |
| } |