The <channel-adapter/> now actually creates a channel instance rather than requiring another distinct channel object. Instead of configuring the poller on the channel-adapter, it is currently to be configured on the consuming endpoint just as if the <channel-adapter/> were any other pollable channel (e.g. <queue-channel/>).
This commit is contained in:
@@ -20,6 +20,8 @@ import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.channel.interceptor.ChannelInterceptorAdapter;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.security.AccessDecisionManager;
|
||||
import org.springframework.security.Authentication;
|
||||
import org.springframework.security.AuthenticationCredentialsNotFoundException;
|
||||
import org.springframework.security.ConfigAttributeDefinition;
|
||||
import org.springframework.security.context.SecurityContextHolder;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -29,6 +31,7 @@ import org.springframework.util.Assert;
|
||||
* enforce the security on the send and receive calls of the {@link MessageChannel}.
|
||||
*
|
||||
* @author Jonas Partner
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class SecurityEnforcingChannelInterceptor extends ChannelInterceptorAdapter {
|
||||
|
||||
@@ -83,8 +86,12 @@ public class SecurityEnforcingChannelInterceptor extends ChannelInterceptorAdapt
|
||||
|
||||
private void checkPermission(MessageChannel messageChannel, ConfigAttributeDefinition securityAttributes) {
|
||||
if (securityAttributes != null) {
|
||||
this.accessDecisionManger.decide(SecurityContextHolder.getContext().getAuthentication(), messageChannel,
|
||||
securityAttributes);
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication == null) {
|
||||
throw new AuthenticationCredentialsNotFoundException(
|
||||
"No Authentication object available. Consider enabling the SecurityPropagatingBeanPostProcessor.");
|
||||
}
|
||||
this.accessDecisionManger.decide(authentication, messageChannel, securityAttributes);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/integration"
|
||||
xmlns:si-security="http://www.springframework.org/schema/integration-security"
|
||||
xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:security="http://www.springframework.org/schema/security"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
|
||||
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd
|
||||
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
|
||||
http://www.springframework.org/schema/integration-security http://www.springframework.org/schema/integration/spring-integration-security-1.0.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
|
||||
|
||||
<beans:import resource="classpath:org/springframework/integration/security/config/commonSecurityConfiguration.xml"/>
|
||||
|
||||
<message-bus auto-create-channels="false" />
|
||||
|
||||
<si-security:secured-channels send-access="ROLE_ADMIN">
|
||||
<si-security:channel-name-pattern>secured.*</si-security:channel-name-pattern>
|
||||
</si-security:secured-channels>
|
||||
|
||||
<beans:bean id="testTarget"
|
||||
class="org.springframework.integration.security.endpoint.TestTarget"/>
|
||||
|
||||
<channel-adapter id="securedChannelAdapter" target="testTarget"/>
|
||||
|
||||
<channel-adapter id="unsecuredChannelAdapter" target="testTarget"/>
|
||||
|
||||
</beans:beans>
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.security.channel;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.integration.security.SecurityTestUtil;
|
||||
import org.springframework.integration.security.endpoint.TestTarget;
|
||||
import org.springframework.security.AccessDeniedException;
|
||||
import org.springframework.security.AuthenticationException;
|
||||
import org.springframework.security.context.SecurityContext;
|
||||
import org.springframework.security.context.SecurityContextHolder;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
@ContextConfiguration
|
||||
public class ChannelAdapterSecurityIntegrationTests extends AbstractJUnit4SpringContextTests {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("securedChannelAdapter")
|
||||
MessageChannel securedChannelAdapter;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("unsecuredChannelAdapter")
|
||||
MessageChannel unsecuredChannelAdapter;
|
||||
|
||||
@Autowired
|
||||
TestTarget testTarget;
|
||||
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void testSecuredWithPermission() {
|
||||
login("bob", "bobspassword", "ROLE_ADMIN");
|
||||
securedChannelAdapter.send(new StringMessage("test"));
|
||||
assertEquals("Wrong size of message list in target", 1, testTarget.sentMessages.size());
|
||||
}
|
||||
|
||||
@Test(expected = AccessDeniedException.class)
|
||||
@DirtiesContext
|
||||
public void testSecuredWithoutPermision() {
|
||||
login("bob", "bobspassword", "ROLE_USER");
|
||||
securedChannelAdapter.send(new StringMessage("test"));
|
||||
}
|
||||
|
||||
@Test(expected = AuthenticationException.class)
|
||||
@DirtiesContext
|
||||
public void testSecuredWithoutAuthenticating() {
|
||||
securedChannelAdapter.send(new StringMessage("test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void testUnsecuredAsAdmin() {
|
||||
login("bob", "bobspassword", "ROLE_ADMIN");
|
||||
unsecuredChannelAdapter.send(new StringMessage("test"));
|
||||
assertEquals("Wrong size of message list in target", 1, testTarget.sentMessages.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void testUnsecuredAsUser() {
|
||||
login("bob", "bobspassword", "ROLE_USER");
|
||||
unsecuredChannelAdapter.send(new StringMessage("test"));
|
||||
assertEquals("Wrong size of message list in target", 1, testTarget.sentMessages.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DirtiesContext
|
||||
public void testUnsecuredWithoutAuthenticating() {
|
||||
unsecuredChannelAdapter.send(new StringMessage("test"));
|
||||
assertEquals("Wrong size of message list in target", 1, testTarget.sentMessages.size());
|
||||
}
|
||||
|
||||
|
||||
private void login(String username, String password, String... roles) {
|
||||
SecurityContext context = SecurityTestUtil.createContext(username, password, roles);
|
||||
SecurityContextHolder.setContext(context);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -30,8 +30,10 @@ import org.springframework.security.AccessDeniedException;
|
||||
import org.springframework.security.Authentication;
|
||||
import org.springframework.security.ConfigAttribute;
|
||||
import org.springframework.security.ConfigAttributeDefinition;
|
||||
import org.springframework.security.GrantedAuthorityImpl;
|
||||
import org.springframework.security.InsufficientAuthenticationException;
|
||||
import org.springframework.security.context.SecurityContextHolder;
|
||||
import org.springframework.security.providers.TestingAuthenticationToken;
|
||||
|
||||
/**
|
||||
* @author Jonas Partner
|
||||
@@ -46,8 +48,10 @@ public class SecurityEnforcingChannelInterceptorTests {
|
||||
@Before
|
||||
public void setUp() {
|
||||
channel = new QueueChannel();
|
||||
SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(
|
||||
"stub", "passwd", new GrantedAuthorityImpl[] {}));
|
||||
}
|
||||
|
||||
|
||||
@After
|
||||
public void clearSecurityContext(){
|
||||
SecurityContextHolder.clearContext();
|
||||
|
||||
@@ -11,33 +11,26 @@
|
||||
http://www.springframework.org/schema/integration-security http://www.springframework.org/schema/integration/spring-integration-security-1.0.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
|
||||
|
||||
|
||||
<beans:import resource="classpath:org/springframework/integration/security/config/commonSecurityConfiguration.xml"/>
|
||||
|
||||
<si-security:secured-channels send-access="ROLE_ADMIN">
|
||||
<si-security:channel-name-pattern>adminRequiredForSend</si-security:channel-name-pattern>
|
||||
</si-security:secured-channels>
|
||||
|
||||
|
||||
<si-security:secured-channels send-access="ROLE_ADMIN, ROLE_USER">
|
||||
<si-security:channel-name-pattern>adminOrUserRequiredForSend</si-security:channel-name-pattern>
|
||||
</si-security:secured-channels>
|
||||
|
||||
|
||||
<si-security:secured-channels receive-access="ROLE_ADMIN">
|
||||
<si-security:channel-name-pattern>adminRequiredForReceive</si-security:channel-name-pattern>
|
||||
</si-security:secured-channels>
|
||||
|
||||
|
||||
<si-security:secured-channels receive-access="ROLE_ADMIN, ROLE_USER">
|
||||
<si-security:channel-name-pattern>adminOrUserRequiredForReceive</si-security:channel-name-pattern>
|
||||
</si-security:secured-channels>
|
||||
|
||||
|
||||
<si-security:secured-channels receive-access="ROLE_ADMIN" send-access="ROLE_ADMIN">
|
||||
<si-security:channel-name-pattern>adminForSendAndReceive</si-security:channel-name-pattern>
|
||||
</si-security:secured-channels>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</beans:beans>
|
||||
@@ -13,17 +13,24 @@
|
||||
|
||||
<message-bus auto-create-channels="false" />
|
||||
|
||||
<beans:bean id="testTarget"
|
||||
class="org.springframework.integration.security.endpoint.TestTarget"/>
|
||||
<direct-channel id="input"/>
|
||||
|
||||
<direct-channel id="testChannel"/>
|
||||
|
||||
<channel-adapter target="testTarget" channel="testChannel">
|
||||
<service-activator id="endpoint"
|
||||
input-channel="input"
|
||||
output-channel="output"
|
||||
ref="testHandler">
|
||||
<interceptors>
|
||||
<si-security:endpoint-security-policy access="ROLE_ADMIN" />
|
||||
</interceptors>
|
||||
</channel-adapter>
|
||||
</service-activator>
|
||||
|
||||
<channel-adapter id="output" target="testTarget"/>
|
||||
|
||||
<beans:bean id="testHandler"
|
||||
class="org.springframework.integration.security.endpoint.TestHandler"/>
|
||||
|
||||
<beans:bean id="testTarget"
|
||||
class="org.springframework.integration.security.endpoint.TestTarget"/>
|
||||
|
||||
<beans:bean id="accessDecisionManager"
|
||||
class="org.springframework.security.vote.AffirmativeBased">
|
||||
|
||||
@@ -13,13 +13,16 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.security.endpoint;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.integration.security.SecurityTestUtil;
|
||||
@@ -30,15 +33,24 @@ import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
|
||||
|
||||
/**
|
||||
* @author Jonas Partner
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
@ContextConfiguration
|
||||
public class EndpointSecurityIntegrationTest extends AbstractJUnit4SpringContextTests {
|
||||
|
||||
@Autowired
|
||||
MessageChannel channel;
|
||||
@Qualifier("input")
|
||||
MessageChannel input;
|
||||
|
||||
@Autowired
|
||||
TestHandler testHandler;
|
||||
|
||||
@Autowired
|
||||
TestTarget testTarget;
|
||||
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
SecurityContextHolder.clearContext();
|
||||
@@ -48,22 +60,20 @@ public class EndpointSecurityIntegrationTest extends AbstractJUnit4SpringContext
|
||||
@DirtiesContext
|
||||
public void testWithPermision() {
|
||||
login("bob", "bobspassword", "ROLE_ADMIN");
|
||||
channel.send(new StringMessage("test"));
|
||||
assertEquals("Wrong size of message list in target ", 1, testTarget.sentMessages.size());
|
||||
input.send(new StringMessage("test"));
|
||||
assertEquals("Wrong size of message list in handler", 1, testHandler.sentMessages.size());
|
||||
assertEquals("Wrong size of message list in target", 1, testTarget.sentMessages.size());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Test(expected = AccessDeniedException.class)
|
||||
@DirtiesContext
|
||||
public void testWithoutPermision() {
|
||||
login("bob", "bobspassword", "ROLE_USER");
|
||||
channel.send(new StringMessage("test"));
|
||||
assertEquals("Wrong size of message list in target ", 1, testTarget.sentMessages.size());
|
||||
input.send(new StringMessage("test"));
|
||||
}
|
||||
|
||||
public void login(String username, String password, String... roles) {
|
||||
|
||||
private void login(String username, String password, String... roles) {
|
||||
SecurityContext context = SecurityTestUtil.createContext(username, password, roles);
|
||||
SecurityContextHolder.setContext(context);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.security.endpoint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.integration.handler.MessageHandler;
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class TestHandler implements MessageHandler {
|
||||
|
||||
public List<Message<?>> sentMessages = new ArrayList<Message<?>>();
|
||||
|
||||
public Message<?> handle(Message<?> message) {
|
||||
sentMessages.add(message);
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user