@@ -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,12 @@ 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
45+ import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*
4346import org.springframework.security.web.SecurityFilterChain
4447import org.springframework.security.web.access.intercept.RequestAuthorizationContext
4548import 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