@@ -27,6 +27,8 @@ import org.springframework.context.annotation.Configuration
2727import org.springframework.http.HttpMethod
2828import org.springframework.security.access.hierarchicalroles.RoleHierarchy
2929import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl
30+ import org.springframework.security.authentication.RememberMeAuthenticationToken
31+ import org.springframework.security.authentication.TestAuthentication
3032import org.springframework.security.authorization.AuthorizationDecision
3133import org.springframework.security.authorization.AuthorizationManager
3234import org.springframework.security.config.annotation.web.builders.HttpSecurity
@@ -35,11 +37,11 @@ import org.springframework.security.config.core.GrantedAuthorityDefaults
3537import org.springframework.security.config.test.SpringTestContext
3638import org.springframework.security.config.test.SpringTestContextExtension
3739import org.springframework.security.core.Authentication
40+ import org.springframework.security.core.authority.AuthorityUtils
3841import org.springframework.security.core.userdetails.User
3942import org.springframework.security.core.userdetails.UserDetailsService
4043import 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.*
4345import org.springframework.security.web.SecurityFilterChain
4446import org.springframework.security.web.access.intercept.RequestAuthorizationContext
4547import 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