@@ -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,11 @@ 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.*
43
45
import org.springframework.security.web.SecurityFilterChain
44
46
import org.springframework.security.web.access.intercept.RequestAuthorizationContext
45
47
import org.springframework.security.web.util.matcher.RegexRequestMatcher
@@ -961,4 +963,61 @@ class AuthorizeHttpRequestsDslTests {
961
963
}
962
964
963
965
}
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
+ }
964
1023
}
0 commit comments