@@ -20,17 +20,21 @@ import org.junit.Rule
20
20
import org.junit.Test
21
21
import org.springframework.beans.factory.annotation.Autowired
22
22
import org.springframework.context.annotation.Bean
23
+ import org.springframework.http.HttpMethod
23
24
import org.springframework.security.config.annotation.web.builders.HttpSecurity
24
25
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
25
26
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
26
27
import org.springframework.security.config.test.SpringTestRule
27
28
import org.springframework.security.core.userdetails.User
28
29
import org.springframework.security.core.userdetails.UserDetailsService
29
30
import org.springframework.security.provisioning.InMemoryUserDetailsManager
31
+ import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf
30
32
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic
31
33
import org.springframework.security.web.util.matcher.RegexRequestMatcher
32
34
import org.springframework.test.web.servlet.MockMvc
33
35
import org.springframework.test.web.servlet.get
36
+ import org.springframework.test.web.servlet.post
37
+ import org.springframework.test.web.servlet.put
34
38
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
35
39
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
36
40
import org.springframework.web.bind.annotation.GetMapping
@@ -72,12 +76,29 @@ class AuthorizeRequestsDslTests {
72
76
}
73
77
}
74
78
79
+ @Test
80
+ fun `request when allowed by regex matcher with http method then responds based on method` () {
81
+ this .spring.register(AuthorizeRequestsByRegexConfig ::class .java).autowire()
82
+
83
+ this .mockMvc.post(" /onlyPostPermitted" ) { with (csrf()) }
84
+ .andExpect {
85
+ status { isOk }
86
+ }
87
+
88
+ this .mockMvc.get(" /onlyPostPermitted" )
89
+ .andExpect {
90
+ status { isForbidden }
91
+ }
92
+ }
93
+
75
94
@EnableWebSecurity
76
95
open class AuthorizeRequestsByRegexConfig : WebSecurityConfigurerAdapter () {
77
96
override fun configure (http : HttpSecurity ) {
78
97
http {
79
98
authorizeRequests {
80
99
authorize(RegexRequestMatcher (" /path" , null ), permitAll)
100
+ authorize(RegexRequestMatcher (" /onlyPostPermitted" , " POST" ), permitAll)
101
+ authorize(RegexRequestMatcher (" /onlyPostPermitted" , " GET" ), denyAll)
81
102
authorize(RegexRequestMatcher (" .*" , null ), authenticated)
82
103
}
83
104
}
@@ -88,6 +109,10 @@ class AuthorizeRequestsDslTests {
88
109
@RequestMapping(" /path" )
89
110
fun path () {
90
111
}
112
+
113
+ @RequestMapping(" /onlyPostPermitted" )
114
+ fun onlyPostPermitted () {
115
+ }
91
116
}
92
117
}
93
118
@@ -271,4 +296,91 @@ class AuthorizeRequestsDslTests {
271
296
}
272
297
}
273
298
}
299
+
300
+ @EnableWebSecurity
301
+ @EnableWebMvc
302
+ open class AuthorizeRequestsByMvcConfigWithHttpMethod : WebSecurityConfigurerAdapter () {
303
+ override fun configure (http : HttpSecurity ) {
304
+ http {
305
+ authorizeRequests {
306
+ authorize(HttpMethod .GET , " /path" , permitAll)
307
+ authorize(HttpMethod .PUT , " /path" , denyAll)
308
+ }
309
+ }
310
+ }
311
+
312
+ @RestController
313
+ internal class PathController {
314
+ @RequestMapping(" /path" )
315
+ fun path () {
316
+ }
317
+ }
318
+ }
319
+
320
+ @Test
321
+ fun `request when secured by mvc with http method then responds based on http method` () {
322
+ this .spring.register(AuthorizeRequestsByMvcConfigWithHttpMethod ::class .java).autowire()
323
+
324
+ this .mockMvc.get(" /path" )
325
+ .andExpect {
326
+ status { isOk }
327
+ }
328
+
329
+ this .mockMvc.put(" /path" ) { with (csrf()) }
330
+ .andExpect {
331
+ status { isForbidden }
332
+ }
333
+ }
334
+
335
+ @EnableWebSecurity
336
+ @EnableWebMvc
337
+ open class MvcMatcherServletPathHttpMethodConfig : WebSecurityConfigurerAdapter () {
338
+ override fun configure (http : HttpSecurity ) {
339
+ http {
340
+ authorizeRequests {
341
+ authorize(HttpMethod .GET , " /path" , " /spring" , denyAll)
342
+ authorize(HttpMethod .PUT , " /path" , " /spring" , denyAll)
343
+ }
344
+ }
345
+ }
346
+
347
+ @RestController
348
+ internal class PathController {
349
+ @RequestMapping(" /path" )
350
+ fun path () {
351
+ }
352
+ }
353
+ }
354
+
355
+
356
+
357
+ @Test
358
+ fun `request when secured by mvc with servlet path and http method then responds based on path and method` () {
359
+ this .spring.register(MvcMatcherServletPathConfig ::class .java).autowire()
360
+
361
+ this .mockMvc.perform(MockMvcRequestBuilders .get(" /spring/path" )
362
+ .with { request ->
363
+ request.apply {
364
+ servletPath = " /spring"
365
+ }
366
+ })
367
+ .andExpect(status().isForbidden)
368
+
369
+ this .mockMvc.perform(MockMvcRequestBuilders .put(" /spring/path" )
370
+ .with { request ->
371
+ request.apply {
372
+ servletPath = " /spring"
373
+ csrf()
374
+ }
375
+ })
376
+ .andExpect(status().isForbidden)
377
+
378
+ this .mockMvc.perform(MockMvcRequestBuilders .get(" /other/path" )
379
+ .with { request ->
380
+ request.apply {
381
+ servletPath = " /other"
382
+ }
383
+ })
384
+ .andExpect(status().isOk)
385
+ }
274
386
}
0 commit comments