16
16
17
17
package org.springframework.security.config.web.servlet
18
18
19
+ import io.mockk.every
20
+ import io.mockk.mockkObject
21
+ import io.mockk.verify
19
22
import org.junit.jupiter.api.Test
20
23
import org.junit.jupiter.api.extension.ExtendWith
21
24
import org.springframework.beans.factory.annotation.Autowired
22
25
import org.springframework.context.annotation.Configuration
26
+ import org.springframework.security.authentication.AuthenticationDetailsSource
23
27
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
24
28
import org.springframework.security.config.annotation.web.builders.HttpSecurity
25
29
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
@@ -36,6 +40,7 @@ import org.springframework.test.web.servlet.get
36
40
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl
37
41
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
38
42
import org.springframework.web.bind.annotation.GetMapping
43
+ import javax.servlet.http.HttpServletRequest
39
44
40
45
/* *
41
46
* Tests for [FormLoginDsl]
@@ -280,6 +285,42 @@ class FormLoginDslTests {
280
285
}
281
286
}
282
287
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
+
283
324
@Configuration
284
325
open class UserConfig {
285
326
@Autowired
0 commit comments