Skip to content

Commit b1612b1

Browse files
nmck257eleftherias
authored andcommitted
Add AuthenticationDetailsSource to Form Login Kotlin DSL
Closes gh-9837
1 parent c71498a commit b1612b1

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

config/src/main/kotlin/org/springframework/security/config/web/servlet/FormLoginDsl.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
package org.springframework.security.config.web.servlet
1818

19+
import org.springframework.security.authentication.AuthenticationDetailsSource
1920
import org.springframework.security.config.annotation.web.HttpSecurityBuilder
2021
import org.springframework.security.config.annotation.web.builders.HttpSecurity
2122
import org.springframework.security.config.annotation.web.configurers.FormLoginConfigurer
2223
import org.springframework.security.web.authentication.AuthenticationFailureHandler
2324
import org.springframework.security.web.authentication.AuthenticationSuccessHandler
25+
import javax.servlet.http.HttpServletRequest
2426

2527
/**
2628
* A Kotlin DSL to configure [HttpSecurity] form login using idiomatic Kotlin code.
@@ -46,6 +48,7 @@ class FormLoginDsl {
4648
var failureUrl: String? = null
4749
var loginProcessingUrl: String? = null
4850
var permitAll: Boolean? = null
51+
var authenticationDetailsSource: AuthenticationDetailsSource<HttpServletRequest, *>? = null
4952

5053
private var defaultSuccessUrlOption: Pair<String, Boolean>? = null
5154

@@ -81,6 +84,7 @@ class FormLoginDsl {
8184
}
8285
authenticationSuccessHandler?.also { login.successHandler(authenticationSuccessHandler) }
8386
authenticationFailureHandler?.also { login.failureHandler(authenticationFailureHandler) }
87+
authenticationDetailsSource?.also { login.authenticationDetailsSource(authenticationDetailsSource) }
8488
}
8589
}
8690
}

config/src/test/kotlin/org/springframework/security/config/web/servlet/FormLoginDslTests.kt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616

1717
package org.springframework.security.config.web.servlet
1818

19+
import io.mockk.every
20+
import io.mockk.mockkObject
21+
import io.mockk.verify
1922
import org.junit.jupiter.api.Test
2023
import org.junit.jupiter.api.extension.ExtendWith
2124
import org.springframework.beans.factory.annotation.Autowired
2225
import org.springframework.context.annotation.Configuration
26+
import org.springframework.security.authentication.AuthenticationDetailsSource
2327
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
2428
import org.springframework.security.config.annotation.web.builders.HttpSecurity
2529
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
@@ -36,6 +40,7 @@ import org.springframework.test.web.servlet.get
3640
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl
3741
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
3842
import org.springframework.web.bind.annotation.GetMapping
43+
import javax.servlet.http.HttpServletRequest
3944

4045
/**
4146
* Tests for [FormLoginDsl]
@@ -280,6 +285,42 @@ class FormLoginDslTests {
280285
}
281286
}
282287

288+
@Test
289+
fun `form login when custom authentication details source then used`() {
290+
this.spring
291+
.register(CustomAuthenticationDetailsSourceConfig::class.java, UserConfig::class.java)
292+
.autowire()
293+
mockkObject(CustomAuthenticationDetailsSourceConfig.AUTHENTICATION_DETAILS_SOURCE)
294+
every {
295+
CustomAuthenticationDetailsSourceConfig.AUTHENTICATION_DETAILS_SOURCE.buildDetails(any())
296+
} returns Any()
297+
298+
this.mockMvc.perform(formLogin())
299+
.andExpect {
300+
status().isFound
301+
redirectedUrl("/")
302+
}
303+
304+
verify(exactly = 1) { CustomAuthenticationDetailsSourceConfig.AUTHENTICATION_DETAILS_SOURCE.buildDetails(any()) }
305+
}
306+
307+
@EnableWebSecurity
308+
open class CustomAuthenticationDetailsSourceConfig : WebSecurityConfigurerAdapter() {
309+
310+
companion object {
311+
val AUTHENTICATION_DETAILS_SOURCE: AuthenticationDetailsSource<HttpServletRequest, *> =
312+
AuthenticationDetailsSource<HttpServletRequest, Any> { Any() }
313+
}
314+
315+
override fun configure(http: HttpSecurity) {
316+
http {
317+
formLogin {
318+
authenticationDetailsSource = AUTHENTICATION_DETAILS_SOURCE
319+
}
320+
}
321+
}
322+
}
323+
283324
@Configuration
284325
open class UserConfig {
285326
@Autowired

0 commit comments

Comments
 (0)