Class AbstractContextSource
- All Implemented Interfaces:
org.springframework.beans.factory.InitializingBean,ContextSource,BaseLdapPathContextSource,BaseLdapPathSource
- Direct Known Subclasses:
DirContextSource,LdapContextSource
ContextSource interface. By default,
returns an authenticated
DirContext implementation for both read-only and
read-write operations. To have an anonymous environment created for read-only
operations, set the anonymousReadOnly property to
true.
Implementing classes need to implement
getDirContextInstance(Hashtable) to create a DirContext
instance of the desired type.
If an AuthenticationSource is set, this will be used for getting user
principal and password for each new connection, otherwise a default one will
be created using the specified userDn and password.
Note: When using implementations of this class outside of a Spring
Context it is necessary to call afterPropertiesSet() when all
properties are set, in order to finish up initialization.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected StringDeprecated.static final Stringprotected StringDeprecated.usegetUserDn()andsetUserDn(String)instead -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidChecks that all necessary data is set and that there is no compatibility issues, after which the instance is initialized.assembleProviderUrlString(String[] ldapUrls) Assemble a valid url String from all registered urls to add asPROVIDER_URLto the environment.protected DirContextcreateContext(Hashtable<String, Object> environment) Create a DirContext using the supplied environment.getAuthenticatedEnv(String principal, String credentials) Get the authentication source.Get the base LDAP path as aLdapName.Deprecated.DistinguishedNameand associated classes and methods are deprecated as of 2.0.Get the base LDAP path as a String.getContext(String principal, String credentials) Gets aDirContextinstance authenticated using the supplied principal and credentials.Class<?>Get the context factory.protected abstract DirContextgetDirContextInstance(Hashtable<String, Object> environment) Implement in subclass to create a DirContext of the desired type (e.g.Class<?>Get the DirObjectFactory to use.Gets the password (credentials) to use for getting authenticated contexts.Gets a read-onlyDirContext.Gets a read-writeDirContextinstance.String[]getUrls()Get the urls of the LDAP servers.Gets the user distinguished name (principal) to use for getting authenticated contexts.booleanGet whether an anonymous environment should be used for read-only operations.booleanisPooled()Get whether the pooling flag should be set.voidsetAnonymousReadOnly(boolean anonymousReadOnly) Set whether an anonymous environment should be used for read-only operations.voidsetAuthenticationSource(AuthenticationSource authenticationSource) Set the authentication source to use when retrieving user principal and credentials.voidsetAuthenticationStrategy(DirContextAuthenticationStrategy authenticationStrategy) Set theDirContextAuthenticationStrategyto use for preparing the environment and processing the createdDirContextinstances.voidSet the base suffix from which all operations should origin.voidsetBaseEnvironmentProperties(Map<String, Object> baseEnvironmentProperties) If any custom environment properties are needed, these can be set using this method.voidsetCacheEnvironmentProperties(boolean cacheEnvironmentProperties) Set whether environment properties should be cached between requsts for anonymous environment.voidsetContextFactory(Class<?> contextFactory) Set the context factory.voidsetDirObjectFactory(Class<?> dirObjectFactory) Set the DirObjectFactory to use.voidsetPassword(String password) Set the password (credentials) to use for getting authenticated contexts.voidsetPooled(boolean pooled) Set whether the pooling flag should be set, enabling the built-in LDAP connection pooling.voidsetReferral(String referral) Set the method to handle referrals.protected voidDefault implementation of setting the environment up to be authenticated.voidSet the url of the LDAP server.voidSet the urls of the LDAP servers.voidSet the user distinguished name (principal) to use for getting authenticated contexts.
-
Field Details
-
userDn
Deprecated.usegetUserDn()andsetUserDn(String)instead -
password
Deprecated.usegetPassword()andsetPassword(String)instead -
SUN_LDAP_POOLING_FLAG
- See Also:
-
-
Constructor Details
-
AbstractContextSource
public AbstractContextSource()
-
-
Method Details
-
getContext
Description copied from interface:ContextSourceGets aDirContextinstance authenticated using the supplied principal and credentials. Typically to be used for plain authentication purposes. Note that this method will never make use of native Java LDAP pooling, even though this instance is configured to do so. This is to force password changes in the target directory to take effect as soon as possible.- Specified by:
getContextin interfaceContextSource- Parameters:
principal- The principal (typically a distinguished name of a user in the LDAP tree) to use for authentication.credentials- The credentials to use for authentication.- Returns:
- an authenticated
DirContextinstance, nevernull.
-
getReadOnlyContext
Description copied from interface:ContextSourceGets a read-onlyDirContext. The returnedDirContextmust be possible to perform read-only operations on.- Specified by:
getReadOnlyContextin interfaceContextSource- Returns:
- A DirContext instance, never null.
-
getReadWriteContext
Description copied from interface:ContextSourceGets a read-writeDirContextinstance.- Specified by:
getReadWriteContextin interfaceContextSource- Returns:
- A
DirContextinstance, nevernull.
-
setupAuthenticatedEnvironment
protected void setupAuthenticatedEnvironment(Hashtable<String, Object> env, String principal, String credentials) Default implementation of setting the environment up to be authenticated. This method should typically NOT be overridden; any customization to the authentication mechanism should be managed by setting a differentDirContextAuthenticationStrategyon this instance.- Parameters:
env- the environment to modify.principal- the principal to authenticate with.credentials- the credentials to authenticate with.- See Also:
-
assembleProviderUrlString
Assemble a valid url String from all registered urls to add asPROVIDER_URLto the environment.- Parameters:
ldapUrls- all individual url Strings.- Returns:
- the full url String
-
setBase
Set the base suffix from which all operations should origin. If a base suffix is set, you will not have to (and, indeed, must not) specify the full distinguished names in any operations performed.- Parameters:
base- the base suffix.
-
getBaseLdapPath
Deprecated.DistinguishedNameand associated classes and methods are deprecated as of 2.0.Description copied from interface:BaseLdapPathSourceGet the base LDAP path as aDistinguishedName.- Specified by:
getBaseLdapPathin interfaceBaseLdapPathSource- Returns:
- the base LDAP path as a
DistinguishedName. The path will be empty if no base path is specified.
-
getBaseLdapName
Description copied from interface:BaseLdapPathSourceGet the base LDAP path as aLdapName.- Specified by:
getBaseLdapNamein interfaceBaseLdapPathSource- Returns:
- the base LDAP path as a
LdapName. The path will be empty if no base path is specified.
-
getBaseLdapPathAsString
Description copied from interface:BaseLdapPathSourceGet the base LDAP path as a String.- Specified by:
getBaseLdapPathAsStringin interfaceBaseLdapPathSource- Returns:
- the base LDAP path as a An empty String will be returned if no base path is specified.
-
createContext
Create a DirContext using the supplied environment.- Parameters:
environment- the LDAP environment to use when creating theDirContext.- Returns:
- a new DirContext implementation initialized with the supplied environment.
-
setContextFactory
Set the context factory. Default is com.sun.jndi.ldap.LdapCtxFactory.- Parameters:
contextFactory- the context factory used when creating Contexts.
-
getContextFactory
Get the context factory.- Returns:
- the context factory used when creating Contexts.
-
setDirObjectFactory
Set the DirObjectFactory to use. Default isDefaultDirObjectFactory. The specified class needs to be an implementation of javax.naming.spi.DirObjectFactory. Note: Setting this value to null may have cause connection leaks when using ContextMapper methods in LdapTemplate.- Parameters:
dirObjectFactory- the DirObjectFactory to be used. Null means that no DirObjectFactory will be used.
-
getDirObjectFactory
Get the DirObjectFactory to use.- Returns:
- the DirObjectFactory to be used.
nullmeans that no DirObjectFactory will be used.
-
afterPropertiesSet
public void afterPropertiesSet()Checks that all necessary data is set and that there is no compatibility issues, after which the instance is initialized. Note that you need to call this method explicitly after setting all desired properties if using the class outside of a Spring Context.- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean
-
setPassword
Set the password (credentials) to use for getting authenticated contexts.- Parameters:
password- the password.
-
getPassword
Gets the password (credentials) to use for getting authenticated contexts.- Returns:
- the password
-
setUserDn
Set the user distinguished name (principal) to use for getting authenticated contexts.- Parameters:
userDn- the user distinguished name.
-
getUserDn
Gets the user distinguished name (principal) to use for getting authenticated contexts.- Returns:
- the user distinguished name.
-
setUrls
Set the urls of the LDAP servers. Use this method if several servers are required.- Parameters:
urls- the urls of all servers.
-
getUrls
Get the urls of the LDAP servers.- Returns:
- the urls of all servers.
-
setUrl
Set the url of the LDAP server. Utility method if only one server is used.- Parameters:
url- the url of the LDAP server.
-
setPooled
public void setPooled(boolean pooled) Set whether the pooling flag should be set, enabling the built-in LDAP connection pooling. Default isfalse. The built-in LDAP connection pooling suffers from a number of deficiencies, e.g. no connection validation. Also, enabling this flag when using TLS connections will explicitly not work. Consider using the Spring LDAPPoolingContextSourceas an alternative instead of enabling this flag.Note that since LDAP pooling is system wide, full configuration of this needs be done using system parameters as specified in the LDAP/JNDI documentation. Also note, that pooling is done on user dn basis, i.e. each individually authenticated connection will be pooled separately. This means that LDAP pooling will be most efficient using anonymous connections or connections authenticated using one single system user.
- Parameters:
pooled- whether Contexts should be pooled.
-
isPooled
public boolean isPooled()Get whether the pooling flag should be set.- Returns:
- whether Contexts should be pooled.
-
setBaseEnvironmentProperties
If any custom environment properties are needed, these can be set using this method.- Parameters:
baseEnvironmentProperties- the base environment properties that should always be used when creating new Context instances.
-
getAnonymousEnv
-
getAuthenticatedEnv
-
setAuthenticationSource
Set the authentication source to use when retrieving user principal and credentials.- Parameters:
authenticationSource- theAuthenticationSourcethat will provide user info.
-
getAuthenticationSource
Get the authentication source.- Returns:
- the
AuthenticationSourcethat will provide user info.
-
setCacheEnvironmentProperties
public void setCacheEnvironmentProperties(boolean cacheEnvironmentProperties) Set whether environment properties should be cached between requsts for anonymous environment. Default istrue; setting this property tofalsecauses the environment Hashmap to be rebuilt from the current property settings of this instance between each request for an anonymous environment.- Parameters:
cacheEnvironmentProperties-truecauses that the anonymous environment properties should be cached,falsecauses the Hashmap to be rebuilt for each request.
-
setAnonymousReadOnly
public void setAnonymousReadOnly(boolean anonymousReadOnly) Set whether an anonymous environment should be used for read-only operations. Default isfalse.- Parameters:
anonymousReadOnly-trueif an anonymous environment should be used for read-only operations,falseotherwise.
-
isAnonymousReadOnly
public boolean isAnonymousReadOnly()Get whether an anonymous environment should be used for read-only operations.- Returns:
trueif an anonymous environment should be used for read-only operations,falseotherwise.
-
setAuthenticationStrategy
Set theDirContextAuthenticationStrategyto use for preparing the environment and processing the createdDirContextinstances.- Parameters:
authenticationStrategy- theDirContextAuthenticationStrategyto use; default isSimpleDirContextAuthenticationStrategy.
-
setReferral
Set the method to handle referrals. Default is 'ignore'; setting this flag to 'follow' will enable referrals to be automatically followed. Note that this might require particular name server setup in order to work (the referred URLs will need to be automatically found using standard DNS resolution).- Parameters:
referral- the value to set the system propertyContext.REFERRALto, customizing the way that referrals are handled.
-
getDirContextInstance
protected abstract DirContext getDirContextInstance(Hashtable<String, Object> environment) throws NamingExceptionImplement in subclass to create a DirContext of the desired type (e.g. InitialDirContext or InitialLdapContext).- Parameters:
environment- the environment to use when creating the instance.- Returns:
- a new DirContext instance.
- Throws:
NamingException- if one is encountered when creating the instance.
-
getPassword()andsetPassword(String)instead