|
@@ -1,5 +1,5 @@
|
|
/*
|
|
/*
|
|
- * Copyright 2002-2016 the original author or authors.
|
|
|
|
|
|
+ * Copyright 2002-2019 the original author or authors.
|
|
*
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* you may not use this file except in compliance with the License.
|
|
@@ -20,6 +20,7 @@ import java.net.ServerSocket;
|
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
import org.apache.commons.logging.Log;
|
|
import org.apache.commons.logging.LogFactory;
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
+
|
|
import org.springframework.beans.factory.config.BeanDefinition;
|
|
import org.springframework.beans.factory.config.BeanDefinition;
|
|
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
|
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
|
@@ -28,11 +29,14 @@ import org.springframework.beans.factory.xml.BeanDefinitionParser;
|
|
import org.springframework.beans.factory.xml.ParserContext;
|
|
import org.springframework.beans.factory.xml.ParserContext;
|
|
import org.springframework.security.config.BeanIds;
|
|
import org.springframework.security.config.BeanIds;
|
|
import org.springframework.security.ldap.server.ApacheDSContainer;
|
|
import org.springframework.security.ldap.server.ApacheDSContainer;
|
|
|
|
+import org.springframework.security.ldap.server.UnboundIdContainer;
|
|
|
|
+import org.springframework.util.ClassUtils;
|
|
import org.springframework.util.StringUtils;
|
|
import org.springframework.util.StringUtils;
|
|
import org.w3c.dom.Element;
|
|
import org.w3c.dom.Element;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @author Luke Taylor
|
|
* @author Luke Taylor
|
|
|
|
+ * @author Eddú Meléndez
|
|
*/
|
|
*/
|
|
public class LdapServerBeanDefinitionParser implements BeanDefinitionParser {
|
|
public class LdapServerBeanDefinitionParser implements BeanDefinitionParser {
|
|
private static final String CONTEXT_SOURCE_CLASS = "org.springframework.security.ldap.DefaultSpringSecurityContextSource";
|
|
private static final String CONTEXT_SOURCE_CLASS = "org.springframework.security.ldap.DefaultSpringSecurityContextSource";
|
|
@@ -65,6 +69,12 @@ public class LdapServerBeanDefinitionParser implements BeanDefinitionParser {
|
|
private static final int DEFAULT_PORT = 33389;
|
|
private static final int DEFAULT_PORT = 33389;
|
|
public static final String OPT_DEFAULT_PORT = String.valueOf(DEFAULT_PORT);
|
|
public static final String OPT_DEFAULT_PORT = String.valueOf(DEFAULT_PORT);
|
|
|
|
|
|
|
|
+ private static final String APACHEDS_CLASSNAME = "org.apache.directory.server.core.DefaultDirectoryService";
|
|
|
|
+ private static final String UNBOUNID_CLASSNAME = "com.unboundid.ldap.listener.InMemoryDirectoryServer";
|
|
|
|
+
|
|
|
|
+ private static final String APACHEDS_CONTAINER_CLASSNAME = "org.springframework.security.ldap.server.ApacheDSContainer";
|
|
|
|
+ private static final String UNBOUNDID_CONTAINER_CLASSNAME = "org.springframework.security.ldap.server.UnboundIdContainer";
|
|
|
|
+
|
|
public BeanDefinition parse(Element elt, ParserContext parserContext) {
|
|
public BeanDefinition parse(Element elt, ParserContext parserContext) {
|
|
String url = elt.getAttribute(ATT_URL);
|
|
String url = elt.getAttribute(ATT_URL);
|
|
|
|
|
|
@@ -114,6 +124,7 @@ public class LdapServerBeanDefinitionParser implements BeanDefinitionParser {
|
|
* @return the BeanDefinition for the ContextSource for the embedded server.
|
|
* @return the BeanDefinition for the ContextSource for the embedded server.
|
|
*
|
|
*
|
|
* @see ApacheDSContainer
|
|
* @see ApacheDSContainer
|
|
|
|
+ * @see UnboundIdContainer
|
|
*/
|
|
*/
|
|
private RootBeanDefinition createEmbeddedServer(Element element,
|
|
private RootBeanDefinition createEmbeddedServer(Element element,
|
|
ParserContext parserContext) {
|
|
ParserContext parserContext) {
|
|
@@ -142,34 +153,65 @@ public class LdapServerBeanDefinitionParser implements BeanDefinitionParser {
|
|
contextSource.addPropertyValue("userDn", "uid=admin,ou=system");
|
|
contextSource.addPropertyValue("userDn", "uid=admin,ou=system");
|
|
contextSource.addPropertyValue("password", "secret");
|
|
contextSource.addPropertyValue("password", "secret");
|
|
|
|
|
|
- RootBeanDefinition apacheContainer = new RootBeanDefinition(
|
|
|
|
- "org.springframework.security.ldap.server.ApacheDSContainer", null, null);
|
|
|
|
- apacheContainer.setSource(source);
|
|
|
|
- apacheContainer.getConstructorArgumentValues().addGenericArgumentValue(suffix);
|
|
|
|
|
|
+ String mode = element.getAttribute("mode");
|
|
|
|
+ RootBeanDefinition ldapContainer = getRootBeanDefinition(mode);
|
|
|
|
+ ldapContainer.setSource(source);
|
|
|
|
+ ldapContainer.getConstructorArgumentValues().addGenericArgumentValue(suffix);
|
|
|
|
|
|
String ldifs = element.getAttribute(ATT_LDIF_FILE);
|
|
String ldifs = element.getAttribute(ATT_LDIF_FILE);
|
|
if (!StringUtils.hasText(ldifs)) {
|
|
if (!StringUtils.hasText(ldifs)) {
|
|
ldifs = OPT_DEFAULT_LDIF_FILE;
|
|
ldifs = OPT_DEFAULT_LDIF_FILE;
|
|
}
|
|
}
|
|
|
|
|
|
- apacheContainer.getConstructorArgumentValues().addGenericArgumentValue(ldifs);
|
|
|
|
- apacheContainer.getPropertyValues().addPropertyValue("port", port);
|
|
|
|
|
|
+ ldapContainer.getConstructorArgumentValues().addGenericArgumentValue(ldifs);
|
|
|
|
+ ldapContainer.getPropertyValues().addPropertyValue("port", port);
|
|
|
|
|
|
logger.info("Embedded LDAP server bean definition created for URL: " + url);
|
|
logger.info("Embedded LDAP server bean definition created for URL: " + url);
|
|
|
|
|
|
if (parserContext.getRegistry()
|
|
if (parserContext.getRegistry()
|
|
- .containsBeanDefinition(BeanIds.EMBEDDED_APACHE_DS)) {
|
|
|
|
|
|
+ .containsBeanDefinition(BeanIds.EMBEDDED_APACHE_DS) ||
|
|
|
|
+ parserContext.getRegistry().containsBeanDefinition(BeanIds.EMBEDDED_UNBOUNDID)) {
|
|
parserContext.getReaderContext().error(
|
|
parserContext.getReaderContext().error(
|
|
"Only one embedded server bean is allowed per application context",
|
|
"Only one embedded server bean is allowed per application context",
|
|
element);
|
|
element);
|
|
}
|
|
}
|
|
|
|
|
|
- parserContext.getRegistry().registerBeanDefinition(BeanIds.EMBEDDED_APACHE_DS,
|
|
|
|
- apacheContainer);
|
|
|
|
|
|
+ String beanId = resolveBeanId(mode);
|
|
|
|
+ if (beanId != null) {
|
|
|
|
+ parserContext.getRegistry().registerBeanDefinition(beanId, ldapContainer);
|
|
|
|
+ }
|
|
|
|
|
|
return (RootBeanDefinition) contextSource.getBeanDefinition();
|
|
return (RootBeanDefinition) contextSource.getBeanDefinition();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private RootBeanDefinition getRootBeanDefinition(String mode) {
|
|
|
|
+ if (isApacheDsEnabled(mode)) {
|
|
|
|
+ return new RootBeanDefinition(APACHEDS_CONTAINER_CLASSNAME, null, null);
|
|
|
|
+ }
|
|
|
|
+ else if (isUnboundidEnabled(mode)) {
|
|
|
|
+ return new RootBeanDefinition(UNBOUNDID_CONTAINER_CLASSNAME, null, null);
|
|
|
|
+ }
|
|
|
|
+ throw new IllegalStateException("Embedded LDAP server is not provided");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String resolveBeanId(String mode) {
|
|
|
|
+ if (isApacheDsEnabled(mode)) {
|
|
|
|
+ return BeanIds.EMBEDDED_APACHE_DS;
|
|
|
|
+ }
|
|
|
|
+ else if (isUnboundidEnabled(mode)) {
|
|
|
|
+ return BeanIds.EMBEDDED_UNBOUNDID;
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private boolean isApacheDsEnabled(String mode) {
|
|
|
|
+ return "apacheds".equals(mode) || ClassUtils.isPresent(APACHEDS_CLASSNAME, getClass().getClassLoader());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private boolean isUnboundidEnabled(String mode) {
|
|
|
|
+ return "unboundid".equals(mode) || ClassUtils.isPresent(UNBOUNID_CLASSNAME, getClass().getClassLoader());
|
|
|
|
+ }
|
|
|
|
+
|
|
private String getDefaultPort() {
|
|
private String getDefaultPort() {
|
|
ServerSocket serverSocket = null;
|
|
ServerSocket serverSocket = null;
|
|
try {
|
|
try {
|
|
@@ -196,4 +238,5 @@ public class LdapServerBeanDefinitionParser implements BeanDefinitionParser {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|