Skip to content

Commit 456f7f5

Browse files
franticticktickMax Batischev
authored andcommitted
Add support fullyAuthenticated to Kotlin DSL
Closes gh-16162
1 parent ff7dbb4 commit 456f7f5

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 64 additions & 2 deletions
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,12 @@ 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
45+
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*
4346
import org.springframework.security.web.SecurityFilterChain
4447
import org.springframework.security.web.access.intercept.RequestAuthorizationContext
4548
import org.springframework.security.web.util.matcher.RegexRequestMatcher
@@ -961,4 +964,63 @@ class AuthorizeHttpRequestsDslTests {
961964
}
962965

963966
}
967+
968+
@Test
969+
fun `request when fully authenticated configured then responds ok`() {
970+
this.spring.register(FullyAuthenticatedConfig::class.java).autowire()
971+
972+
this.mockMvc.post("/path") {
973+
with(SecurityMockMvcRequestPostProcessors.user("user").roles("USER"))
974+
with(csrf())
975+
}.andExpect {
976+
status {
977+
isOk()
978+
}
979+
}
980+
}
981+
982+
@Test
983+
fun `request when fully authenticated configured and remember-me token then responds unauthorized`() {
984+
this.spring.register(FullyAuthenticatedConfig::class.java).autowire()
985+
val rememberMe = RememberMeAuthenticationToken("key", "user",
986+
AuthorityUtils.createAuthorityList("ROLE_USER"))
987+
988+
this.mockMvc.post("/path") {
989+
with(SecurityMockMvcRequestPostProcessors.user("user").roles("USER"))
990+
with(csrf())
991+
with(authentication(rememberMe))
992+
}.andExpect {
993+
status {
994+
isUnauthorized()
995+
}
996+
}
997+
}
998+
999+
@Configuration
1000+
@EnableWebSecurity
1001+
@EnableWebMvc
1002+
open class FullyAuthenticatedConfig {
1003+
@Bean
1004+
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
1005+
http {
1006+
authorizeHttpRequests {
1007+
authorize("/path", fullyAuthenticated)
1008+
}
1009+
httpBasic { }
1010+
rememberMe { }
1011+
}
1012+
return http.build()
1013+
}
1014+
1015+
@Bean
1016+
open fun userDetailsService(): UserDetailsService = InMemoryUserDetailsManager(TestAuthentication.user())
1017+
1018+
@RestController
1019+
internal class PathController {
1020+
@RequestMapping("/path")
1021+
fun path(): String {
1022+
return "ok"
1023+
}
1024+
}
1025+
}
9641026
}

0 commit comments

Comments
 (0)