Skip to content

Commit 869fc8c

Browse files
committed
Use LDAP AuthenticationManager factory in reference docs
Closes gh-10789
1 parent 0882136 commit 869fc8c

File tree

1 file changed

+110
-86
lines changed
  • docs/modules/ROOT/pages/servlet/authentication/passwords

1 file changed

+110
-86
lines changed

docs/modules/ROOT/pages/servlet/authentication/passwords/ldap.adoc

Lines changed: 110 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,39 @@ depenendencies {
120120
----
121121
====
122122

123-
You can then configure the Embedded LDAP Server:
123+
You can then configure the Embedded LDAP Server using an `EmbeddedLdapServerContextSourceFactoryBean`.
124+
This will instruct Spring Security to start an in-memory LDAP server:
124125

125126
.Embedded LDAP Server Configuration
126127
====
127128
.Java
128129
[source,java,role="primary"]
129130
----
130131
@Bean
132+
public EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
133+
return EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer();
134+
}
135+
----
136+
137+
.Kotlin
138+
[source,kotlin,role="secondary"]
139+
----
140+
@Bean
141+
fun contextSourceFactoryBean(): EmbeddedLdapServerContextSourceFactoryBean {
142+
return EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer()
143+
}
144+
----
145+
====
146+
147+
Alternatively, you can manually configure the Embedded LDAP Server.
148+
If you choose this approach, you will be responsible for managing the lifecycle of the Embedded LDAP Server.
149+
150+
.Explicit Embedded LDAP Server Configuration
151+
====
152+
.Java
153+
[source,java,role="primary"]
154+
----
155+
@Bean
131156
UnboundIdContainer ldapContainer() {
132157
return new UnboundIdContainer("dc=springframework,dc=org",
133158
"classpath:users.ldif");
@@ -229,7 +254,36 @@ fun ldapContainer(): ApacheDSContainer {
229254
== LDAP ContextSource
230255

231256
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.
232-
To do so, create an LDAP `ContextSource` (which is the equivalent of a JDBC `DataSource`):
257+
To do so, create an LDAP `ContextSource` (which is the equivalent of a JDBC `DataSource`).
258+
If you have already configured an `EmbeddedLdapServerContextSourceFactoryBean`, Spring Security will create an LDAP `ContextSource` that points to the embedded LDAP server.
259+
260+
.LDAP Context Source with Embedded LDAP Server
261+
====
262+
.Java
263+
[source,java,role="primary"]
264+
----
265+
@Bean
266+
public EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
267+
EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean =
268+
EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer();
269+
contextSourceFactoryBean.setPort(0);
270+
return contextSourceFactoryBean;
271+
}
272+
----
273+
274+
.Kotlin
275+
[source,kotlin,role="secondary"]
276+
----
277+
@Bean
278+
fun contextSourceFactoryBean(): EmbeddedLdapServerContextSourceFactoryBean {
279+
val contextSourceFactoryBean = EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer()
280+
contextSourceFactoryBean.setPort(0)
281+
return contextSourceFactoryBean
282+
}
283+
----
284+
====
285+
286+
Alternatively, you can explicitly configure the LDAP `ContextSource` to connect to the supplied LDAP server:
233287

234288
.LDAP Context Source
235289
====
@@ -288,15 +342,10 @@ The following example shows bind authentication configuration:
288342
[source,java,role="primary",attrs="-attributes"]
289343
----
290344
@Bean
291-
BindAuthenticator authenticator(BaseLdapPathContextSource contextSource) {
292-
BindAuthenticator authenticator = new BindAuthenticator(contextSource);
293-
authenticator.setUserDnPatterns(new String[] { "uid={0},ou=people" });
294-
return authenticator;
295-
}
296-
297-
@Bean
298-
LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticator) {
299-
return new LdapAuthenticationProvider(authenticator);
345+
AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) {
346+
LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource);
347+
factory.setUserDnPatterns("uid={0},ou=people");
348+
return factory.createAuthenticationManager();
300349
}
301350
----
302351
@@ -311,15 +360,10 @@ LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticato
311360
[source,kotlin,role="secondary",attrs="-attributes"]
312361
----
313362
@Bean
314-
fun authenticator(contextSource: BaseLdapPathContextSource): BindAuthenticator {
315-
val authenticator = BindAuthenticator(contextSource)
316-
authenticator.setUserDnPatterns(arrayOf("uid={0},ou=people"))
317-
return authenticator
318-
}
319-
320-
@Bean
321-
fun authenticationProvider(authenticator: LdapAuthenticator): LdapAuthenticationProvider {
322-
return LdapAuthenticationProvider(authenticator)
363+
fun authenticationManager(contextSource: BaseLdapPathContextSource): AuthenticationManager {
364+
val factory = LdapBindAuthenticationManagerFactory(contextSource)
365+
factory.setUserDnPatterns("uid={0},ou=people")
366+
return factory.createAuthenticationManager()
323367
}
324368
----
325369
====
@@ -334,19 +378,11 @@ If, instead, you wish to configure an LDAP search filter to locate the user, you
334378
[source,java,role="primary",attrs="-attributes"]
335379
----
336380
@Bean
337-
BindAuthenticator authenticator(BaseLdapPathContextSource contextSource) {
338-
String searchBase = "ou=people";
339-
String filter = "(uid={0})";
340-
FilterBasedLdapUserSearch search =
341-
new FilterBasedLdapUserSearch(searchBase, filter, contextSource);
342-
BindAuthenticator authenticator = new BindAuthenticator(contextSource);
343-
authenticator.setUserSearch(search);
344-
return authenticator;
345-
}
346-
347-
@Bean
348-
LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticator) {
349-
return new LdapAuthenticationProvider(authenticator);
381+
AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) {
382+
LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource);
383+
factory.setUserSearchFilter("(uid={0})");
384+
factory.setUserSearchBase("ou=people");
385+
return factory.createAuthenticationManager();
350386
}
351387
----
352388
@@ -362,18 +398,11 @@ LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticato
362398
[source,kotlin,role="secondary",attrs="-attributes"]
363399
----
364400
@Bean
365-
fun authenticator(contextSource: BaseLdapPathContextSource): BindAuthenticator {
366-
val searchBase = "ou=people"
367-
val filter = "(uid={0})"
368-
val search = FilterBasedLdapUserSearch(searchBase, filter, contextSource)
369-
val authenticator = BindAuthenticator(contextSource)
370-
authenticator.setUserSearch(search)
371-
return authenticator
372-
}
373-
374-
@Bean
375-
fun authenticationProvider(authenticator: LdapAuthenticator): LdapAuthenticationProvider {
376-
return LdapAuthenticationProvider(authenticator)
401+
fun authenticationManager(contextSource: BaseLdapPathContextSource): AuthenticationManager {
402+
val factory = LdapBindAuthenticationManagerFactory(contextSource)
403+
factory.setUserSearchFilter("(uid={0})")
404+
factory.setUserSearchBase("ou=people")
405+
return factory.createAuthenticationManager()
377406
}
378407
----
379408
====
@@ -395,13 +424,11 @@ An LDAP compare cannot be done when the password is properly hashed with a rando
395424
[source,java,role="primary"]
396425
----
397426
@Bean
398-
PasswordComparisonAuthenticator authenticator(BaseLdapPathContextSource contextSource) {
399-
return new PasswordComparisonAuthenticator(contextSource);
400-
}
401-
402-
@Bean
403-
LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticator) {
404-
return new LdapAuthenticationProvider(authenticator);
427+
AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) {
428+
LdapPasswordComparisonAuthenticationManagerFactory factory = new LdapPasswordComparisonAuthenticationManagerFactory(
429+
contextSource, NoOpPasswordEncoder.getInstance());
430+
factory.setUserDnPatterns("uid={0},ou=people");
431+
return factory.createAuthenticationManager();
405432
}
406433
----
407434
@@ -418,13 +445,12 @@ LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticato
418445
[source,kotlin,role="secondary"]
419446
----
420447
@Bean
421-
fun authenticator(contextSource: BaseLdapPathContextSource): PasswordComparisonAuthenticator {
422-
return PasswordComparisonAuthenticator(contextSource)
423-
}
424-
425-
@Bean
426-
fun authenticationProvider(authenticator: LdapAuthenticator): LdapAuthenticationProvider {
427-
return LdapAuthenticationProvider(authenticator)
448+
fun authenticationManager(contextSource: BaseLdapPathContextSource?): AuthenticationManager? {
449+
val factory = LdapPasswordComparisonAuthenticationManagerFactory(
450+
contextSource, NoOpPasswordEncoder.getInstance()
451+
)
452+
factory.setUserDnPatterns("uid={0},ou=people")
453+
return factory.createAuthenticationManager()
428454
}
429455
----
430456
====
@@ -437,17 +463,12 @@ The following example shows a more advanced configuration with some customizatio
437463
[source,java,role="primary"]
438464
----
439465
@Bean
440-
PasswordComparisonAuthenticator authenticator(BaseLdapPathContextSource contextSource) {
441-
PasswordComparisonAuthenticator authenticator =
442-
new PasswordComparisonAuthenticator(contextSource);
443-
authenticator.setPasswordAttributeName("pwd"); // <1>
444-
authenticator.setPasswordEncoder(new BCryptPasswordEncoder()); // <2>
445-
return authenticator;
446-
}
447-
448-
@Bean
449-
LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticator) {
450-
return new LdapAuthenticationProvider(authenticator);
466+
AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) {
467+
LdapPasswordComparisonAuthenticationManagerFactory factory = new LdapPasswordComparisonAuthenticationManagerFactory(
468+
contextSource, new BCryptPasswordEncoder());
469+
factory.setUserDnPatterns("uid={0},ou=people");
470+
factory.setPasswordAttribute("pwd"); // <1>
471+
return factory.createAuthenticationManager();
451472
}
452473
----
453474
@@ -468,23 +489,18 @@ LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticato
468489
[source,kotlin,role="secondary"]
469490
----
470491
@Bean
471-
fun authenticator(contextSource: BaseLdapPathContextSource): PasswordComparisonAuthenticator {
472-
val authenticator = PasswordComparisonAuthenticator(contextSource)
473-
authenticator.setPasswordAttributeName("pwd") // <1>
474-
authenticator.setPasswordEncoder(BCryptPasswordEncoder()) // <2>
475-
return authenticator
476-
}
477-
478-
@Bean
479-
fun authenticationProvider(authenticator: LdapAuthenticator): LdapAuthenticationProvider {
480-
return LdapAuthenticationProvider(authenticator)
492+
fun authenticationManager(contextSource: BaseLdapPathContextSource): AuthenticationManager {
493+
val factory = LdapPasswordComparisonAuthenticationManagerFactory(
494+
contextSource, BCryptPasswordEncoder()
495+
)
496+
factory.setUserDnPatterns("uid={0},ou=people")
497+
factory.setPasswordAttribute("pwd") // <1>
498+
return factory.createAuthenticationManager()
481499
}
482500
----
483501
====
484502

485503
<1> Specify the password attribute as `pwd`.
486-
<2> Use `BCryptPasswordEncoder`.
487-
488504

489505
== LdapAuthoritiesPopulator
490506

@@ -506,8 +522,11 @@ LdapAuthoritiesPopulator authorities(BaseLdapPathContextSource contextSource) {
506522
}
507523
508524
@Bean
509-
LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticator, LdapAuthoritiesPopulator authorities) {
510-
return new LdapAuthenticationProvider(authenticator, authorities);
525+
AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource, LdapAuthoritiesPopulator authorities) {
526+
LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource);
527+
factory.setUserDnPatterns("uid={0},ou=people");
528+
factory.setLdapAuthoritiesPopulator(authorities);
529+
return factory.createAuthenticationManager();
511530
}
512531
----
513532
@@ -531,8 +550,13 @@ fun authorities(contextSource: BaseLdapPathContextSource): LdapAuthoritiesPopula
531550
}
532551
533552
@Bean
534-
fun authenticationProvider(authenticator: LdapAuthenticator, authorities: LdapAuthoritiesPopulator): LdapAuthenticationProvider {
535-
return LdapAuthenticationProvider(authenticator, authorities)
553+
fun authenticationManager(
554+
contextSource: BaseLdapPathContextSource,
555+
authorities: LdapAuthoritiesPopulator): AuthenticationManager {
556+
val factory = LdapBindAuthenticationManagerFactory(contextSource)
557+
factory.setUserDnPatterns("uid={0},ou=people")
558+
factory.setLdapAuthoritiesPopulator(authorities)
559+
return factory.createAuthenticationManager()
536560
}
537561
----
538562
====

0 commit comments

Comments
 (0)