Namespace support for endpoint interceptor security

This commit is contained in:
Jonas Partner
2008-07-03 14:21:08 +00:00
parent c59bbe767a
commit 27ac25ed33
12 changed files with 270 additions and 42 deletions

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.security.config;
package org.springframework.integration.security;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;

View File

@@ -1,6 +1,6 @@
<?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: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"
@@ -11,10 +11,15 @@
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">
<si-security:security-propagating-channels propagate-by-default="false">
<si-security:security-propagating-channels
propagate-by-default="false">
<si-security:propagation-patterns>
<si-security:excludePattern>adminSpecial</si-security:excludePattern>
<si-security:excludePattern>admin.*</si-security:excludePattern>
<si-security:excludePattern>
adminSpecial
</si-security:excludePattern>
<si-security:includePattern>
admin.*
</si-security:includePattern>
</si-security:propagation-patterns>
</si-security:security-propagating-channels>

View File

@@ -0,0 +1,49 @@
<?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">
<message-bus auto-create-channels="false" />
<beans:bean id="testTarget"
class="org.springframework.integration.security.endpoint.TestTarget" />
<direct-channel id="testChannel"></direct-channel>
<target-endpoint target="testTarget" input-channel="testChannel">
<interceptors>
<si-security:endpoint-security-policy access="ROLE_ADMIN" />
</interceptors>
</target-endpoint>
<beans:bean id="accessDecisionManager"
class="org.springframework.security.vote.AffirmativeBased">
<beans:property name="allowIfAllAbstainDecisions" value="true" />
<beans:property name="decisionVoters">
<beans:list>
<beans:bean
class="org.springframework.security.vote.RoleVoter" />
</beans:list>
</beans:property>
</beans:bean>
<security:authentication-provider
user-service-ref="userDetailsService" />
<security:user-service id="userDetailsService">
<security:user name="jimi" password="jimispassword"
authorities="ROLE_USER, ROLE_ADMIN" />
<security:user name="bob" password="bobspassword"
authorities="ROLE_USER" />
</security:user-service>
</beans:beans>

View File

@@ -0,0 +1,71 @@
/*
* 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 static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.security.SecurityTestUtil;
import org.springframework.security.AccessDeniedException;
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;
@ContextConfiguration
public class EnpointSecurityIntegrationTest extends AbstractJUnit4SpringContextTests {
@Autowired
MessageChannel channel;
@Autowired
TestTarget testTarget;
@After
public void tearDown() {
SecurityContextHolder.clearContext();
}
@Test
@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());
}
/**
*
*/
@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());
}
public void login(String username, String password, String... roles) {
SecurityContext context = SecurityTestUtil.createContext(username, password, roles);
SecurityContextHolder.setContext(context);
}
}

View File

@@ -25,7 +25,7 @@ import org.junit.Test;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.security.SecurityContextUtils;
import org.springframework.integration.security.config.SecurityTestUtil;
import org.springframework.integration.security.SecurityTestUtil;
import org.springframework.security.AccessDecisionManager;
import org.springframework.security.AccessDeniedException;
import org.springframework.security.ConfigAttributeDefinition;

View File

@@ -0,0 +1,34 @@
/*
* 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.message.Message;
import org.springframework.integration.message.MessageTarget;
public class TestTarget implements MessageTarget {
public List<Message<?>> sentMessages = new ArrayList<Message<?>>();
public boolean send(Message<?> message) {
sentMessages.add(message);
return true;
}
}