@@ -27,6 +27,8 @@ import org.springframework.context.annotation.Configuration
27
27
import org.springframework.http.HttpMethod
28
28
import org.springframework.security.access.hierarchicalroles.RoleHierarchy
29
29
import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl
30
+ import org.springframework.security.authentication.RememberMeAuthenticationToken
31
+ import org.springframework.security.authentication.TestAuthentication
30
32
import org.springframework.security.authorization.AuthorizationDecision
31
33
import org.springframework.security.authorization.AuthorizationManager
32
34
import org.springframework.security.config.annotation.web.builders.HttpSecurity
@@ -35,11 +37,12 @@ import org.springframework.security.config.core.GrantedAuthorityDefaults
35
37
import org.springframework.security.config.test.SpringTestContext
36
38
import org.springframework.security.config.test.SpringTestContextExtension
37
39
import org.springframework.security.core.Authentication
40
+ import org.springframework.security.core.authority.AuthorityUtils
38
41
import org.springframework.security.core.userdetails.User
39
42
import org.springframework.security.core.userdetails.UserDetailsService
40
43
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.*
43
46
import org.springframework.security.web.SecurityFilterChain
44
47
import org.springframework.security.web.access.intercept.RequestAuthorizationContext
45
48
import org.springframework.security.web.util.matcher.RegexRequestMatcher
@@ -961,4 +964,63 @@ class AuthorizeHttpRequestsDslTests {
961
964
}
962
965
963
966
}
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
+ }
964
1026
}
0 commit comments