소스 검색

Fix for SEC-159. Added clearContext() method to SecurityContextHolder and refactored code to use it instead of putting an empty context into the holder.

Luke Taylor 19 년 전
부모
커밋
dc959b1847
20개의 변경된 파일46개의 추가작업 그리고 25개의 파일을 삭제
  1. 1 1
      core/src/main/java/org/acegisecurity/context/HttpSessionContextIntegrationFilter.java
  2. 12 0
      core/src/main/java/org/acegisecurity/context/SecurityContextHolder.java
  3. 1 1
      core/src/main/java/org/acegisecurity/context/rmi/ContextPropagatingRemoteInvocation.java
  4. 9 0
      core/src/test/java/org/acegisecurity/captcha/CaptchaChannelProcessorTemplateTests.java
  5. 1 1
      core/src/test/java/org/acegisecurity/context/rmi/ContextPropagatingRemoteInvocationTests.java
  6. 2 2
      core/src/test/java/org/acegisecurity/intercept/web/FilterSecurityInterceptorTests.java
  7. 2 2
      core/src/test/java/org/acegisecurity/providers/anonymous/AnonymousProcessingFilterTests.java
  8. 2 2
      core/src/test/java/org/acegisecurity/providers/jaas/SecurityContextLoginModuleTests.java
  9. 1 1
      core/src/test/java/org/acegisecurity/taglibs/authz/AuthorizeTagAttributeTests.java
  10. 1 1
      core/src/test/java/org/acegisecurity/taglibs/authz/AuthorizeTagCustomGrantedAuthorityTests.java
  11. 1 1
      core/src/test/java/org/acegisecurity/taglibs/authz/AuthorizeTagExpressionLanguageTests.java
  12. 1 1
      core/src/test/java/org/acegisecurity/taglibs/authz/AuthorizeTagTests.java
  13. 1 1
      core/src/test/java/org/acegisecurity/taglibs/velocity/AuthzImplAttributeTest.java
  14. 1 1
      core/src/test/java/org/acegisecurity/taglibs/velocity/AuthzImplAuthorizeTagTest.java
  15. 2 2
      core/src/test/java/org/acegisecurity/ui/AbstractProcessingFilterTests.java
  16. 1 1
      core/src/test/java/org/acegisecurity/ui/ExceptionTranslationFilterTests.java
  17. 2 2
      core/src/test/java/org/acegisecurity/ui/basicauth/BasicProcessingFilterTests.java
  18. 2 2
      core/src/test/java/org/acegisecurity/ui/digestauth/DigestProcessingFilterTests.java
  19. 2 2
      core/src/test/java/org/acegisecurity/ui/rememberme/RememberMeProcessingFilterTests.java
  20. 1 1
      samples/contacts/src/main/java/sample/contact/ClientApplication.java

+ 1 - 1
core/src/main/java/org/acegisecurity/context/HttpSessionContextIntegrationFilter.java

@@ -279,7 +279,7 @@ public class HttpSessionContextIntegrationFilter implements InitializingBean,
                 }
 
                 // Remove SecurityContextHolder contents
