Added DirContextAuthenticationStrategy, a default implementation for simple authentication,
and modified AbstractContextSource to call the strategy at appropriate stages in authentication.
This commit is contained in:
@@ -13,6 +13,8 @@ http://www.ietf.org/rfc/rfc2696.txt
|
||||
|
||||
Changes in version 1.2.2 (XXX 2008)
|
||||
-------------------------------------------
|
||||
* Removed deprecated method setUserName() in AbstractContextSource.
|
||||
|
||||
* Added a method DistinguishedName.toCompactString that returns a more
|
||||
compact String representation without blanks. (LDAP-91)
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2005-2007 the original author or authors.
|
||||
* Copyright 2005-2008 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.
|
||||
@@ -97,6 +97,8 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource
|
||||
|
||||
private static final String JDK_142 = "1.4.2";
|
||||
|
||||
private DirContextAuthenticationStrategy authenticationStrategy = new SimpleDirContextAuthenticationStrategy();
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
@@ -122,15 +124,22 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource
|
||||
|
||||
/**
|
||||
* Default implementation of setting the environment up to be authenticated.
|
||||
* Override in subclass if necessary.
|
||||
* This method should typically NOT be overridden; any customization to the
|
||||
* authentication mechanism should be managed by setting a different
|
||||
* {@link DirContextAuthenticationStrategy} on this instance.
|
||||
*
|
||||
* @param env the environment to modify.
|
||||
* @see DirContextAuthenticationStrategy
|
||||
* @see #setAuthenticationStrategy(DirContextAuthenticationStrategy)
|
||||
*/
|
||||
protected void setupAuthenticatedEnvironment(Hashtable env) {
|
||||
String principal = authenticationSource.getPrincipal();
|
||||
env.put(Context.SECURITY_PRINCIPAL, principal);
|
||||
log.debug("Principal: '" + principal + "'");
|
||||
env.put(Context.SECURITY_CREDENTIALS, authenticationSource.getCredentials());
|
||||
try {
|
||||
authenticationStrategy.setupEnvironment(env, authenticationSource.getPrincipal(), authenticationSource
|
||||
.getCredentials());
|
||||
}
|
||||
catch (NamingException e) {
|
||||
throw LdapUtils.convertLdapException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -211,9 +220,9 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource
|
||||
/**
|
||||
* Create a DirContext using the supplied environment.
|
||||
*
|
||||
* @param environment the Ldap environment to use when creating the
|
||||
* @param environment the LDAP environment to use when creating the
|
||||
* <code>DirContext</code>.
|
||||
* @return a new DirContext implpementation initialized with the supplied
|
||||
* @return a new DirContext implementation initialized with the supplied
|
||||
* environment.
|
||||
*/
|
||||
protected DirContext createContext(Hashtable environment) {
|
||||
@@ -222,6 +231,9 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource
|
||||
try {
|
||||
ctx = getDirContextInstance(environment);
|
||||
|
||||
authenticationStrategy.processContextAfterCreation(ctx, authenticationSource.getPrincipal(),
|
||||
authenticationSource.getCredentials());
|
||||
|
||||
if (log.isInfoEnabled()) {
|
||||
Hashtable ctxEnv = ctx.getEnvironment();
|
||||
String ldapUrl = (String) ctxEnv.get(Context.PROVIDER_URL);
|
||||
@@ -357,17 +369,6 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource
|
||||
this.userDn = userDn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the user distinguished name (principal) to use for getting
|
||||
* authenticated contexts.
|
||||
*
|
||||
* @param userName the user distinguished name.
|
||||
* @deprecated Use {@link #setUserDn(String)} instead.
|
||||
*/
|
||||
public void setUserName(String userName) {
|
||||
setUserDn(userName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the urls of the LDAP servers. Use this method if several servers are
|
||||
* required.
|
||||
@@ -508,6 +509,19 @@ public abstract class AbstractContextSource implements BaseLdapPathContextSource
|
||||
return anonymousReadOnly;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link DirContextAuthenticationStrategy} to use for preparing the
|
||||
* environment and processing the created <code>DirContext</code>
|
||||
* instances.
|
||||
*
|
||||
* @param authenticationStrategy the
|
||||
* {@link DirContextAuthenticationStrategy} to use; default is
|
||||
* {@link SimpleDirContextAuthenticationStrategy}.
|
||||
*/
|
||||
public void setAuthenticationStrategy(DirContextAuthenticationStrategy authenticationStrategy) {
|
||||
this.authenticationStrategy = authenticationStrategy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implement in subclass to create a DirContext of the desired type (e.g.
|
||||
* InitialDirContext or InitialLdapContext).
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright 2005-2008 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;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.directory.DirContext;
|
||||
|
||||
import org.springframework.ldap.core.AuthenticationSource;
|
||||
import org.springframework.ldap.core.ContextSource;
|
||||
|
||||
/**
|
||||
* A strategy to use when authenticating LDAP connections on creation. When
|
||||
* authenticating LDAP connections different strategies are needed depending on
|
||||
* the authentication mechanism used. Furthermore, depending on the mechanism
|
||||
* the work to be done needs to be applied at different stages of the
|
||||
* <code>DirContext</code> creation process. A
|
||||
* DirContextAuthenticationStrategy contains the logic to perform a particular
|
||||
* type of authentication mechanism and will be called by its
|
||||
* {@link ContextSource} at appropriate stages of the process.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public interface DirContextAuthenticationStrategy {
|
||||
|
||||
/**
|
||||
* This method is responsible for preparing the environment to be used when
|
||||
* creating the <code>DirContext</code> instance. The base environment
|
||||
* (including URL, <code>ContextFactory</code> etc. will already be set,
|
||||
* and this method is called just before the actual Context is to be
|
||||
* created.
|
||||
*
|
||||
* @param env The <code>Hashtable</code> to be sent to the
|
||||
* <code>DirContext</code> instance on initialization. Pre-configured with
|
||||
* the basic settings; the implementation of this method is responsible for
|
||||
* manipulating the environment as appropriate for the particular
|
||||
* authentication mechanism.
|
||||
* @param userDn the user DN to authenticate, as received from the
|
||||
* {@link AuthenticationSource} of the {@link ContextSource}.
|
||||
* @param password the password to authenticate with, as received from the
|
||||
* {@link AuthenticationSource} of the {@link ContextSource}.
|
||||
* @throws NamingException if anything goes wrong. This will cause the
|
||||
* <code>DirContext</code> creation to be aborted and the exception to be
|
||||
* translated and rethrown.
|
||||
*/
|
||||
public void setupEnvironment(Hashtable env, String userDn, String password) throws NamingException;
|
||||
|
||||
/**
|
||||
* This method is responsible for post-processing the
|
||||
* <code>DirContext</code> instance after it has been created. It will be
|
||||
* called immediately after the instance has been created.
|
||||
*
|
||||
* @param ctx the freshly created <code>DirContext</code> instance. The
|
||||
* actual implementation class (e.g. <code>InitialLdapContext</code>)
|
||||
* depends on the {@link ContextSource} implementation.
|
||||
* @param userDn the user DN to authenticate, as received from the
|
||||
* {@link AuthenticationSource} of the {@link ContextSource}.
|
||||
* @param password the password to authenticate with, as received from the
|
||||
* {@link AuthenticationSource} of the {@link ContextSource}.
|
||||
* @throws NamingException if anything goes wrong. This will cause the
|
||||
* <code>DirContext</code> creation to be aborted and the exception to be
|
||||
* translated and rethrown.
|
||||
*/
|
||||
public void processContextAfterCreation(DirContext ctx, String userDn, String password) throws NamingException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2005-2008 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;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.directory.DirContext;
|
||||
|
||||
/**
|
||||
* The default {@link DirContextAuthenticationStrategy} implementation, setting
|
||||
* the <code>DirContext</code> environment up for 'SIMPLE' authentication, and
|
||||
* specifying the user DN and password as SECURITY_PRINCIPAL and
|
||||
* SECURITY_CREDENTIALS respectively in the authenticated environment before the
|
||||
* context is created.
|
||||
*
|
||||
* @author Mattias Hellborg Arthursson
|
||||
*/
|
||||
public class SimpleDirContextAuthenticationStrategy implements DirContextAuthenticationStrategy {
|
||||
|
||||
private static final String SIMPLE_AUTHENTICATION = "simple";
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.ldap.core.support.DirContextAuthenticationStrategy#setupEnvironment(java.util.Hashtable,
|
||||
* java.lang.String, java.lang.String)
|
||||
*/
|
||||
public void setupEnvironment(Hashtable env, String userDn, String password) {
|
||||
env.put(Context.SECURITY_AUTHENTICATION, SIMPLE_AUTHENTICATION);
|
||||
env.put(Context.SECURITY_PRINCIPAL, userDn);
|
||||
env.put(Context.SECURITY_CREDENTIALS, password);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.ldap.core.support.DirContextAuthenticationStrategy#processContextAfterCreation(javax.naming.directory.DirContext,
|
||||
* java.lang.String, java.lang.String)
|
||||
*/
|
||||
public void processContextAfterCreation(DirContext ctx, String userDn, String password) {
|
||||
// Nothing to do here
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2005-2008 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;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
import javax.naming.Context;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class SimpleDirContextAuthenticationStrategyTest extends TestCase {
|
||||
private SimpleDirContextAuthenticationStrategy tested;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
tested = new SimpleDirContextAuthenticationStrategy();
|
||||
}
|
||||
|
||||
public void testSetupEnvironment() {
|
||||
Hashtable env = new Hashtable();
|
||||
tested.setupEnvironment(env, "cn=John Doe", "pw");
|
||||
|
||||
assertEquals("simple", env.get(Context.SECURITY_AUTHENTICATION));
|
||||
assertEquals("cn=John Doe", env.get(Context.SECURITY_PRINCIPAL));
|
||||
assertEquals("pw", env.get(Context.SECURITY_CREDENTIALS));
|
||||
}
|
||||
|
||||
public void testProcessContextAfterCreation() {
|
||||
Hashtable env = new Hashtable();
|
||||
tested.processContextAfterCreation(null, "cn=John Doe", "pw");
|
||||
|
||||
assertTrue(env.isEmpty());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user