Skip to content

Commit a2fb2c9

Browse files
committed
Kotlin examples in documentation
Issue: gh-5558
1 parent 9740b59 commit a2fb2c9

File tree

6 files changed

+240
-2
lines changed

6 files changed

+240
-2
lines changed

docs/manual/src/docs/asciidoc/_includes/about/authentication/password-storage.adoc

+9
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,15 @@ public static NoOpPasswordEncoder passwordEncoder() {
366366
<b:bean id="passwordEncoder"
367367
class="org.springframework.security.crypto.password.NoOpPasswordEncoder" factory-method="getInstance"/>
368368
----
369+
370+
.Kotlin
371+
[source,kotlin,role="secondary"]
372+
----
373+
@Bean
374+
fun passwordEncoder(): PasswordEncoder {
375+
return NoOpPasswordEncoder.getInstance();
376+
}
377+
----
369378
====
370379

371380
[NOTE]

docs/manual/src/docs/asciidoc/_includes/servlet/authentication/unpwd/input/basic.adoc

+11
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,15 @@ protected void configure(HttpSecurity http) {
2929
<http-basic />
3030
</http>
3131
----
32+
33+
[source,kotlin,role="secondary"]
34+
.Kotlin
35+
----
36+
fun configure(http: HttpSecurity) {
37+
http {
38+
// ...
39+
httpBasic { }
40+
}
41+
}
42+
----
3243
====

docs/manual/src/docs/asciidoc/_includes/servlet/authentication/unpwd/input/form.adoc

+25
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ protected void configure(HttpSecurity http) {
3232
<form-login />
3333
</http>
3434
----
35+
36+
.Kotlin
37+
[source,kotlin,role="secondary"]
38+
----
39+
fun configure(http: HttpSecurity) {
40+
http {
41+
// ...
42+
formLogin { }
43+
}
44+
}
45+
----
3546
====
3647

3748
In this configuration Spring Security will render a default log in page.
@@ -66,6 +77,20 @@ protected void configure(HttpSecurity http) throws Exception {
6677
<form-login login-page="/login" />
6778
</http>
6879
----
80+
81+
.Kotlin
82+
[source,kotlin,role="secondary"]
83+
----
84+
fun configure(http: HttpSecurity) {
85+
http {
86+
// ...
87+
formLogin {
88+
loginPage = "/login"
89+
permitAll()
90+
}
91+
}
92+
}
93+
----
6994
====
7095

7196
[[servlet-authentication-form-custom-html]]

docs/manual/src/docs/asciidoc/_includes/servlet/authentication/unpwd/storage/in-memory.adoc

+42-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ public UserDetailsService users() {
4040
authorities="ROLE_USER,ROLE_ADMIN" />
4141
</user-service>
4242
----
43+
44+
.Kotlin
45+
[source,kotlin,role="secondary"]
46+
----
47+
@Bean
48+
fun users(): UserDetailsService {
49+
val user = User.builder()
50+
.username("user")
51+
.password("{bcrypt}$2a$10\$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW")
52+
.roles("USER")
53+
.build()
54+
val admin = User.builder()
55+
.username("admin")
56+
.password("{bcrypt}$2a$10\$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW")
57+
.roles("USER", "ADMIN")
58+
.build()
59+
return InMemoryUserDetailsManager(user, admin)
60+
}
61+
----
4362
====
4463

4564
The samples above store the passwords in a secure format, but leave a lot to be desired in terms of getting started experience.
@@ -51,7 +70,8 @@ For this reason, `User.withDefaultPasswordEncoder` should only be used for "gett
5170

5271
.InMemoryUserDetailsManager with User.withDefaultPasswordEncoder
5372
====
54-
[source,java]
73+
.Java
74+
[source,java,role="primary"]
5575
----
5676
@Bean
5777
public UserDetailsService users() {
@@ -70,6 +90,27 @@ public UserDetailsService users() {
7090
return new InMemoryUserDetailsManager(user, admin);
7191
}
7292
----
93+
94+
.Kotlin
95+
[source,kotlin,role="secondary"]
96+
----
97+
@Bean
98+
fun users(): UserDetailsService {
99+
// The builder will ensure the passwords are encoded before saving in memory
100+
val users = User.withDefaultPasswordEncoder()
101+
val user = users
102+
.username("user")
103+
.password("password")
104+
.roles("USER")
105+
.build()
106+
val admin = users
107+
.username("admin")
108+
.password("password")
109+
.roles("USER", "ADMIN")
110+
.build()
111+
return InMemoryUserDetailsManager(user, admin)
112+
}
113+
----
73114
====
74115

75116
There is no simple way to use `User.withDefaultPasswordEncoder` with XML based configuration.

docs/manual/src/docs/asciidoc/_includes/servlet/authentication/unpwd/storage/jdbc.adoc

+34
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ DataSource dataSource() {
128128
<jdbc:script location="classpath:org/springframework/security/core/userdetails/jdbc/users.ddl"/>
129129
</jdbc:embedded-database>
130130
----
131+
132+
.Kotlin
133+
[source,kotlin,role="secondary"]
134+
----
135+
@Bean
136+
fun dataSource(): DataSource {
137+
return EmbeddedDatabaseBuilder()
138+
.setType(H2)
139+
.addScript("classpath:org/springframework/security/core/userdetails/jdbc/users.ddl")
140+
.build()
141+
}
142+
----
131143
====
132144

133145
In a production environment, you will want to ensure you setup a connection to an external database.
@@ -173,4 +185,26 @@ UserDetailsManager users(DataSource dataSource) {
173185
authorities="ROLE_USER,ROLE_ADMIN" />
174186
</jdbc-user-service>
175187
----
188+
189+
.Kotlin
190+
[source,kotlin,role="secondary"]
191+
----
192+
@Bean
193+
fun users(dataSource: DataSource): UserDetailsManager {
194+
val user = User.builder()
195+
.username("user")
196+
.password("{bcrypt}$2a$10\$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW")
197+
.roles("USER")
198+
.build();
199+
val admin = User.builder()
200+
.username("admin")
201+
.password("{bcrypt}$2a$10\$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW")
202+
.roles("USER", "ADMIN")
203+
.build();
204+
val users = JdbcUserDetailsManager(dataSource)
205+
users.createUser(user)
206+
users.createUser(admin)
207+
return users
208+
}
209+
----
176210
====

docs/manual/src/docs/asciidoc/_includes/servlet/authentication/unpwd/storage/ldap.adoc

+119-1
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,21 @@ UnboundIdContainer ldapContainer() {
133133
----
134134
135135
.XML
136-
[source,xml]
136+
[source,xml,role="secondary"]
137137
----
138138
<b:bean class="org.springframework.security.ldap.server.UnboundIdContainer"
139139
c:defaultPartitionSuffix="dc=springframework,dc=org"
140140
c:ldif="classpath:users.ldif"/>
141141
----
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+
----
142151
====
143152

144153
[[servlet-authentication-ldap-apacheds]]
@@ -203,6 +212,15 @@ ApacheDSContainer ldapContainer() {
203212
c:defaultPartitionSuffix="dc=springframework,dc=org"
204213
c:ldif="classpath:users.ldif"/>
205214
----
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+
----
206224
====
207225

208226
[[servlet-authentication-ldap-contextsource]]
@@ -227,6 +245,14 @@ ContextSource contextSource(UnboundIdContainer container) {
227245
<ldap-server
228246
url="ldap://localhost:53389/dc=springframework,dc=org" />
229247
----
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+
----
230256
====
231257

232258
[[servlet-authentication-ldap-authentication]]
@@ -279,6 +305,22 @@ LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticato
279305
<ldap-authentication-provider
280306
user-dn-pattern="uid={0},ou=people"/>
281307
----
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+
----
282324
====
283325

284326
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
314356
user-search-filter="(uid={0})"
315357
user-search-base="ou=people"/>
316358
----
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+
----
317378
====
318379

319380
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
351412
<password-compare />
352413
</ldap-authentication-provider>
353414
----
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+
----
354429
====
355430

356431
A more advanced configuration with some customizations can be found below.
@@ -387,6 +462,23 @@ LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticato
387462
<b:bean id="passwordEncoder"
388463
class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />
389464
----
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+
----
390482
====
391483

392484
<1> Specify the password attribute as `pwd`
@@ -424,6 +516,23 @@ LdapAuthenticationProvider authenticationProvider(LdapAuthenticator authenticato
424516
user-dn-pattern="uid={0},ou=people"
425517
group-search-filter="member={0}"/>
426518
----
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+
----
427536
====
428537

429538
== Active Directory
@@ -457,4 +566,13 @@ ActiveDirectoryLdapAuthenticationProvider authenticationProvider() {
457566
<constructor-arg value="ldap://company.example.com/" />
458567
</bean>
459568
----
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+
----
460578
====

0 commit comments

Comments
 (0)