|
|
|
@ -1,39 +1,30 @@ |
|
|
|
package org.springframework.security.ldap; |
|
|
|
package org.springframework.security.ldap; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.security.BadCredentialsException; |
|
|
|
import java.util.ArrayList; |
|
|
|
import org.springframework.security.SpringSecurityMessageSource; |
|
|
|
import java.util.StringTokenizer; |
|
|
|
import org.springframework.context.MessageSource; |
|
|
|
|
|
|
|
import org.springframework.context.MessageSourceAware; |
|
|
|
|
|
|
|
import org.springframework.context.support.MessageSourceAccessor; |
|
|
|
|
|
|
|
import org.springframework.ldap.core.support.LdapContextSource; |
|
|
|
|
|
|
|
import org.springframework.util.Assert; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.ldap.core.support.LdapContextSource; |
|
|
|
import javax.naming.Context; |
|
|
|
import org.springframework.util.Assert; |
|
|
|
import javax.naming.directory.DirContext; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import java.util.Hashtable; |
|
|
|
|
|
|
|
import java.util.StringTokenizer; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* SpringSecurityContextSource implementation which uses Spring LDAP's <tt>LdapContextSource</tt> as a base |
|
|
|
* ContextSource implementation which uses Spring LDAP's <tt>LdapContextSource</tt> as a base |
|
|
|
* class. Intended as a replacement for <tt>DefaultInitialDirContextFactory</tt> from versions of the framework prior |
|
|
|
* class. Used internally by the Spring Security LDAP namespace configuration. |
|
|
|
* to 2.0. |
|
|
|
* <p> |
|
|
|
|
|
|
|
* From Spring Security 2.5, Spring LDAP 1.3 is used and the <tt>ContextSource</tt> interface
|
|
|
|
|
|
|
|
* provides support for binding with a username and password. As a result, Spring LDAP <tt>ContextSource</tt> |
|
|
|
|
|
|
|
* implementations such as <tt>LdapContextSource</tt> may be used directly with Spring Security. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Luke Taylor |
|
|
|
* @author Luke Taylor |
|
|
|
* @version $Id$ |
|
|
|
* @version $Id$ |
|
|
|
* @since 2.0 |
|
|
|
* @since 2.0 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class DefaultSpringSecurityContextSource extends LdapContextSource implements SpringSecurityContextSource, |
|
|
|
public class DefaultSpringSecurityContextSource extends LdapContextSource { |
|
|
|
MessageSourceAware { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final Log logger = LogFactory.getLog(DefaultSpringSecurityContextSource.class); |
|
|
|
private static final Log logger = LogFactory.getLog(DefaultSpringSecurityContextSource.class); |
|
|
|
private String rootDn; |
|
|
|
private String rootDn; |
|
|
|
|
|
|
|
|
|
|
|
protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Create and initialize an instance which will connect to the supplied LDAP URL. |
|
|
|
* Create and initialize an instance which will connect to the supplied LDAP URL. |
|
|
|
* |
|
|
|
* |
|
|
|
@ -65,31 +56,4 @@ public class DefaultSpringSecurityContextSource extends LdapContextSource implem |
|
|
|
super.setUrls(urls.toArray(new String[urls.size()])); |
|
|
|
super.setUrls(urls.toArray(new String[urls.size()])); |
|
|
|
super.setBase(rootDn); |
|
|
|
super.setBase(rootDn); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
|
|
|
public DirContext getReadWriteContext(String userDn, Object credentials) { |
|
|
|
|
|
|
|
Hashtable env = new Hashtable(getAnonymousEnv()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
env.put(Context.SECURITY_PRINCIPAL, userDn); |
|
|
|
|
|
|
|
env.put(Context.SECURITY_CREDENTIALS, credentials); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (logger.isDebugEnabled()) { |
|
|
|
|
|
|
|
logger.debug("Creating context with principal: '" + userDn + "'"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
return createContext(env); |
|
|
|
|
|
|
|
} catch (org.springframework.ldap.NamingException e) { |
|
|
|
|
|
|
|
if ((e instanceof org.springframework.ldap.AuthenticationException) |
|
|
|
|
|
|
|
|| (e instanceof org.springframework.ldap.OperationNotSupportedException)) { |
|
|
|
|
|
|
|
throw new BadCredentialsException( |
|
|
|
|
|
|
|
messages.getMessage("DefaultSpringSecurityContextSource.badCredentials", "Bad credentials"), e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
throw e; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setMessageSource(MessageSource messageSource) { |
|
|
|
|
|
|
|
this.messages = new MessageSourceAccessor(messageSource); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|