mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-06-27 06:12:27 +00:00
Added default constructor for easier use
This commit is contained in:
parent
80c1ae3bde
commit
488abe58fb
@ -106,11 +106,33 @@ public class DefaultInitialDirContextFactory implements InitialDirContextFactory
|
|||||||
|
|
||||||
//~ Constructors ===================================================================================================
|
//~ Constructors ===================================================================================================
|
||||||
|
|
||||||
public DefaultInitialDirContextFactory(String providerUrl) {
|
/**
|
||||||
this.providerUrl = providerUrl;
|
* Create an uninitialized object. You must call {@link #setProviderUrl(String)} after instantiation.
|
||||||
|
*/
|
||||||
|
public DefaultInitialDirContextFactory() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create and initialize an instance to the LDAP url provided
|
||||||
|
*
|
||||||
|
* @param providerUrl a String of the form <code>ldap://localhost:389/base_dn<code>
|
||||||
|
*/
|
||||||
|
public DefaultInitialDirContextFactory(String providerUrl) {
|
||||||
|
this.setProviderUrl(providerUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
//~ Methods ========================================================================================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the LDAP url
|
||||||
|
*
|
||||||
|
* @param providerUrl a String of the form <code>ldap://localhost:389/base_dn<code>
|
||||||
|
*/
|
||||||
|
public void setProviderUrl(String providerUrl) {
|
||||||
Assert.hasLength(providerUrl, "An LDAP connection URL must be supplied.");
|
Assert.hasLength(providerUrl, "An LDAP connection URL must be supplied.");
|
||||||
|
|
||||||
|
this.providerUrl = providerUrl;
|
||||||
|
|
||||||
StringTokenizer st = new StringTokenizer(providerUrl);
|
StringTokenizer st = new StringTokenizer(providerUrl);
|
||||||
|
|
||||||
// Work out rootDn from the first URL and check that the other URLs (if any) match
|
// Work out rootDn from the first URL and check that the other URLs (if any) match
|
||||||
@ -131,7 +153,14 @@ public class DefaultInitialDirContextFactory implements InitialDirContextFactory
|
|||||||
//Assert.isTrue(uri.getScheme().equals("ldap"), "Ldap URL must start with 'ldap://'");
|
//Assert.isTrue(uri.getScheme().equals("ldap"), "Ldap URL must start with 'ldap://'");
|
||||||
}
|
}
|
||||||
|
|
||||||
//~ Methods ========================================================================================================
|
/**
|
||||||
|
* Get the LDAP url
|
||||||
|
*
|
||||||
|
* @return the url
|
||||||
|
*/
|
||||||
|
public String getProviderUrl() {
|
||||||
|
return providerUrl;
|
||||||
|
}
|
||||||
|
|
||||||
private InitialDirContext connect(Hashtable env) {
|
private InitialDirContext connect(Hashtable env) {
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
@ -169,7 +198,7 @@ public class DefaultInitialDirContextFactory implements InitialDirContextFactory
|
|||||||
|
|
||||||
env.put(Context.SECURITY_AUTHENTICATION, authenticationType);
|
env.put(Context.SECURITY_AUTHENTICATION, authenticationType);
|
||||||
env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
|
env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
|
||||||
env.put(Context.PROVIDER_URL, providerUrl);
|
env.put(Context.PROVIDER_URL, getProviderUrl());
|
||||||
|
|
||||||
if (useConnectionPool) {
|
if (useConnectionPool) {
|
||||||
env.put(CONNECTION_POOL_KEY, "true");
|
env.put(CONNECTION_POOL_KEY, "true");
|
||||||
|
@ -123,16 +123,44 @@ public class LdapAuthenticationProvider extends AbstractUserDetailsAuthenticatio
|
|||||||
|
|
||||||
//~ Constructors ===================================================================================================
|
//~ Constructors ===================================================================================================
|
||||||
|
|
||||||
public LdapAuthenticationProvider(LdapAuthenticator authenticator, LdapAuthoritiesPopulator authoritiesPopulator) {
|
/**
|
||||||
Assert.notNull(authenticator, "An LdapAuthenticator must be supplied");
|
* Create an uninitialized instance. You must call {@link #setAuthenticator(LdapAuthenticator)} and
|
||||||
Assert.notNull(authoritiesPopulator, "An LdapAuthoritiesPopulator must be supplied");
|
* {@link #setAuthoritiesPopulator(LdapAuthoritiesPopulator)} before using.
|
||||||
|
*/
|
||||||
|
public LdapAuthenticationProvider() {
|
||||||
|
}
|
||||||
|
|
||||||
this.authenticator = authenticator;
|
/**
|
||||||
this.authoritiesPopulator = authoritiesPopulator;
|
* Create an initialized instance to the values passed as arguments
|
||||||
|
*
|
||||||
|
* @param authenticator
|
||||||
|
* @param authoritiesPopulator
|
||||||
|
*/
|
||||||
|
public LdapAuthenticationProvider(LdapAuthenticator authenticator, LdapAuthoritiesPopulator authoritiesPopulator) {
|
||||||
|
this.setAuthenticator(authenticator);
|
||||||
|
this.setAuthoritiesPopulator(authoritiesPopulator);
|
||||||
}
|
}
|
||||||
|
|
||||||
//~ Methods ========================================================================================================
|
//~ Methods ========================================================================================================
|
||||||
|
|
||||||
|
public void setAuthenticator(LdapAuthenticator authenticator) {
|
||||||
|
Assert.notNull(authenticator, "An LdapAuthenticator must be supplied");
|
||||||
|
this.authenticator = authenticator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LdapAuthenticator getAuthenticator() {
|
||||||
|
return authenticator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAuthoritiesPopulator(LdapAuthoritiesPopulator authoritiesPopulator) {
|
||||||
|
Assert.notNull(authoritiesPopulator, "An LdapAuthoritiesPopulator must be supplied");
|
||||||
|
this.authoritiesPopulator = authoritiesPopulator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LdapAuthoritiesPopulator getAuthoritiesPopulator() {
|
||||||
|
return authoritiesPopulator;
|
||||||
|
}
|
||||||
|
|
||||||
protected void additionalAuthenticationChecks(UserDetails userDetails,
|
protected void additionalAuthenticationChecks(UserDetails userDetails,
|
||||||
UsernamePasswordAuthenticationToken authentication)
|
UsernamePasswordAuthenticationToken authentication)
|
||||||
throws AuthenticationException {
|
throws AuthenticationException {
|
||||||
@ -161,7 +189,7 @@ public class LdapAuthenticationProvider extends AbstractUserDetailsAuthenticatio
|
|||||||
user.setUsername(username);
|
user.setUsername(username);
|
||||||
user.setPassword(password);
|
user.setPassword(password);
|
||||||
|
|
||||||
GrantedAuthority[] extraAuthorities = authoritiesPopulator.getGrantedAuthorities(ldapUser);
|
GrantedAuthority[] extraAuthorities = getAuthoritiesPopulator().getGrantedAuthorities(ldapUser);
|
||||||
|
|
||||||
for (int i = 0; i < extraAuthorities.length; i++) {
|
for (int i = 0; i < extraAuthorities.length; i++) {
|
||||||
user.addAuthority(extraAuthorities[i]);
|
user.addAuthority(extraAuthorities[i]);
|
||||||
@ -171,7 +199,7 @@ public class LdapAuthenticationProvider extends AbstractUserDetailsAuthenticatio
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected LdapAuthoritiesPopulator getAuthoritiesPoulator() {
|
protected LdapAuthoritiesPopulator getAuthoritiesPoulator() {
|
||||||
return authoritiesPopulator;
|
return getAuthoritiesPopulator();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication)
|
protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication)
|
||||||
@ -195,7 +223,7 @@ public class LdapAuthenticationProvider extends AbstractUserDetailsAuthenticatio
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
LdapUserDetails ldapUser = authenticator.authenticate(username, password);
|
LdapUserDetails ldapUser = getAuthenticator().authenticate(username, password);
|
||||||
|
|
||||||
return createUserDetails(ldapUser, username, password);
|
return createUserDetails(ldapUser, username, password);
|
||||||
|
|
||||||
|
@ -70,7 +70,36 @@ public abstract class AbstractLdapAuthenticator implements LdapAuthenticator, In
|
|||||||
|
|
||||||
//~ Constructors ===================================================================================================
|
//~ Constructors ===================================================================================================
|
||||||
|
|
||||||
protected AbstractLdapAuthenticator(InitialDirContextFactory initialDirContextFactory) {
|
/**
|
||||||
|
* Create an uninitialized instance. You must call {@link #setInitialDirContextFactory(InitialDirContextFactory)}
|
||||||
|
* before using it.
|
||||||
|
*/
|
||||||
|
public AbstractLdapAuthenticator() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an initialized instance to the {@link InitialDirContextFactory} provided.
|
||||||
|
*
|
||||||
|
* @param initialDirContextFactory
|
||||||
|
*/
|
||||||
|
public AbstractLdapAuthenticator(InitialDirContextFactory initialDirContextFactory) {
|
||||||
|
this.setInitialDirContextFactory(initialDirContextFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~ Methods
|
||||||
|
// ========================================================================================================
|
||||||
|
|
||||||
|
public void afterPropertiesSet() throws Exception {
|
||||||
|
Assert.isTrue((userDnFormat != null) || (userSearch != null),
|
||||||
|
"Either an LdapUserSearch or DN pattern (or both) must be supplied.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the {@link InitialDirContextFactory} and initialize this instance from its data.
|
||||||
|
*
|
||||||
|
* @param initialDirContextFactory
|
||||||
|
*/
|
||||||
|
public void setInitialDirContextFactory(InitialDirContextFactory initialDirContextFactory) {
|
||||||
Assert.notNull(initialDirContextFactory, "initialDirContextFactory must not be null.");
|
Assert.notNull(initialDirContextFactory, "initialDirContextFactory must not be null.");
|
||||||
this.initialDirContextFactory = initialDirContextFactory;
|
this.initialDirContextFactory = initialDirContextFactory;
|
||||||
|
|
||||||
@ -81,14 +110,7 @@ public abstract class AbstractLdapAuthenticator implements LdapAuthenticator, In
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//~ Methods ========================================================================================================
|
public InitialDirContextFactory getInitialDirContextFactory() {
|
||||||
|
|
||||||
public void afterPropertiesSet() throws Exception {
|
|
||||||
Assert.isTrue((userDnFormat != null) || (userSearch != null),
|
|
||||||
"Either an LdapUserSearch or DN pattern (or both) must be supplied.");
|
|
||||||
}
|
|
||||||
|
|
||||||
protected InitialDirContextFactory getInitialDirContextFactory() {
|
|
||||||
return initialDirContextFactory;
|
return initialDirContextFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,6 +44,19 @@ public class BindAuthenticator extends AbstractLdapAuthenticator {
|
|||||||
|
|
||||||
//~ Constructors ===================================================================================================
|
//~ Constructors ===================================================================================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an uninitialized instance. You must call {@link #setInitialDirContextFactory(InitialDirContextFactory)}
|
||||||
|
* before using it.
|
||||||
|
*/
|
||||||
|
public BindAuthenticator() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an initialized instance to the {@link InitialDirContextFactory} provided.
|
||||||
|
*
|
||||||
|
* @param initialDirContextFactory
|
||||||
|
*/
|
||||||
public BindAuthenticator(InitialDirContextFactory initialDirContextFactory) {
|
public BindAuthenticator(InitialDirContextFactory initialDirContextFactory) {
|
||||||
super(initialDirContextFactory);
|
super(initialDirContextFactory);
|
||||||
}
|
}
|
||||||
|
@ -112,6 +112,13 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|||||||
|
|
||||||
//~ Constructors ===================================================================================================
|
//~ Constructors ===================================================================================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an uninitialized instance. You must call {@link #setInitialDirContextFactory(InitialDirContextFactory)}
|
||||||
|
* and {@link #setGroupSearchBase(String)} before using it.
|
||||||
|
*/
|
||||||
|
public DefaultLdapAuthoritiesPopulator() {
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for group search scenarios. <tt>userRoleAttributes</tt> may still be
|
* Constructor for group search scenarios. <tt>userRoleAttributes</tt> may still be
|
||||||
* set as a property.
|
* set as a property.
|
||||||
@ -121,18 +128,8 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|||||||
* context factory.
|
* context factory.
|
||||||
*/
|
*/
|
||||||
public DefaultLdapAuthoritiesPopulator(InitialDirContextFactory initialDirContextFactory, String groupSearchBase) {
|
public DefaultLdapAuthoritiesPopulator(InitialDirContextFactory initialDirContextFactory, String groupSearchBase) {
|
||||||
Assert.notNull(initialDirContextFactory, "InitialDirContextFactory must not be null");
|
this.setInitialDirContextFactory(initialDirContextFactory);
|
||||||
Assert.notNull(groupSearchBase, "The groupSearchBase (name to search under), must not be null.");
|
this.setGroupSearchBase(groupSearchBase);
|
||||||
this.initialDirContextFactory = initialDirContextFactory;
|
|
||||||
this.groupSearchBase = groupSearchBase;
|
|
||||||
|
|
||||||
if (groupSearchBase.length() == 0) {
|
|
||||||
logger.info("groupSearchBase is empty. Searches will be performed from the root: "
|
|
||||||
+ initialDirContextFactory.getRootDn());
|
|
||||||
}
|
|
||||||
|
|
||||||
ldapTemplate = new LdapTemplate(initialDirContextFactory);
|
|
||||||
ldapTemplate.setSearchControls(searchControls);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//~ Methods ========================================================================================================
|
//~ Methods ========================================================================================================
|
||||||
@ -204,16 +201,16 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|||||||
public Set getGroupMembershipRoles(String userDn, String username) {
|
public Set getGroupMembershipRoles(String userDn, String username) {
|
||||||
Set authorities = new HashSet();
|
Set authorities = new HashSet();
|
||||||
|
|
||||||
if (groupSearchBase == null) {
|
if (getGroupSearchBase() == null) {
|
||||||
return authorities;
|
return authorities;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug("Searching for roles for user '" + username + "', DN = " + "'" + userDn + "', with filter "
|
logger.debug("Searching for roles for user '" + username + "', DN = " + "'" + userDn + "', with filter "
|
||||||
+ groupSearchFilter + " in search base '" + groupSearchBase + "'");
|
+ groupSearchFilter + " in search base '" + getGroupSearchBase() + "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
Set userRoles = ldapTemplate.searchForSingleAttributeValues(groupSearchBase, groupSearchFilter,
|
Set userRoles = ldapTemplate.searchForSingleAttributeValues(getGroupSearchBase(), groupSearchFilter,
|
||||||
new String[] {userDn, username}, groupRoleAttribute);
|
new String[] {userDn, username}, groupRoleAttribute);
|
||||||
|
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
@ -254,6 +251,38 @@ public class DefaultLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator
|
|||||||
return initialDirContextFactory;
|
return initialDirContextFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the {@link InitialDirContextFactory}
|
||||||
|
*
|
||||||
|
* @param initialDirContextFactory supplies the contexts used to search for user roles.
|
||||||
|
*/
|
||||||
|
public void setInitialDirContextFactory(InitialDirContextFactory initialDirContextFactory) {
|
||||||
|
Assert.notNull(initialDirContextFactory, "InitialDirContextFactory must not be null");
|
||||||
|
this.initialDirContextFactory = initialDirContextFactory;
|
||||||
|
|
||||||
|
ldapTemplate = new LdapTemplate(initialDirContextFactory);
|
||||||
|
ldapTemplate.setSearchControls(searchControls);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the group search base (name to search under)
|
||||||
|
*
|
||||||
|
* @param groupSearchBase if this is an empty string the search will be performed from the root DN of the context
|
||||||
|
* factory.
|
||||||
|
*/
|
||||||
|
public void setGroupSearchBase(String groupSearchBase) {
|
||||||
|
Assert.notNull(groupSearchBase, "The groupSearchBase (name to search under), must not be null.");
|
||||||
|
this.groupSearchBase = groupSearchBase;
|
||||||
|
if (groupSearchBase.length() == 0) {
|
||||||
|
logger.info("groupSearchBase is empty. Searches will be performed from the root: "
|
||||||
|
+ getInitialDirContextFactory().getRootDn());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String getGroupSearchBase() {
|
||||||
|
return groupSearchBase;
|
||||||
|
}
|
||||||
|
|
||||||
public void setConvertToUpperCase(boolean convertToUpperCase) {
|
public void setConvertToUpperCase(boolean convertToUpperCase) {
|
||||||
this.convertToUpperCase = convertToUpperCase;
|
this.convertToUpperCase = convertToUpperCase;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user