Skip to content

Commit 871deba

Browse files
Add support fullyAuthenticated to Kotlin DSL
Closes gh-16162
1 parent d39e329 commit 871deba

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ 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+
* @since 6.5
281+
*/
282+
val fullyAuthenticated: AuthorizationManager<RequestAuthorizationContext> =
283+
AuthenticatedAuthorizationManager.fullyAuthenticated()
284+
278285
internal fun get(): (AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizationManagerRequestMatcherRegistry) -> Unit {
279286
return { requests ->
280287
authorizationRules.forEach { rule ->

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

+63-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ 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.RememberMeAuthenticationToken
31+
import org.springframework.security.authentication.TestAuthentication
3032
import org.springframework.security.authorization.AuthorizationDecision
3133
import org.springframework.security.authorization.AuthorizationManager
3234
import org.springframework.security.config.annotation.web.builders.HttpSecurity
@@ -35,11 +37,11 @@ import org.springframework.security.config.core.GrantedAuthorityDefaults
3537
import org.springframework.security.config.test.SpringTestContext
3638
import org.springframework.security.config.test.SpringTestContextExtension
3739
import org.springframework.security.core.Authentication
40+
import org.springframework.security.core.authority.AuthorityUtils
3841
import org.springframework.security.core.userdetails.User
3942
import org.springframework.security.core.userdetails.UserDetailsService
4043
import org.springframework.security.provisioning.InMemoryUserDetailsManager
41-
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf
42-
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic
44+
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*
4345
import org.springframework.security.web.SecurityFilterChain
4446
import org.springframework.security.web.access.intercept.RequestAuthorizationContext
4547
import org.springframework.security.web.util.matcher.RegexRequestMatcher
@@ -961,4 +963,63 @@ 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(user("user").roles("USER"))
973+
with(csrf())
974+
}.andExpect {
975+
status {
976+
isOk()
977+
}
978+
}
979+
}
980+
981+
@Test
982+
fun `request when fully authenticated configured and remember-me token then responds unauthorized`() {
983+
this.spring.register(FullyAuthenticatedConfig::class.java).autowire()
984+
val rememberMe = RememberMeAuthenticationToken("key", "user",
985+
AuthorityUtils.createAuthorityList("ROLE_USER"))
986+
987+
this.mockMvc.post("/path") {
988+
with(user("user").roles("USER"))
989+
with(csrf())
990+
with(authentication(rememberMe))
991+
}.andExpect {
992+
status {
993+
isUnauthorized()
994+
}
995+
}
996+
}
997+
998+
@Configuration
999+
@EnableWebSecurity
1000+
@EnableWebMvc
1001+
open class FullyAuthenticatedConfig {
1002+
@Bean
1003+
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
1004+
http {
1005+
authorizeHttpRequests {
1006+
authorize("/path", fullyAuthenticated)
1007+
}
1008+
httpBasic { }
1009+
rememberMe { }
1010+
}
1011+
return http.build()
1012+
}
1013+
1014+
@Bean
1015+
open fun userDetailsService(): UserDetailsService = InMemoryUserDetailsManager(TestAuthentication.user())
1016+
1017+
@RestController
1018+
internal class PathController {
1019+
@RequestMapping("/path")
1020+
fun path(): String {
1021+
return "ok"
1022+
}
1023+
}
1024+
}
9641025
}

0 commit comments

Comments
 (0)