-                SecurityContextHolder.setContext(generateNewContext());
+                SecurityContextHolder.clearContext();
 
                 if (logger.isDebugEnabled()) {
                     logger.debug(

+ 12 - 0
core/src/main/java/org/acegisecurity/context/SecurityContextHolder.java

@@ -75,4 +75,16 @@ public class SecurityContextHolder {
 
         return (SecurityContext) contextHolder.get();
     }
+
+    /**
+     * Explicitly clears the context value from thread local storage.
+     * Typically used on completion of a request to prevent potential
+     * misuse of the associated context information if the thread is
+     * reused. 
+     */
+    public static void clearContext() {
+        // Internally set the context value to null. This is never visible
+        // outside the class.
+        contextHolder.set(null);
+    }
 }

+ 1 - 1
core/src/main/java/org/acegisecurity/context/rmi/ContextPropagatingRemoteInvocation.java

@@ -123,7 +123,7 @@ public class ContextPropagatingRemoteInvocation extends RemoteInvocation {
 
         } finally {
 
-            SecurityContextHolder.setContext(new SecurityContextImpl());
+            SecurityContextHolder.clearContext();
 
             if (logger.isDebugEnabled()) {
                 logger.debug(

+ 9 - 0
core/src/test/java/org/acegisecurity/captcha/CaptchaChannelProcessorTemplateTests.java

@@ -39,6 +39,15 @@ import javax.servlet.ServletException;
 public class CaptchaChannelProcessorTemplateTests extends TestCase {
     //~ Methods ================================================================
 
+    public void setUp() {
+        SecurityContextHolder.clearContext();
+    }
+
+    public void tearDown() {
+        SecurityContextHolder.clearContext();
+    }
+
+
     public void testContextRedirect() throws Exception {
         CaptchaChannelProcessorTemplate processor = new TestHumanityCaptchaChannelProcessor();
         processor.setKeyword("X");

+ 1 - 1
core/src/test/java/org/acegisecurity/context/rmi/ContextPropagatingRemoteInvocationTests.java

@@ -66,7 +66,7 @@ public class ContextPropagatingRemoteInvocationTests extends TestCase {
         // Set to null, as ContextPropagatingRemoteInvocation already obtained
         // a copy and nulling is necessary to ensure the Context delivered by
         // ContextPropagatingRemoteInvocation is used on server-side
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
 
         // The result from invoking the TargetObject should contain the
         // Authentication class delivered via the SecurityContextHolder

+ 2 - 2
core/src/test/java/org/acegisecurity/intercept/web/FilterSecurityInterceptorTests.java

@@ -176,7 +176,7 @@ public class FilterSecurityInterceptorTests extends TestCase {
         interceptor.invoke(fi);
 
         // Destroy the Context
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
     }
 
     public void testNormalStartupAndGetter() throws Exception {
@@ -233,7 +233,7 @@ public class FilterSecurityInterceptorTests extends TestCase {
         interceptor.invoke(fi);
 
         // Destroy the Context
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
     }
 
     //~ Inner Classes ==========================================================

+ 2 - 2
core/src/test/java/org/acegisecurity/providers/anonymous/AnonymousProcessingFilterTests.java

@@ -164,12 +164,12 @@ public class AnonymousProcessingFilterTests extends TestCase {
 
     protected void setUp() throws Exception {
         super.setUp();
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
     }
 
     protected void tearDown() throws Exception {
         super.tearDown();
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
     }
 
     private void executeFilterInContainerSimulator(FilterConfig filterConfig,

+ 2 - 2
core/src/test/java/org/acegisecurity/providers/jaas/SecurityContextLoginModuleTests.java

@@ -111,11 +111,11 @@ public class SecurityContextLoginModuleTests extends TestCase {
     protected void setUp() throws Exception {
         module = new SecurityContextLoginModule();
         module.initialize(subject, null, null, null);
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
     }
 
     protected void tearDown() throws Exception {
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
         module = null;
     }
 }

+ 1 - 1
core/src/test/java/org/acegisecurity/taglibs/authz/AuthorizeTagAttributeTests.java

@@ -98,6 +98,6 @@ public class AuthorizeTagAttributeTests extends TestCase {
     }
 
     protected void tearDown() throws Exception {
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
     }
 }

+ 1 - 1
core/src/test/java/org/acegisecurity/taglibs/authz/AuthorizeTagCustomGrantedAuthorityTests.java

@@ -73,7 +73,7 @@ public class AuthorizeTagCustomGrantedAuthorityTests extends TestCase {
     }
 
     protected void tearDown() throws Exception {
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
     }
 
     //~ Inner Classes ==========================================================

+ 1 - 1
core/src/test/java/org/acegisecurity/taglibs/authz/AuthorizeTagExpressionLanguageTests.java

@@ -81,6 +81,6 @@ public class AuthorizeTagExpressionLanguageTests extends TestCase {
     }
 
     protected void tearDown() throws Exception {
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
     }
 }

+ 1 - 1
core/src/test/java/org/acegisecurity/taglibs/authz/AuthorizeTagTests.java

@@ -120,6 +120,6 @@ public class AuthorizeTagTests extends TestCase {
     }
 
     protected void tearDown() throws Exception {
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
     }
 }

+ 1 - 1
core/src/test/java/org/acegisecurity/taglibs/velocity/AuthzImplAttributeTest.java

@@ -51,7 +51,7 @@ public class AuthzImplAttributeTest extends TestCase {
     }
 
     protected void tearDown() throws Exception {
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
     }
 
     public void testAssertsIfAllGrantedSecond() {

+ 1 - 1
core/src/test/java/org/acegisecurity/taglibs/velocity/AuthzImplAuthorizeTagTest.java

@@ -49,7 +49,7 @@ public class AuthzImplAuthorizeTagTest extends TestCase {
     }
 
     protected void tearDown() throws Exception {
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
     }
 
     public void testAlwaysReturnsUnauthorizedIfNoUserFound() {

+ 2 - 2
core/src/test/java/org/acegisecurity/ui/AbstractProcessingFilterTests.java

@@ -419,12 +419,12 @@ public class AbstractProcessingFilterTests extends TestCase {
 
     protected void setUp() throws Exception {
         super.setUp();
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
     }
 
     protected void tearDown() throws Exception {
         super.tearDown();
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
     }
 
     private MockHttpServletRequest createMockRequest() {

+ 1 - 1
core/src/test/java/org/acegisecurity/ui/ExceptionTranslationFilterTests.java

@@ -67,7 +67,7 @@ public class ExceptionTranslationFilterTests extends TestCase {
 
     protected void tearDown() throws Exception {
         super.tearDown();
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
     }
 
     public void testAccessDeniedWhenAnonymous() throws Exception {

+ 2 - 2
core/src/test/java/org/acegisecurity/ui/basicauth/BasicProcessingFilterTests.java

@@ -74,7 +74,7 @@ public class BasicProcessingFilterTests extends MockObjectTestCase {
 
     protected void setUp() throws Exception {
         super.setUp();
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
 
         // Create User Details Service, provider and authentication manager
         InMemoryDaoImpl dao = new InMemoryDaoImpl();
@@ -97,7 +97,7 @@ public class BasicProcessingFilterTests extends MockObjectTestCase {
 
     protected void tearDown() throws Exception {
         super.tearDown();
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
     }
 
     public void testDoFilterWithNonHttpServletRequestDetected()

+ 2 - 2
core/src/test/java/org/acegisecurity/ui/digestauth/DigestProcessingFilterTests.java

@@ -86,7 +86,7 @@ public class DigestProcessingFilterTests extends MockObjectTestCase {
 
     protected void setUp() throws Exception {
         super.setUp();
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
         // Create User Details Service
         InMemoryDaoImpl dao = new InMemoryDaoImpl();
         UserMapEditor editor = new UserMapEditor();
@@ -107,7 +107,7 @@ public class DigestProcessingFilterTests extends MockObjectTestCase {
 
     protected void tearDown() throws Exception {
         super.tearDown();
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
     }
 
     public void testDoFilterWithNonHttpServletRequestDetected()

+ 2 - 2
core/src/test/java/org/acegisecurity/ui/rememberme/RememberMeProcessingFilterTests.java

@@ -76,12 +76,12 @@ public class RememberMeProcessingFilterTests extends TestCase {
 
     protected void setUp() throws Exception {
         super.setUp();
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
     }
 
     protected void tearDown() throws Exception {
         super.tearDown();
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
     }
 
     public void testDetectsAuthenticationManagerProperty()

+ 1 - 1
samples/contacts/src/main/java/sample/contact/ClientApplication.java

@@ -137,7 +137,7 @@ public class ClientApplication {
             System.out.println(stopWatch.prettyPrint());
         }
 
-        SecurityContextHolder.setContext(new SecurityContextImpl());
+        SecurityContextHolder.clearContext();
     }
 
     public static void main(String[] args) {