INT-1408, added changes based on Luke's comments

This commit is contained in:
Oleg Zhurakousky
2010-09-03 13:32:15 +00:00
parent c00c75d5d5
commit 3f390274db
7 changed files with 110 additions and 78 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2010 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.
@@ -13,72 +13,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.security.channel;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Creates the {@link ConfigAttribute}s for secured channel
* send and receive operations based on simple String values.
* Interface to encapsulate {@link ConfigAttribute}s for secured channel
* send and receive operations.
*
* @author Mark Fisher
* @author Oleg Zhurakousky
* @since 2.0
*/
public class ChannelAccessPolicy {
public interface ChannelAccessPolicy {
public Collection<ConfigAttribute> getConfigAttributesForSend();
private final Collection<ConfigAttribute> configAttributeDefinitionForSend;
private final Collection<ConfigAttribute> configAttributeDefinitionForReceive;
/**
* Create an access policy instance. The provided 'sendAccess' and 'receiveAccess'
* values may be a single String or a comma-delimited list of values. All whitespace
* will be trimmed. A <code>null</code> value indicates that the policy does not
* apply for either send or receive access type. At most one of the values may be null.
*/
@SuppressWarnings("unchecked")
public ChannelAccessPolicy(String sendAccess, String receiveAccess) {
boolean sendAccessDefined = StringUtils.hasText(sendAccess);
boolean recieveAccessDefined = StringUtils.hasText(receiveAccess);
Assert.isTrue(sendAccessDefined || recieveAccessDefined,
"At least one of 'sendAccess' and 'receiveAccess' must not be null.");
if (sendAccessDefined){
String[] sendAccessValues = StringUtils.commaDelimitedListToStringArray(sendAccess);
configAttributeDefinitionForSend = new HashSet<ConfigAttribute>();
for (String sendAccessValue : sendAccessValues) {
configAttributeDefinitionForSend.add(new SecurityConfig(StringUtils.trimAllWhitespace(sendAccessValue)));
}
} else {
configAttributeDefinitionForSend = Collections.EMPTY_SET;
}
if (recieveAccessDefined){
String[] receiveAccessValues = StringUtils.commaDelimitedListToStringArray(receiveAccess);
configAttributeDefinitionForReceive = new HashSet<ConfigAttribute>();
for (String receiveAccessValue : receiveAccessValues) {
configAttributeDefinitionForReceive.add(new SecurityConfig(StringUtils.trimAllWhitespace(receiveAccessValue)));
}
} else {
configAttributeDefinitionForReceive = Collections.EMPTY_SET;
}
}
public Collection<ConfigAttribute> getConfigAttributeDefinitionForSend() {
return this.configAttributeDefinitionForSend;
}
public Collection<ConfigAttribute> getConfigAttributeDefinitionForReceive() {
return this.configAttributeDefinitionForReceive;
}
}
public Collection<ConfigAttribute> getConfigAttributesForReceive();
}

View File

@@ -72,13 +72,13 @@ public class ChannelInvocationDefinitionSource implements SecurityMetadataSource
ChannelAccessPolicy accessPolicy = mapping.getValue();
if (pattern.matcher(channelName).matches()) {
if (invocation.isSend()) {
Collection<ConfigAttribute> definition = accessPolicy.getConfigAttributeDefinitionForSend();
Collection<ConfigAttribute> definition = accessPolicy.getConfigAttributesForSend();
if (definition != null) {
attributes.addAll(definition);
}
}
else if (invocation.isReceive()) {
Collection<ConfigAttribute> definition = accessPolicy.getConfigAttributeDefinitionForReceive();
Collection<ConfigAttribute> definition = accessPolicy.getConfigAttributesForReceive();
if (definition != null) {
attributes.addAll(definition);
}
@@ -92,9 +92,9 @@ public class ChannelInvocationDefinitionSource implements SecurityMetadataSource
Set<ConfigAttribute> allAttributes = new HashSet<ConfigAttribute>();
for (ChannelAccessPolicy policy : patternMappings.values()) {
Collection<ConfigAttribute> receiveAttributes = policy.getConfigAttributeDefinitionForReceive();
Collection<ConfigAttribute> receiveAttributes = policy.getConfigAttributesForReceive();
allAttributes.addAll(receiveAttributes);
Collection<ConfigAttribute> sendAttributes = policy.getConfigAttributeDefinitionForSend();
Collection<ConfigAttribute> sendAttributes = policy.getConfigAttributesForSend();
allAttributes.addAll(sendAttributes);
}

View File

@@ -0,0 +1,84 @@
/*
* 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 java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Creates the {@link ConfigAttribute}s for secured channel
* send and receive operations based on simple String values.
*
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
public class DefaultChannelAccessPolicy implements ChannelAccessPolicy {
private final Collection<ConfigAttribute> configAttributeDefinitionForSend;
private final Collection<ConfigAttribute> configAttributeDefinitionForReceive;
/**
* Create an access policy instance. The provided 'sendAccess' and 'receiveAccess'
* values may be a single String or a comma-delimited list of values. All whitespace
* will be trimmed. A <code>null</code> value indicates that the policy does not
* apply for either send or receive access type. At most one of the values may be null.
*/
@SuppressWarnings("unchecked")
public DefaultChannelAccessPolicy(String sendAccess, String receiveAccess) {
boolean sendAccessDefined = StringUtils.hasText(sendAccess);
boolean recieveAccessDefined = StringUtils.hasText(receiveAccess);
Assert.isTrue(sendAccessDefined || recieveAccessDefined,
"At least one of 'sendAccess' and 'receiveAccess' must not be null.");
if (sendAccessDefined){
String[] sendAccessValues = StringUtils.commaDelimitedListToStringArray(sendAccess);
configAttributeDefinitionForSend = new HashSet<ConfigAttribute>();
for (String sendAccessValue : sendAccessValues) {
configAttributeDefinitionForSend.add(new SecurityConfig(StringUtils.trimAllWhitespace(sendAccessValue)));
}
} else {
configAttributeDefinitionForSend = Collections.EMPTY_SET;
}
if (recieveAccessDefined){
String[] receiveAccessValues = StringUtils.commaDelimitedListToStringArray(receiveAccess);
configAttributeDefinitionForReceive = new HashSet<ConfigAttribute>();
for (String receiveAccessValue : receiveAccessValues) {
configAttributeDefinitionForReceive.add(new SecurityConfig(StringUtils.trimAllWhitespace(receiveAccessValue)));
}
} else {
configAttributeDefinitionForReceive = Collections.EMPTY_SET;
}
}
public Collection<ConfigAttribute> getConfigAttributesForSend() {
return this.configAttributeDefinitionForSend;
}
public Collection<ConfigAttribute> getConfigAttributesForReceive() {
return this.configAttributeDefinitionForReceive;
}
}

View File

@@ -83,7 +83,7 @@ public class SecuredChannelsParser extends AbstractSingleBeanDefinitionParser {
"At least one of 'send-access' or 'receive-access' must be provided.", accessPolicyElement);
}
BeanDefinitionBuilder accessPolicyBuilder = BeanDefinitionBuilder.genericBeanDefinition(
BASE_PACKAGE_NAME + ".channel.ChannelAccessPolicy");
BASE_PACKAGE_NAME + ".channel.DefaultChannelAccessPolicy");
accessPolicyBuilder.addConstructorArgValue(sendAccess);
accessPolicyBuilder.addConstructorArgValue(receiveAccess);
accessPolicyBuilder.getBeanDefinition().setRole(BeanDefinition.ROLE_SUPPORT);