From 0bd11cd4e4742370557a157498dd2ca0b6882c9b Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Tue, 11 Feb 2014 15:11:51 -0600 Subject: [PATCH] LDAP-284: AbstractContextSource userDn and password are protected This is necessary so Spring Security can support both Spring LDAP 1.x & 2.x --- .../core/support/AbstractContextSource.java | 34 +++++++++++-- .../core/support/ldap294/Ldap294Tests.java | 51 +++++++++++++++++++ 2 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 core/src/test/java/org/springframework/ldap/core/support/ldap294/Ldap294Tests.java diff --git a/core/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java b/core/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java index a4d4f1b9..c97712ed 100644 --- a/core/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java +++ b/core/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java @@ -37,6 +37,7 @@ import javax.naming.directory.Attributes; import javax.naming.directory.DirContext; import javax.naming.ldap.LdapName; import javax.naming.ldap.Rdn; + import java.net.URI; import java.net.URISyntaxException; import java.util.Hashtable; @@ -88,9 +89,17 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource private LdapName base = LdapUtils.emptyLdapName(); - private String userDn = ""; + /** + * @deprecated use {@link #getUserDn()} and {@link #setUserDn(String)} instead + */ + @Deprecated + protected String userDn = ""; - private String password = ""; + /** + * @deprecated use {@link #getPassword()} and {@link #setPassword(String)} instead + */ + @Deprecated + protected String password = ""; private String[] urls; @@ -122,7 +131,7 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource return doGetContext(principal, credentials, EXPLICITLY_DISABLE_POOLING); } - private DirContext doGetContext(String principal, String credentials, boolean explicitlyDisablePooling) { + private DirContext doGetContext(String principal, String credentials, boolean explicitlyDisablePooling) { Hashtable env = getAuthenticatedEnv(principal, credentials); if(explicitlyDisablePooling) { env.remove(SUN_LDAP_POOLING_FLAG); @@ -421,6 +430,7 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource } } + @SuppressWarnings("deprecation") private Hashtable setupAnonymousEnv() { if (pooled) { baseEnv.put(SUN_LDAP_POOLING_FLAG, "true"); @@ -463,6 +473,14 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource this.password = password; } + /** + * Gets the password (credentials) to use for getting authenticated contexts. + * @return the password + */ + public String getPassword() { + return password; + } + /** * Set the user distinguished name (principal) to use for getting * authenticated contexts. @@ -473,6 +491,16 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource this.userDn = userDn; } + /** + * Gets the user distinguished name (principal) to use for getting + * authenticated contexts. + * + * @return the user distinguished name. + */ + protected String getUserDn() { + return userDn; + } + /** * Set the urls of the LDAP servers. Use this method if several servers are * required. diff --git a/core/src/test/java/org/springframework/ldap/core/support/ldap294/Ldap294Tests.java b/core/src/test/java/org/springframework/ldap/core/support/ldap294/Ldap294Tests.java new file mode 100644 index 00000000..006ee6a6 --- /dev/null +++ b/core/src/test/java/org/springframework/ldap/core/support/ldap294/Ldap294Tests.java @@ -0,0 +1,51 @@ +/* + * Copyright 2002-2013 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.ldap.core.support.ldap294; + +import java.util.Hashtable; + +import javax.naming.NamingException; +import javax.naming.directory.DirContext; + +import org.junit.Test; +import org.springframework.ldap.core.support.AbstractContextSource; + +/** + * These tests just ensure that the subclass compiles + * + * @author Rob Winch + * + */ +public class Ldap294Tests { + + @Test + public void concerteContextSourceCanAccessPasswordAndUserDn() {} + + static class ConcerteContextSource extends AbstractContextSource { + + @Override + protected DirContext getDirContextInstance( + Hashtable environment) throws NamingException { + // Verify a subclass outside of package scope can access password + // and userDn since Spring Security needs to be able to access these + // properties. + String pass = super.password; + String userDn = super.userDn; + return null; + } + + } +}