Skip to content

Commit bdc0bd6

Browse files
ty-v1marcusdacoregio
authored andcommitted
Add usernameParameter and passwordParameter to FormLoginDsl
Closes gh-14474
1 parent 7a8f9b4 commit bdc0bd6

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,6 +38,8 @@ import jakarta.servlet.http.HttpServletRequest
3838
* @property loginProcessingUrl the URL to validate the credentials
3939
* @property permitAll whether to grant access to the urls for [failureUrl] as well as
4040
* for the [HttpSecurityBuilder], the [loginPage] and [loginProcessingUrl] for every user
41+
* @property usernameParameter the HTTP parameter to look for the username when performing authentication
42+
* @property passwordParameter the HTTP parameter to look for the password when performing authentication
4143
*/
4244
@SecurityMarker
4345
class FormLoginDsl {
@@ -48,6 +50,8 @@ class FormLoginDsl {
4850
var loginProcessingUrl: String? = null
4951
var permitAll: Boolean? = null
5052
var authenticationDetailsSource: AuthenticationDetailsSource<HttpServletRequest, *>? = null
53+
var usernameParameter: String? = null
54+
var passwordParameter: String? = null
5155

5256
private var defaultSuccessUrlOption: Pair<String, Boolean>? = null
5357

@@ -95,6 +99,8 @@ class FormLoginDsl {
9599
authenticationSuccessHandler?.also { login.successHandler(authenticationSuccessHandler) }
96100
authenticationFailureHandler?.also { login.failureHandler(authenticationFailureHandler) }
97101
authenticationDetailsSource?.also { login.authenticationDetailsSource(authenticationDetailsSource) }
102+
usernameParameter?.also { login.usernameParameter(usernameParameter) }
103+
passwordParameter?.also { login.passwordParameter(passwordParameter) }
98104
if (disabled) {
99105
login.disable()
100106
}

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@ import org.springframework.security.config.test.SpringTestContextExtension
3333
import org.springframework.security.core.userdetails.User
3434
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin
3535
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf
36+
import org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated
3637
import org.springframework.security.web.SecurityFilterChain
3738
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler
3839
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler
@@ -367,6 +368,50 @@ class FormLoginDslTests {
367368
verify(exactly = 1) { CustomAuthenticationDetailsSourceConfig.AUTHENTICATION_DETAILS_SOURCE.buildDetails(any()) }
368369
}
369370

371+
@Configuration
372+
@EnableWebSecurity
373+
open class CustomUsernameParameterConfig {
374+
@Bean
375+
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
376+
http {
377+
formLogin {
378+
usernameParameter = "custom-username"
379+
}
380+
}
381+
return http.build()
382+
}
383+
}
384+
385+
@Test
386+
fun `form login when custom username parameter then used`() {
387+
this.spring.register(CustomUsernameParameterConfig::class.java, UserConfig::class.java).autowire()
388+
389+
this.mockMvc.perform(formLogin().userParameter("custom-username"))
390+
.andExpect(authenticated())
391+
}
392+
393+
@Configuration
394+
@EnableWebSecurity
395+
open class CustomPasswordParameterConfig {
396+
@Bean
397+
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
398+
http {
399+
formLogin {
400+
passwordParameter = "custom-password"
401+
}
402+
}
403+
return http.build()
404+
}
405+
}
406+
407+
@Test
408+
fun `form login when custom password parameter then used`() {
409+
this.spring.register(CustomPasswordParameterConfig::class.java, UserConfig::class.java).autowire()
410+
411+
this.mockMvc.perform(formLogin().passwordParam("custom-password"))
412+
.andExpect(authenticated())
413+
}
414+
370415
@Configuration
371416
@EnableWebSecurity
372417
open class CustomAuthenticationDetailsSourceConfig {

0 commit comments

Comments
 (0)