@@ -118,14 +118,39 @@ depenendencies {
118118----
119119====
120120
121- You can then configure the Embedded LDAP Server
121+ You can then configure the Embedded LDAP Server using an `EmbeddedLdapServerContextSourceFactoryBean`.
122+ This will instruct Spring Security to start an in-memory LDAP server.
122123
123124.Embedded LDAP Server Configuration
124125====
125126.Java
126127[source,java,role="primary"]
127128----
128129@Bean
130+ public EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
131+ return EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer();
132+ }
133+ ----
134+
135+ .Kotlin
136+ [source,kotlin,role="secondary"]
137+ ----
138+ @Bean
139+ fun contextSourceFactoryBean(): EmbeddedLdapServerContextSourceFactoryBean {
140+ return EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer()
141+ }
142+ ----
143+ ====
144+
145+ Alternatively, you can manually configure the Embedded LDAP Server.
146+ If you choose this approach, you will be responsible for managing the lifecycle of the Embedded LDAP Server.
147+
148+ .Explicit Embedded LDAP Server Configuration
149+ ====
150+ .Java
151+ [source,java,role="primary"]
152+ ----
153+ @Bean
129154UnboundIdContainer ldapContainer() {
130155 return new UnboundIdContainer("dc=springframework,dc=org",
131156 "classpath:users.ldif");
@@ -228,6 +253,35 @@ fun ldapContainer(): ApacheDSContainer {
228253
229254Once you have an LDAP Server to point your configuration to, you need configure Spring Security to point to an LDAP server that should be used to authenticate users.
230255This is done by creating an LDAP `ContextSource`, which is the equivalent of a JDBC `DataSource`.
256+ If you have already configured an `EmbeddedLdapServerContextSourceFactoryBean`, Spring Security will create an LDAP `ContextSource` that points to the embedded LDAP server.
257+
258+ .LDAP Context Source with Embedded LDAP Server
259+ ====
260+ .Java
261+ [source,java,role="primary"]
262+ ----
263+ @Bean
264+ public EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
265+ EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean =
266+ EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer();
267+ contextSourceFactoryBean.setPort(0);
268+ return contextSourceFactoryBean;
269+ }
270+ ----
271+
272+ .Kotlin
273+ [source,kotlin,role="secondary"]
274+ ----
275+ @Bean
276+ fun contextSourceFactoryBean(): EmbeddedLdapServerContextSourceFactoryBean {
277+ val contextSourceFactoryBean = EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer()
278+ contextSourceFactoryBean.setPort(0)
279+ return contextSourceFactoryBean
280+ }
281+ ----
282+ ====
283+
284+ Alternatively, you can explicitly configure the LDAP `ContextSource` to connect to the supplied LDAP server.
231285
232286.LDAP Context Source
233287====
@@ -287,15 +341,10 @@ An example of bind authentication configuration can be found below.
287341[source,java,role="primary",attrs="-attributes"]
288342----
289343@Bean
290- BindAuthenticator authenticator(BaseLdapPathContextSource contextSource) {
291- BindAuthenticator authenticator = new BindAuthenticator(contextSource);
292- authenticator.setUserDnPatterns(new String[] { "uid={0},ou=people" });
293- return authenticator;
294- }
295-
296- @Bean
297- LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticator) {
298- return new LdapAuthenticationProvider(authenticator);
344+ AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) {
345+ LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource);
346+ factory.setUserDnPatterns("uid={0},ou=people");
347+ return factory.createAuthenticationManager();
299348}
300349----
301350
@@ -310,15 +359,10 @@ LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticato
310359[source,kotlin,role="secondary",attrs="-attributes"]
311360----
312361@Bean
313- fun authenticator(contextSource: BaseLdapPathContextSource): BindAuthenticator {
314- val authenticator = BindAuthenticator(contextSource)
315- authenticator.setUserDnPatterns(arrayOf("uid={0},ou=people"))
316- return authenticator
317- }
318-
319- @Bean
320- fun authenticationProvider(authenticator: LdapAuthenticator): LdapAuthenticationProvider {
321- return LdapAuthenticationProvider(authenticator)
362+ fun authenticationManager(contextSource: BaseLdapPathContextSource): AuthenticationManager {
363+ val factory = LdapBindAuthenticationManagerFactory(contextSource)
364+ factory.setUserDnPatterns("uid={0},ou=people")
365+ return factory.createAuthenticationManager()
322366}
323367----
324368====
@@ -333,19 +377,11 @@ If instead you wished to configure an LDAP search filter to locate the user, you
333377[source,java,role="primary",attrs="-attributes"]
334378----
335379@Bean
336- BindAuthenticator authenticator(BaseLdapPathContextSource contextSource) {
337- String searchBase = "ou=people";
338- String filter = "(uid={0})";
339- FilterBasedLdapUserSearch search =
340- new FilterBasedLdapUserSearch(searchBase, filter, contextSource);
341- BindAuthenticator authenticator = new BindAuthenticator(contextSource);
342- authenticator.setUserSearch(search);
343- return authenticator;
344- }
345-
346- @Bean
347- LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticator) {
348- return new LdapAuthenticationProvider(authenticator);
380+ AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) {
381+ LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource);
382+ factory.setUserSearchFilter("(uid={0})");
383+ factory.setUserSearchBase("ou=people");
384+ return factory.createAuthenticationManager();
349385}
350386----
351387
@@ -361,18 +397,11 @@ LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticato
361397[source,kotlin,role="secondary",attrs="-attributes"]
362398----
363399@Bean
364- fun authenticator(contextSource: BaseLdapPathContextSource): BindAuthenticator {
365- val searchBase = "ou=people"
366- val filter = "(uid={0})"
367- val search = FilterBasedLdapUserSearch(searchBase, filter, contextSource)
368- val authenticator = BindAuthenticator(contextSource)
369- authenticator.setUserSearch(search)
370- return authenticator
371- }
372-
373- @Bean
374- fun authenticationProvider(authenticator: LdapAuthenticator): LdapAuthenticationProvider {
375- return LdapAuthenticationProvider(authenticator)
400+ fun authenticationManager(contextSource: BaseLdapPathContextSource): AuthenticationManager {
401+ val factory = LdapBindAuthenticationManagerFactory(contextSource)
402+ factory.setUserSearchFilter("(uid={0})")
403+ factory.setUserSearchBase("ou=people")
404+ return factory.createAuthenticationManager()
376405}
377406----
378407====
@@ -394,13 +423,11 @@ An LDAP compare cannot be done when the password is properly hashed with a rando
394423[source,java,role="primary"]
395424----
396425@Bean
397- PasswordComparisonAuthenticator authenticator(BaseLdapPathContextSource contextSource) {
398- return new PasswordComparisonAuthenticator(contextSource);
399- }
400-
401- @Bean
402- LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticator) {
403- return new LdapAuthenticationProvider(authenticator);
426+ AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) {
427+ LdapPasswordComparisonAuthenticationManagerFactory factory = new LdapPasswordComparisonAuthenticationManagerFactory(
428+ contextSource, NoOpPasswordEncoder.getInstance());
429+ factory.setUserDnPatterns("uid={0},ou=people");
430+ return factory.createAuthenticationManager();
404431}
405432----
406433
@@ -417,13 +444,12 @@ LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticato
417444[source,kotlin,role="secondary"]
418445----
419446@Bean
420- fun authenticator(contextSource: BaseLdapPathContextSource): PasswordComparisonAuthenticator {
421- return PasswordComparisonAuthenticator(contextSource)
422- }
423-
424- @Bean
425- fun authenticationProvider(authenticator: LdapAuthenticator): LdapAuthenticationProvider {
426- return LdapAuthenticationProvider(authenticator)
447+ fun authenticationManager(contextSource: BaseLdapPathContextSource?): AuthenticationManager? {
448+ val factory = LdapPasswordComparisonAuthenticationManagerFactory(
449+ contextSource, NoOpPasswordEncoder.getInstance()
450+ )
451+ factory.setUserDnPatterns("uid={0},ou=people")
452+ return factory.createAuthenticationManager()
427453}
428454----
429455====
@@ -436,17 +462,12 @@ A more advanced configuration with some customizations can be found below.
436462[source,java,role="primary"]
437463----
438464@Bean
439- PasswordComparisonAuthenticator authenticator(BaseLdapPathContextSource contextSource) {
440- PasswordComparisonAuthenticator authenticator =
441- new PasswordComparisonAuthenticator(contextSource);
442- authenticator.setPasswordAttributeName("pwd"); // <1>
443- authenticator.setPasswordEncoder(new BCryptPasswordEncoder()); // <2>
444- return authenticator;
445- }
446-
447- @Bean
448- LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticator) {
449- return new LdapAuthenticationProvider(authenticator);
465+ AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) {
466+ LdapPasswordComparisonAuthenticationManagerFactory factory = new LdapPasswordComparisonAuthenticationManagerFactory(
467+ contextSource, new BCryptPasswordEncoder());
468+ factory.setUserDnPatterns("uid={0},ou=people");
469+ factory.setPasswordAttribute("pwd"); // <1>
470+ return factory.createAuthenticationManager();
450471}
451472----
452473
@@ -467,23 +488,18 @@ LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticato
467488[source,kotlin,role="secondary"]
468489----
469490@Bean
470- fun authenticator(contextSource: BaseLdapPathContextSource): PasswordComparisonAuthenticator {
471- val authenticator = PasswordComparisonAuthenticator(contextSource)
472- authenticator.setPasswordAttributeName("pwd") // <1>
473- authenticator.setPasswordEncoder(BCryptPasswordEncoder()) // <2>
474- return authenticator
475- }
476-
477- @Bean
478- fun authenticationProvider(authenticator: LdapAuthenticator): LdapAuthenticationProvider {
479- return LdapAuthenticationProvider(authenticator)
491+ fun authenticationManager(contextSource: BaseLdapPathContextSource): AuthenticationManager {
492+ val factory = LdapPasswordComparisonAuthenticationManagerFactory(
493+ contextSource, BCryptPasswordEncoder()
494+ )
495+ factory.setUserDnPatterns("uid={0},ou=people")
496+ factory.setPasswordAttribute("pwd") // <1>
497+ return factory.createAuthenticationManager()
480498}
481499----
482500====
483501
484502<1> Specify the password attribute as `pwd`
485- <2> Use `BCryptPasswordEncoder`
486-
487503
488504== LdapAuthoritiesPopulator
489505
@@ -504,8 +520,11 @@ LdapAuthoritiesPopulator authorities(BaseLdapPathContextSource contextSource) {
504520}
505521
506522@Bean
507- LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticator, LdapAuthoritiesPopulator authorities) {
508- return new LdapAuthenticationProvider(authenticator, authorities);
523+ AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource, LdapAuthoritiesPopulator authorities) {
524+ LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource);
525+ factory.setUserDnPatterns("uid={0},ou=people");
526+ factory.setLdapAuthoritiesPopulator(authorities);
527+ return factory.createAuthenticationManager();
509528}
510529----
511530
@@ -529,8 +548,13 @@ fun authorities(contextSource: BaseLdapPathContextSource): LdapAuthoritiesPopula
529548}
530549
531550@Bean
532- fun authenticationProvider(authenticator: LdapAuthenticator, authorities: LdapAuthoritiesPopulator): LdapAuthenticationProvider {
533- return LdapAuthenticationProvider(authenticator, authorities)
551+ fun authenticationManager(
552+ contextSource: BaseLdapPathContextSource,
553+ authorities: LdapAuthoritiesPopulator): AuthenticationManager {
554+ val factory = LdapBindAuthenticationManagerFactory(contextSource)
555+ factory.setUserDnPatterns("uid={0},ou=people")
556+ factory.setLdapAuthoritiesPopulator(authorities)
557+ return factory.createAuthenticationManager()
534558}
535559----
536560====
0 commit comments