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