Skip to content

Commit 70e67e5

Browse files
Add support fullyAuthenticated to Kotlin DSL
Closes gh-16162
1 parent ff7dbb4 commit 70e67e5

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,12 @@ class AuthorizeHttpRequestsDsl : AbstractRequestMatcherDsl {
275275
val authenticated: AuthorizationManager<RequestAuthorizationContext> =
276276
AuthenticatedAuthorizationManager.authenticated()
277277

278+
/**
279+
* Specify that URLs are allowed by users who have authenticated and were not "remembered".
280+
*/
281+
val fullyAuthenticated: AuthorizationManager<RequestAuthorizationContext> =
282+
AuthenticatedAuthorizationManager.fullyAuthenticated()
283+
278284
internal fun get(): (AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizationManagerRequestMatcherRegistry) -> Unit {
279285
return { requests ->
280286
authorizationRules.forEach { rule ->

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import org.springframework.context.annotation.Configuration
2727
import org.springframework.http.HttpMethod
2828
import org.springframework.security.access.hierarchicalroles.RoleHierarchy
2929
import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl
30+
import org.springframework.security.authentication.TestAuthentication
3031
import org.springframework.security.authorization.AuthorizationDecision
3132
import org.springframework.security.authorization.AuthorizationManager
3233
import org.springframework.security.config.annotation.web.builders.HttpSecurity
@@ -38,6 +39,7 @@ import org.springframework.security.core.Authentication
3839
import org.springframework.security.core.userdetails.User
3940
import org.springframework.security.core.userdetails.UserDetailsService
4041
import org.springframework.security.provisioning.InMemoryUserDetailsManager
42+
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors
4143
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf
4244
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic
4345
import org.springframework.security.web.SecurityFilterChain
@@ -961,4 +963,45 @@ class AuthorizeHttpRequestsDslTests {
961963
}
962964

963965
}
966+
967+
@Test
968+
fun `request when fully authenticated configured then responds ok`() {
969+
this.spring.register(FullyAuthenticatedConfig::class.java).autowire()
970+
971+
this.mockMvc.post("/path") {
972+
with(SecurityMockMvcRequestPostProcessors.user("user").roles("USER"))
973+
with(csrf())
974+
}
975+
.andExpect {
976+
status { isOk() }
977+
}
978+
}
979+
980+
@Configuration
981+
@EnableWebSecurity
982+
@EnableWebMvc
983+
open class FullyAuthenticatedConfig {
984+
@Bean
985+
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
986+
http {
987+
authorizeHttpRequests {
988+
authorize("/path", fullyAuthenticated)
989+
}
990+
httpBasic { }
991+
rememberMe { }
992+
}
993+
return http.build()
994+
}
995+
996+
@Bean
997+
open fun userDetailsService(): UserDetailsService = InMemoryUserDetailsManager(TestAuthentication.user())
998+
999+
@RestController
1000+
internal class PathController {
1001+
@RequestMapping("/path")
1002+
fun path(): String {
1003+
return "ok"
1004+
}
1005+
}
1006+
}
9641007
}

0 commit comments

Comments
 (0)