LDAP-284: AbstractContextSource userDn and password are protected

This is necessary so Spring Security can support both Spring LDAP 1.x & 2.x
This commit is contained in:
Rob Winch
2014-02-11 15:11:51 -06:00
parent 9e2fbbc273
commit 0bd11cd4e4
2 changed files with 82 additions and 3 deletions

View File

@@ -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<String, Object> 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<String, Object> 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.

View File

@@ -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<String, Object> 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;
}
}
}