Fix for SEC-159. Added clearContext() method to SecurityContextHolder and refactored code to use it instead of putting an empty context into the holder.
This commit is contained in:
parent
8c0ce12332
commit
dc959b1847
|
@ -279,7 +279,7 @@ public class HttpSessionContextIntegrationFilter implements InitializingBean,
|
|||
}
|
||||
|
||||
// Remove SecurityContextHolder contents
|
||||
SecurityContextHolder.setContext(generateNewContext());
|
||||
SecurityContextHolder.clearContext();
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -123,7 +123,7 @@ public class ContextPropagatingRemoteInvocation extends RemoteInvocation {
|
|||
|
||||
} finally {
|
||||
|
||||
SecurityContextHolder.setContext(new SecurityContextImpl());
|
||||
SecurityContextHolder.clearContext();
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 ==========================================================
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -98,6 +98,6 @@ public class AuthorizeTagAttributeTests extends TestCase {
|
|||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
SecurityContextHolder.setContext(new SecurityContextImpl());
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -73,7 +73,7 @@ public class AuthorizeTagCustomGrantedAuthorityTests extends TestCase {
|
|||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
SecurityContextHolder.setContext(new SecurityContextImpl());
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
//~ Inner Classes ==========================================================
|
||||
|
|
|
@ -81,6 +81,6 @@ public class AuthorizeTagExpressionLanguageTests extends TestCase {
|
|||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
SecurityContextHolder.setContext(new SecurityContextImpl());
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -120,6 +120,6 @@ public class AuthorizeTagTests extends TestCase {
|
|||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
SecurityContextHolder.setContext(new SecurityContextImpl());
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@ public class AuthzImplAttributeTest extends TestCase {
|
|||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
SecurityContextHolder.setContext(new SecurityContextImpl());
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
public void testAssertsIfAllGrantedSecond() {
|
||||
|
|
|
@ -49,7 +49,7 @@ public class AuthzImplAuthorizeTagTest extends TestCase {
|
|||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
SecurityContextHolder.setContext(new SecurityContextImpl());
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
public void testAlwaysReturnsUnauthorizedIfNoUserFound() {
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -137,7 +137,7 @@ public class ClientApplication {
|
|||
System.out.println(stopWatch.prettyPrint());
|
||||
}
|
||||
|
||||
SecurityContextHolder.setContext(new SecurityContextImpl());
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
Loading…
Reference in New Issue