Skip to content

Commit a2abe3c

Browse files
committed
Add HttpMessageConverter WebAuthnDsl Support
Issue gh-16397
1 parent 683f1f4 commit a2abe3c

File tree

2 files changed

+41
-0
lines changed
  • config/src

2 files changed

+41
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

19+
import org.springframework.http.converter.HttpMessageConverter
1920
import org.springframework.security.config.annotation.web.builders.HttpSecurity
2021
import org.springframework.security.config.annotation.web.configurers.WebAuthnConfigurer
2122
import org.springframework.security.web.webauthn.registration.PublicKeyCredentialCreationOptionsRepository
@@ -37,6 +38,7 @@ class WebAuthnDsl {
3738
var allowedOrigins: Set<String>? = null
3839
var disableDefaultRegistrationPage: Boolean? = false
3940
var creationOptionsRepository: PublicKeyCredentialCreationOptionsRepository? = null
41+
var messageConverter: HttpMessageConverter<Any>? = null
4042

4143
internal fun get(): (WebAuthnConfigurer<HttpSecurity>) -> Unit {
4244
return { webAuthn ->
@@ -45,6 +47,7 @@ class WebAuthnDsl {
4547
allowedOrigins?.also { webAuthn.allowedOrigins(allowedOrigins) }
4648
disableDefaultRegistrationPage?.also { webAuthn.disableDefaultRegistrationPage(disableDefaultRegistrationPage!!) }
4749
creationOptionsRepository?.also { webAuthn.creationOptionsRepository(creationOptionsRepository) }
50+
messageConverter?.also { webAuthn.messageConverter(messageConverter) }
4851
}
4952
}
5053
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.junit.jupiter.api.extension.ExtendWith
2222
import org.springframework.beans.factory.annotation.Autowired
2323
import org.springframework.context.annotation.Bean
2424
import org.springframework.context.annotation.Configuration
25+
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
2526
import org.springframework.security.config.annotation.web.builders.HttpSecurity
2627
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
2728
import org.springframework.security.config.test.SpringTestContext
@@ -69,6 +70,16 @@ class WebAuthnDslTests {
6970
}
7071
}
7172

73+
@Test
74+
fun `explicit HttpMessageConverter`() {
75+
this.spring.register(ExplicitHttpMessageConverterConfig::class.java).autowire()
76+
77+
this.mockMvc.post("/test1")
78+
.andExpect {
79+
status { isForbidden() }
80+
}
81+
}
82+
7283
@Test
7384
fun `webauthn and formLogin configured with default registration page`() {
7485
spring.register(DefaultWebauthnConfig::class.java).autowire()
@@ -166,6 +177,33 @@ class WebAuthnDslTests {
166177
}
167178
}
168179

180+
@Configuration
181+
@EnableWebSecurity
182+
open class ExplicitHttpMessageConverterConfig {
183+
@Bean
184+
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
185+
http {
186+
webAuthn {
187+
rpName = "Spring Security Relying Party"
188+
rpId = "example.com"
189+
allowedOrigins = setOf("https://example.com")
190+
messageConverter = MappingJackson2HttpMessageConverter()
191+
}
192+
}
193+
return http.build()
194+
}
195+
196+
@Bean
197+
open fun userDetailsService(): UserDetailsService {
198+
val userDetails = User.withDefaultPasswordEncoder()
199+
.username("rod")
200+
.password("password")
201+
.roles("USER")
202+
.build()
203+
return InMemoryUserDetailsManager(userDetails)
204+
}
205+
}
206+
169207
@Configuration
170208
@EnableWebSecurity
171209
open class WebauthnConfig {

0 commit comments

Comments
 (0)