From 26f5f1a9b3c6282409bb247e91e3a3f4bca7e336 Mon Sep 17 00:00:00 2001
From: Ben Alex
- * If an {@link AccessDeniedException} is detected, the filter will response
- * with a HttpServletResponse.SC_FORBIDDEN
(403 error). Again,
- * this allows common access denied handling irrespective of the originating
- * security interceptor.
+ * If an {@link AccessDeniedException} is detected, the filter will respond
+ * with a HttpServletResponse.SC_FORBIDDEN
(403 error). In
+ * addition, the AccessDeniedException
itself will be placed in
+ * the HttpSession
attribute keyed against {@link
+ * #ACEGI_SECURITY_ACCESS_DENIED_EXCEPTION_KEY} (to allow access to the stack
+ * trace etc). Again, this allows common access denied handling irrespective
+ * of the originating security interceptor.
*
@@ -96,6 +99,7 @@ public class SecurityEnforcementFilter implements Filter, InitializingBean { //~ Static fields/initializers ============================================= private static final Log logger = LogFactory.getLog(SecurityEnforcementFilter.class); + public static final String ACEGI_SECURITY_ACCESS_DENIED_EXCEPTION_KEY = "ACEGI_SECURITY_403_EXCEPTION"; //~ Instance fields ======================================================== @@ -202,6 +206,8 @@ public class SecurityEnforcementFilter implements Filter, InitializingBean { "Access is denied - sending back forbidden response"); } + ((HttpServletRequest) request).getSession().setAttribute(ACEGI_SECURITY_ACCESS_DENIED_EXCEPTION_KEY, + accessDenied); sendAccessDeniedError(request, response); } catch (Throwable otherException) { throw new ServletException(otherException); diff --git a/core/src/test/java/org/acegisecurity/intercept/web/SecurityEnforcementFilterTests.java b/core/src/test/java/org/acegisecurity/intercept/web/SecurityEnforcementFilterTests.java index 011a0b3838..dd2a2e6539 100644 --- a/core/src/test/java/org/acegisecurity/intercept/web/SecurityEnforcementFilterTests.java +++ b/core/src/test/java/org/acegisecurity/intercept/web/SecurityEnforcementFilterTests.java @@ -32,6 +32,7 @@ import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; +import javax.servlet.http.HttpSession; /** @@ -64,8 +65,9 @@ public class SecurityEnforcementFilterTests extends TestCase { public void testAccessDeniedWhenAccessDeniedException() throws Exception { // Setup our HTTP request + HttpSession session = new MockHttpSession(); MockHttpServletRequest request = new MockHttpServletRequest(null, - new MockHttpSession()); + session); request.setServletPath("/secure/page.html"); // Setup our expectation that the filter chain will not be invoked, as access is denied @@ -84,6 +86,10 @@ public class SecurityEnforcementFilterTests extends TestCase { MockHttpServletResponse response = new MockHttpServletResponse(); filter.doFilter(request, response, chain); assertEquals(403, response.getError()); + assertEquals(AccessDeniedException.class, + session.getAttribute( + SecurityEnforcementFilter.ACEGI_SECURITY_ACCESS_DENIED_EXCEPTION_KEY) + .getClass()); } public void testDoFilterWithNonHttpServletRequestDetected()