Alternatively, you can manually configure the Embedded LDAP Server.
If you choose this approach, you will be responsible for managing the lifecycle of the Embedded LDAP Server.
.Explicit Embedded LDAP Server Configuration
====
.Java
[source,java,role="primary"]
----
@Bean
UnboundIdContainer ldapContainer() {
return new UnboundIdContainer("dc=springframework,dc=org",
"classpath:users.ldif");
@ -229,7 +254,36 @@ fun ldapContainer(): ApacheDSContainer {
@@ -229,7 +254,36 @@ fun ldapContainer(): ApacheDSContainer {
== LDAP ContextSource
Once you have an LDAP Server to which to point your configuration, you need to configure Spring Security to point to an LDAP server that should be used to authenticate users.
To do so, create an LDAP `ContextSource` (which is the equivalent of a JDBC `DataSource`):
To do so, create an LDAP `ContextSource` (which is the equivalent of a JDBC `DataSource`).
If you have already configured an `EmbeddedLdapServerContextSourceFactoryBean`, Spring Security will create an LDAP `ContextSource` that points to the embedded LDAP server.
.LDAP Context Source with Embedded LDAP Server
====
.Java
[source,java,role="primary"]
----
@Bean
public EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
fun contextSourceFactoryBean(): EmbeddedLdapServerContextSourceFactoryBean {
val contextSourceFactoryBean = EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer()
contextSourceFactoryBean.setPort(0)
return contextSourceFactoryBean
}
----
====
Alternatively, you can explicitly configure the LDAP `ContextSource` to connect to the supplied LDAP server:
.LDAP Context Source
====
@ -288,15 +342,10 @@ The following example shows bind authentication configuration:
@@ -288,15 +342,10 @@ The following example shows bind authentication configuration:
fun authenticationProvider(authenticator: LdapAuthenticator): LdapAuthenticationProvider {
return LdapAuthenticationProvider(authenticator)
fun authenticationManager(contextSource: BaseLdapPathContextSource): AuthenticationManager {
val factory = LdapBindAuthenticationManagerFactory(contextSource)
factory.setUserDnPatterns("uid={0},ou=people")
return factory.createAuthenticationManager()
}
----
====
@ -334,19 +378,11 @@ If, instead, you wish to configure an LDAP search filter to locate the user, you
@@ -334,19 +378,11 @@ If, instead, you wish to configure an LDAP search filter to locate the user, you
fun authenticator(contextSource: BaseLdapPathContextSource): BindAuthenticator {
val searchBase = "ou=people"
val filter = "(uid={0})"
val search = FilterBasedLdapUserSearch(searchBase, filter, contextSource)
val authenticator = BindAuthenticator(contextSource)
authenticator.setUserSearch(search)
return authenticator
}
@Bean
fun authenticationProvider(authenticator: LdapAuthenticator): LdapAuthenticationProvider {
return LdapAuthenticationProvider(authenticator)
fun authenticationManager(contextSource: BaseLdapPathContextSource): AuthenticationManager {
val factory = LdapBindAuthenticationManagerFactory(contextSource)
factory.setUserSearchFilter("(uid={0})")
factory.setUserSearchBase("ou=people")
return factory.createAuthenticationManager()
}
----
====
@ -395,13 +424,11 @@ An LDAP compare cannot be done when the password is properly hashed with a rando
@@ -395,13 +424,11 @@ An LDAP compare cannot be done when the password is properly hashed with a rando
fun authenticationProvider(authenticator: LdapAuthenticator): LdapAuthenticationProvider {
return LdapAuthenticationProvider(authenticator)
fun authenticationManager(contextSource: BaseLdapPathContextSource?): AuthenticationManager? {
val factory = LdapPasswordComparisonAuthenticationManagerFactory(
contextSource, NoOpPasswordEncoder.getInstance()
)
factory.setUserDnPatterns("uid={0},ou=people")
return factory.createAuthenticationManager()
}
----
====
@ -437,17 +463,12 @@ The following example shows a more advanced configuration with some customizatio
@@ -437,17 +463,12 @@ The following example shows a more advanced configuration with some customizatio