* Fix Sonar issues for Sec., STOMP, SFTP, WebFlux

This commit is contained in:
Artem Bilan
2018-12-19 15:25:27 -05:00
committed by Gary Russell
parent 7790f9e550
commit 761af2730c
8 changed files with 342 additions and 260 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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.
@@ -27,10 +27,12 @@ import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator;
import org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.integration.security.channel.ChannelAccessPolicy;
import org.springframework.integration.security.channel.ChannelSecurityInterceptor;
import org.springframework.integration.security.channel.ChannelSecurityMetadataSource;
import org.springframework.lang.Nullable;
import org.springframework.messaging.MessageChannel;
/**
@@ -53,6 +55,7 @@ public class ChannelSecurityInterceptorBeanPostProcessor extends AbstractAutoPro
public ChannelSecurityInterceptorBeanPostProcessor(Map<String, Set<Pattern>> securityInterceptorMappings,
Map<String, Map<Pattern, ChannelAccessPolicy>> accessPolicyMapping) {
this.securityInterceptorMappings = securityInterceptorMappings; //NOSONAR (inconsistent sync)
this.accessPolicyMapping = accessPolicyMapping; //NOSONAR (inconsistent sync)
}
@@ -73,19 +76,24 @@ public class ChannelSecurityInterceptorBeanPostProcessor extends AbstractAutoPro
}
@Override
@Nullable
protected Object[] getAdvicesAndAdvisorsForBean(Class<?> beanClass, String beanName,
TargetSource customTargetSource) throws BeansException {
@Nullable TargetSource customTargetSource) throws BeansException {
if (MessageChannel.class.isAssignableFrom(beanClass)) {
List<Advisor> interceptors = new ArrayList<Advisor>();
List<Advisor> interceptors = new ArrayList<>();
for (Map.Entry<String, Set<Pattern>> entry : this.securityInterceptorMappings.entrySet()) {
if (isMatch(beanName, entry.getValue())) {
DefaultBeanFactoryPointcutAdvisor channelSecurityInterceptor
= new DefaultBeanFactoryPointcutAdvisor();
channelSecurityInterceptor.setAdviceBeanName(entry.getKey());
channelSecurityInterceptor.setBeanFactory(getBeanFactory());
interceptors.add(channelSecurityInterceptor);
DefaultBeanFactoryPointcutAdvisor channelSecurityInterceptor =
new DefaultBeanFactoryPointcutAdvisor();
channelSecurityInterceptor.setAdviceBeanName(entry.getKey());
BeanFactory beanFactory = getBeanFactory();
if (beanFactory != null) {
channelSecurityInterceptor.setBeanFactory(beanFactory);
}
interceptors.add(channelSecurityInterceptor);
}
}
if (!interceptors.isEmpty()) {
return interceptors.toArray();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2018 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.
@@ -18,12 +18,14 @@ package org.springframework.integration.security.config;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.ManagedMap;
@@ -39,6 +41,7 @@ import org.springframework.integration.security.channel.SecuredChannel;
* The Integration Security infrastructure {@code beanFactory} initializer.
*
* @author Artem Bilan
*
* @since 4.0
*/
public class SecurityIntegrationConfigurationInitializer implements IntegrationConfigurationInitializer {
@@ -47,71 +50,30 @@ public class SecurityIntegrationConfigurationInitializer implements IntegrationC
ChannelSecurityInterceptorBeanPostProcessor.class.getName();
@Override
@SuppressWarnings("unchecked")
public void initialize(ConfigurableListableBeanFactory beanFactory) throws BeansException {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
Map<String, ManagedSet<String>> securityInterceptors = new ManagedMap<String, ManagedSet<String>>();
Map<String, Map<Pattern, ChannelAccessPolicy>> policies = new HashMap<String, Map<Pattern, ChannelAccessPolicy>>();
Map<String, Set<String>> securityInterceptors = new ManagedMap<>();
Map<String, Map<Pattern, ChannelAccessPolicy>> policies = new HashMap<>();
for (String beanName : registry.getBeanDefinitionNames()) {
BeanDefinition beanDefinition = registry.getBeanDefinition(beanName);
if (ChannelSecurityInterceptor.class.getName().equals(beanDefinition.getBeanClassName())) {
BeanDefinition metadataSource = (BeanDefinition) beanDefinition.getConstructorArgumentValues()
.getIndexedArgumentValue(0, BeanDefinition.class)
.getValue();
Map<String, ?> value = (Map<String, ?>) metadataSource.getConstructorArgumentValues()
.getIndexedArgumentValue(0, Map.class)
.getValue();
ManagedSet<String> patterns = new ManagedSet<String>();
if (!securityInterceptors.containsKey(beanName)) {
securityInterceptors.put(beanName, patterns);
}
else {
patterns = securityInterceptors.get(beanName);
}
patterns.addAll(value.keySet());
collectPatternsFromInterceptor(securityInterceptors, beanName, beanDefinition);
}
else if (beanDefinition instanceof AnnotatedBeanDefinition) {
if (beanDefinition.getSource() instanceof MethodMetadata) {
MethodMetadata beanMethod = (MethodMetadata) beanDefinition.getSource();
String annotationType = SecuredChannel.class.getName();
if (beanMethod.isAnnotated(annotationType)) {
Map<String, Object> securedAttributes = beanMethod.getAnnotationAttributes(annotationType);
String[] interceptors = (String[]) securedAttributes.get("interceptor");
String[] sendAccess = (String[]) securedAttributes.get("sendAccess");
String[] receiveAccess = (String[]) securedAttributes.get("receiveAccess");
ChannelAccessPolicy accessPolicy = new DefaultChannelAccessPolicy(sendAccess, receiveAccess);
for (String interceptor : interceptors) {
ManagedSet<String> patterns = new ManagedSet<String>();
if (!securityInterceptors.containsKey(interceptor)) {
securityInterceptors.put(interceptor, patterns);
}
else {
patterns = securityInterceptors.get(interceptor);
}
patterns.add(beanName);
Map<Pattern, ChannelAccessPolicy> mapping = new HashMap<Pattern, ChannelAccessPolicy>();
if (!policies.containsKey(interceptor)) {
policies.put(interceptor, mapping);
}
else {
mapping = policies.get(interceptor);
}
mapping.put(Pattern.compile(beanName), accessPolicy);
}
}
Object beanSource = beanDefinition.getSource();
if (beanSource instanceof MethodMetadata) {
collectInterceptorsAndPoliciesBySecuredChannel(securityInterceptors, policies, beanName,
(MethodMetadata) beanSource);
}
}
}
if (!securityInterceptors.isEmpty()) {
BeanDefinitionBuilder builder =
BeanDefinitionBuilder.rootBeanDefinition(ChannelSecurityInterceptorBeanPostProcessor.class)
.addConstructorArgValue(securityInterceptors);
.addConstructorArgValue(securityInterceptors);
if (!policies.isEmpty()) {
builder.addConstructorArgValue(policies);
}
@@ -119,4 +81,67 @@ public class SecurityIntegrationConfigurationInitializer implements IntegrationC
}
}
@SuppressWarnings("unchecked")
private void collectPatternsFromInterceptor(Map<String, Set<String>> securityInterceptors, String beanName,
BeanDefinition beanDefinition) {
ConstructorArgumentValues.ValueHolder metadataSourceValueHolder =
beanDefinition
.getConstructorArgumentValues()
.getIndexedArgumentValue(0, BeanDefinition.class);
if (metadataSourceValueHolder != null) {
BeanDefinition metadataSource = (BeanDefinition) metadataSourceValueHolder.getValue();
if (metadataSource != null) {
ConstructorArgumentValues.ValueHolder patternMappingsValueHolder =
metadataSource
.getConstructorArgumentValues()
.getIndexedArgumentValue(0, Map.class);
if (patternMappingsValueHolder != null) {
Map<String, ?> patternsToAdd = (Map<String, ?>) patternMappingsValueHolder.getValue();
Set<String> patterns = new ManagedSet<>();
if (!securityInterceptors.containsKey(beanName)) {
securityInterceptors.put(beanName, patterns);
}
else {
patterns = securityInterceptors.get(beanName);
}
if (patternsToAdd != null) {
patterns.addAll(patternsToAdd.keySet());
}
}
}
}
}
private void collectInterceptorsAndPoliciesBySecuredChannel(Map<String, Set<String>> securityInterceptors,
Map<String, Map<Pattern, ChannelAccessPolicy>> policies, String beanName, MethodMetadata beanMethod) {
Map<String, Object> securedAttributes = beanMethod.getAnnotationAttributes(SecuredChannel.class.getName());
if (securedAttributes != null) {
String[] interceptors = (String[]) securedAttributes.get("interceptor");
String[] sendAccess = (String[]) securedAttributes.get("sendAccess");
String[] receiveAccess = (String[]) securedAttributes.get("receiveAccess");
ChannelAccessPolicy accessPolicy = new DefaultChannelAccessPolicy(sendAccess, receiveAccess);
for (String interceptor : interceptors) {
Set<String> patterns = new ManagedSet<>();
if (!securityInterceptors.containsKey(interceptor)) {
securityInterceptors.put(interceptor, patterns);
}
else {
patterns = securityInterceptors.get(interceptor);
}
patterns.add(beanName);
Map<Pattern, ChannelAccessPolicy> mapping = new HashMap<>();
if (!policies.containsKey(interceptor)) {
policies.put(interceptor, mapping);
}
else {
mapping = policies.get(interceptor);
}
mapping.put(Pattern.compile(beanName), accessPolicy);
}
}
}
}