DATAGEODE-310 - Use Environment accessible properties to enable/disable Apache Shiro and Authentication Auto-configuration.

This commit is contained in:
John Blum
2020-03-19 13:36:55 -07:00
parent 5d2bec1dc7
commit ce4a6aa2e3
2 changed files with 50 additions and 34 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020 the original author or authors.
* Copyright 2016-2020 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.
@@ -12,6 +12,7 @@
* 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.data.gemfire.config.annotation;
@@ -42,6 +43,7 @@ import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.OrderComparator;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.data.gemfire.config.annotation.support.AbstractAnnotationConfigSupport;
import org.springframework.data.gemfire.util.CollectionUtils;
@@ -102,9 +104,8 @@ public class ApacheShiroSecurityConfiguration extends AbstractAnnotationConfigSu
super.setBeanFactory(Optional.ofNullable(beanFactory)
.filter(ListableBeanFactory.class::isInstance)
.orElseThrow(() -> newIllegalArgumentException(
"BeanFactory [%s] must be an instance of ListableBeanFactory",
ObjectUtils.nullSafeClassName(beanFactory))));
.orElseThrow(() -> newIllegalArgumentException("BeanFactory [%1$s] must be an instance of [%2$s]",
ObjectUtils.nullSafeClassName(beanFactory), ListableBeanFactory.class.getName())));
}
/**
@@ -238,14 +239,24 @@ public class ApacheShiroSecurityConfiguration extends AbstractAnnotationConfigSu
protected static final String APACHE_SHIRO_LIFECYCLE_BEAN_POST_PROCESSOR_CLASS_NAME =
"org.apache.shiro.spring.LifecycleBeanPostProcessor";
public static final String SPRING_DATA_GEMFIRE_SECURITY_SHIRO_ENABLED =
"spring.data.gemfire.security.shiro.enabled";
private boolean isApacheShiroPresent(ConditionContext context) {
return ClassUtils.isPresent(APACHE_SHIRO_LIFECYCLE_BEAN_POST_PROCESSOR_CLASS_NAME,
context.getClassLoader());
}
private boolean isEnabled(Environment environment) {
return environment.getProperty(SPRING_DATA_GEMFIRE_SECURITY_SHIRO_ENABLED, Boolean.class, true);
}
/**
* @inheritDoc
*/
@Override
@SuppressWarnings("all")
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return ClassUtils.isPresent(APACHE_SHIRO_LIFECYCLE_BEAN_POST_PROCESSOR_CLASS_NAME,
context.getClassLoader());
return isEnabled(context.getEnvironment()) && isApacheShiroPresent(context);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020 the original author or authors.
* Copyright 2017-2020 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.
@@ -22,11 +22,6 @@ import java.net.URI;
import java.util.Optional;
import java.util.Properties;
import org.apache.geode.management.internal.security.ResourceConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
@@ -45,6 +40,9 @@ import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The {@link AutoConfiguredAuthenticationConfiguration} class is a Spring {@link Configuration @Configuration} class
* that auto-configures Pivotal GemFire / Apache Geode Authentication by providing a implementation
@@ -78,7 +76,9 @@ public class AutoConfiguredAuthenticationConfiguration {
protected static final String DEFAULT_PASSWORD = DEFAULT_USERNAME;
protected static final String HTTP_PROTOCOL = "HTTP";
protected static final String SECURITY_CLIENT_AUTH_INIT = "security-client-auth-init";
protected static final String SECURITY_PASSWORD = "security-password";
protected static final String SECURITY_PEER_AUTH_INIT = "security-peer-auth-init";
protected static final String SECURITY_USERNAME = "security-username";
private Logger logger = LoggerFactory.getLogger(getClass());
@@ -113,28 +113,23 @@ public class AutoConfiguredAuthenticationConfiguration {
logger.debug("HTTP Request URI [{}]", request.getURI());
Optional.ofNullable(request.getHeaders())
.ifPresent(httpHeaders -> {
HttpHeaders httpHeaders = request.getHeaders();
CollectionUtils.nullSafeSet(httpHeaders.keySet()).forEach(httpHeaderName -> {
logger.debug("HTTP Request Header Name [{}] Value [{}]",
httpHeaderName, httpHeaders.get(httpHeaderName));
});
});
CollectionUtils.nullSafeSet(httpHeaders.keySet()).forEach(httpHeaderName ->
logger.debug("HTTP Request Header Name [{}] Value [{}]",
httpHeaderName, httpHeaders.get(httpHeaderName)));
ClientHttpResponse response = execution.execute(request, body);
Optional.ofNullable(response)
.ifPresent(it -> {
try {
logger.debug("HTTP Response Status Code [{}] Message [{}]",
it.getRawStatusCode(), it.getStatusText());
}
catch (IOException cause) {
logger.debug("Error occurred getting HTTP Response Status Code and Message", cause);
}
});
if (this.logger.isDebugEnabled()) {
try {
this.logger.debug("HTTP Response Status Code [{}] Message [{}]",
response.getRawStatusCode(), response.getStatusText());
}
catch (IOException cause) {
this.logger.debug("Error occurred getting HTTP Response Status Code and Message", cause);
}
}
return response;
};
@@ -163,8 +158,8 @@ public class AutoConfiguredAuthenticationConfiguration {
HttpHeaders requestHeaders = request.getHeaders();
requestHeaders.add(ResourceConstants.USER_NAME, username);
requestHeaders.add(ResourceConstants.PASSWORD, String.valueOf(password));
requestHeaders.add(SECURITY_USERNAME, username);
requestHeaders.add(SECURITY_PASSWORD, String.valueOf(password));
}
return execution.execute(request, body);
@@ -211,9 +206,19 @@ public class AutoConfiguredAuthenticationConfiguration {
public static class AutoConfiguredAuthenticationCondition implements Condition {
public static final String SPRING_DATA_GEMFIRE_SECURITY_AUTH_ENABLED =
"spring.data.gemfire.security.auth.auto-configure.enabled";
private static boolean isEnabled(Environment environment) {
return environment.getProperty(SPRING_DATA_GEMFIRE_SECURITY_AUTH_ENABLED, Boolean.class, true);
}
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
return isMatch(conditionContext.getEnvironment());
Environment environment = conditionContext.getEnvironment();
return isEnabled(environment) && isMatch(environment);
}
}
}