Added stack based context holder strategy to cope with direct channels
This commit is contained in:
@@ -20,8 +20,10 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.dispatcher.DirectChannel;
|
||||
import org.springframework.integration.handler.MessageHandler;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
@@ -36,6 +38,11 @@ import org.springframework.security.context.SecurityContextHolder;
|
||||
*/
|
||||
public class SecurityContextAssociatingHandlerInterceptorTests {
|
||||
|
||||
@After
|
||||
public void clearSecurityContext(){
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessageWithSecurityContext() {
|
||||
final StubSecurityContext securityContext = new StubSecurityContext();
|
||||
@@ -97,6 +104,27 @@ public class SecurityContextAssociatingHandlerInterceptorTests {
|
||||
assertNull("Security context still present after handler returned",
|
||||
SecurityContextHolder.getContext().getAuthentication());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExistingSecurityContextIsNotCleared(){
|
||||
SecurityContextHolder.setStrategyName(StackBasedSecurityContextHolderStrategy.class.getName());
|
||||
final StubSecurityContext securityContext = new StubSecurityContext();
|
||||
SecurityContextHolder.setContext(securityContext);
|
||||
|
||||
StringMessage message = new StringMessage("test");
|
||||
|
||||
final MessageHandler handler = new MessageHandler() {
|
||||
public Message<?> handle(Message<?> message) {
|
||||
SecurityContext associatedContext = SecurityContextHolder.getContext();
|
||||
assertEquals("Wrong security context", securityContext, associatedContext);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
SecurityContextAssociatingHandlerInterceptor associatingInterceptor =
|
||||
new SecurityContextAssociatingHandlerInterceptor(handler);
|
||||
associatingInterceptor.handle(message);
|
||||
assertEquals("Security context no logner set", securityContext, SecurityContextHolder.getContext());
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
|
||||
@@ -23,7 +23,6 @@ import static org.junit.Assert.assertTrue;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.message.MessageHeader;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
@@ -78,11 +77,15 @@ public class SecurityContextPropagatingChannelInterceptorTests {
|
||||
channel.send(message);
|
||||
message = (StringMessage) channel.receive(0);
|
||||
MessageHeader header = message.getHeader();
|
||||
|
||||
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
||||
|
||||
assertFalse("Security context header found when no security context existed.",
|
||||
header.getAttributeNames().contains(SecurityContextPropagatingChannelInterceptor.SECURITY_CONTEXT_HEADER_ATTRIBUTE));
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void associateContextWithThread(){
|
||||
SecurityContextHolder.setContext(securityContext);
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.integration.security;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -29,6 +30,7 @@ import org.springframework.security.Authentication;
|
||||
import org.springframework.security.ConfigAttribute;
|
||||
import org.springframework.security.ConfigAttributeDefinition;
|
||||
import org.springframework.security.InsufficientAuthenticationException;
|
||||
import org.springframework.security.context.SecurityContextHolder;
|
||||
|
||||
/**
|
||||
* @author Jonas Partner
|
||||
@@ -44,7 +46,11 @@ public class SecurityEnforcingChannelInterceptorTests {
|
||||
public void setUp() {
|
||||
channel = new QueueChannel();
|
||||
}
|
||||
|
||||
|
||||
@After
|
||||
public void clearSecurityContext(){
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
@Test(expected = AccessDeniedException.class)
|
||||
public void testSendSecuredAndAccessDenied() {
|
||||
|
||||
@@ -54,6 +54,7 @@ public class SecureChannelsParserTests {
|
||||
if (applicationContext != null) {
|
||||
applicationContext.close();
|
||||
}
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user