@@ -133,12 +133,21 @@ UnboundIdContainer ldapContainer() {
133
133
----
134
134
135
135
.XML
136
- [source,xml]
136
+ [source,xml,role="secondary" ]
137
137
----
138
138
<b:bean class="org.springframework.security.ldap.server.UnboundIdContainer"
139
139
c:defaultPartitionSuffix="dc=springframework,dc=org"
140
140
c:ldif="classpath:users.ldif"/>
141
141
----
142
+
143
+ .Kotlin
144
+ [source,kotlin,role="secondary"]
145
+ ----
146
+ @Bean
147
+ fun ldapContainer(): UnboundIdContainer {
148
+ return UnboundIdContainer("dc=springframework,dc=org","classpath:users.ldif")
149
+ }
150
+ ----
142
151
====
143
152
144
153
[[servlet-authentication-ldap-apacheds]]
@@ -203,6 +212,15 @@ ApacheDSContainer ldapContainer() {
203
212
c:defaultPartitionSuffix="dc=springframework,dc=org"
204
213
c:ldif="classpath:users.ldif"/>
205
214
----
215
+
216
+ .Kotlin
217
+ [source,kotlin,role="secondary"]
218
+ ----
219
+ @Bean
220
+ fun ldapContainer(): ApacheDSContainer {
221
+ return ApacheDSContainer("dc=springframework,dc=org", "classpath:users.ldif")
222
+ }
223
+ ----
206
224
====
207
225
208
226
[[servlet-authentication-ldap-contextsource]]
@@ -227,6 +245,14 @@ ContextSource contextSource(UnboundIdContainer container) {
227
245
<ldap-server
228
246
url="ldap://localhost:53389/dc=springframework,dc=org" />
229
247
----
248
+
249
+ .Kotlin
250
+ [source,kotlin,role="secondary"]
251
+ ----
252
+ fun contextSource(container: UnboundIdContainer): ContextSource {
253
+ return DefaultSpringSecurityContextSource("ldap://localhost:53389/dc=springframework,dc=org")
254
+ }
255
+ ----
230
256
====
231
257
232
258
[[servlet-authentication-ldap-authentication]]
@@ -279,6 +305,22 @@ LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticato
279
305
<ldap-authentication-provider
280
306
user-dn-pattern="uid={0},ou=people"/>
281
307
----
308
+
309
+ .Kotlin
310
+ [source,kotlin,role="secondary"]
311
+ ----
312
+ @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)
322
+ }
323
+ ----
282
324
====
283
325
284
326
This simple example would obtain the DN for the user by substituting the user login name in the supplied pattern and attempting to bind as that user with the login password.
@@ -314,6 +356,25 @@ LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticato
314
356
user-search-filter="(uid={0})"
315
357
user-search-base="ou=people"/>
316
358
----
359
+
360
+ .Kotlin
361
+ [source,kotlin,role="secondary"]
362
+ ----
363
+ @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)
376
+ }
377
+ ----
317
378
====
318
379
319
380
If used with the `ContextSource` <<servlet-authentication-ldap-contextsource,definition above>>, this would perform a search under the DN `ou=people,dc=springframework,dc=org` using `(uid={0})` as a filter.
@@ -351,6 +412,20 @@ LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticato
351
412
<password-compare />
352
413
</ldap-authentication-provider>
353
414
----
415
+
416
+ .Kotlin
417
+ [source,kotlin,role="secondary"]
418
+ ----
419
+ @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)
427
+ }
428
+ ----
354
429
====
355
430
356
431
A more advanced configuration with some customizations can be found below.
@@ -387,6 +462,23 @@ LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticato
387
462
<b:bean id="passwordEncoder"
388
463
class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />
389
464
----
465
+
466
+ .Kotlin
467
+ [source,kotlin,role="secondary"]
468
+ ----
469
+ @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)
480
+ }
481
+ ----
390
482
====
391
483
392
484
<1> Specify the password attribute as `pwd`
@@ -424,6 +516,23 @@ LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticato
424
516
user-dn-pattern="uid={0},ou=people"
425
517
group-search-filter="member={0}"/>
426
518
----
519
+
520
+ .Kotlin
521
+ [source,kotlin,role="secondary"]
522
+ ----
523
+ @Bean
524
+ fun authorities(contextSource: BaseLdapPathContextSource): LdapAuthoritiesPopulator {
525
+ val groupSearchBase = ""
526
+ val authorities = DefaultLdapAuthoritiesPopulator(contextSource, groupSearchBase)
527
+ authorities.setGroupSearchFilter("member={0}")
528
+ return authorities
529
+ }
530
+
531
+ @Bean
532
+ fun authenticationProvider(authenticator: LdapAuthenticator, authorities: LdapAuthoritiesPopulator): LdapAuthenticationProvider {
533
+ return LdapAuthenticationProvider(authenticator, authorities)
534
+ }
535
+ ----
427
536
====
428
537
429
538
== Active Directory
@@ -457,4 +566,13 @@ ActiveDirectoryLdapAuthenticationProvider authenticationProvider() {
457
566
<constructor-arg value="ldap://company.example.com/" />
458
567
</bean>
459
568
----
569
+
570
+ .Kotlin
571
+ [source,kotlin,role="secondary"]
572
+ ----
573
+ @Bean
574
+ fun authenticationProvider(): ActiveDirectoryLdapAuthenticationProvider {
575
+ return ActiveDirectoryLdapAuthenticationProvider("example.com", "ldap://company.example.com/")
576
+ }
577
+ ----
460
578
====
0 commit comments