|
16 | 16 |
|
17 | 17 | package org.springframework.security.config.annotation.web.builders;
|
18 | 18 |
|
| 19 | +import jakarta.servlet.ServletException; |
19 | 20 | import jakarta.servlet.http.HttpServletResponse;
|
20 | 21 |
|
21 | 22 | import org.junit.jupiter.api.AfterEach;
|
|
24 | 25 |
|
25 | 26 | import org.springframework.beans.factory.annotation.Autowired;
|
26 | 27 | import org.springframework.context.annotation.Configuration;
|
| 28 | +import org.springframework.http.HttpStatus; |
27 | 29 | import org.springframework.mock.web.MockFilterChain;
|
28 | 30 | import org.springframework.mock.web.MockHttpServletRequest;
|
29 | 31 | import org.springframework.mock.web.MockHttpServletResponse;
|
|
32 | 34 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
33 | 35 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
34 | 36 | import org.springframework.security.web.FilterChainProxy;
|
| 37 | +import org.springframework.security.web.firewall.HttpStatusRequestRejectedHandler; |
35 | 38 | import org.springframework.web.bind.annotation.RequestMapping;
|
36 | 39 | import org.springframework.web.bind.annotation.RestController;
|
37 | 40 | import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
38 | 41 | import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
39 | 42 | import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
|
40 | 43 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
41 | 44 |
|
| 45 | +import java.io.IOException; |
| 46 | + |
42 | 47 | import static org.assertj.core.api.Assertions.assertThat;
|
43 | 48 |
|
44 | 49 | /**
|
@@ -92,6 +97,15 @@ public void ignoringMvcMatcher() throws Exception {
|
92 | 97 | assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
|
93 | 98 | }
|
94 | 99 |
|
| 100 | + @Test |
| 101 | + public void requestRejectedHandlerInvoked() throws ServletException, IOException { |
| 102 | + loadConfig(RequestRejectedHandlerConfig.class); |
| 103 | + this.request.setServletPath("/spring"); |
| 104 | + this.request.setRequestURI("/spring/\u0019path"); |
| 105 | + this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain); |
| 106 | + assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_BAD_REQUEST); |
| 107 | + } |
| 108 | + |
95 | 109 | @Test
|
96 | 110 | public void ignoringMvcMatcherServletPath() throws Exception {
|
97 | 111 | loadConfig(MvcMatcherServletPathConfig.class, LegacyMvcMatchingConfig.class);
|
@@ -223,4 +237,14 @@ public void configurePathMatch(PathMatchConfigurer configurer) {
|
223 | 237 |
|
224 | 238 | }
|
225 | 239 |
|
| 240 | + @EnableWebSecurity |
| 241 | + static class RequestRejectedHandlerConfig extends WebSecurityConfigurerAdapter { |
| 242 | + |
| 243 | + @Override |
| 244 | + public void configure(WebSecurity web) throws Exception { |
| 245 | + web.requestRejectedHandler(new HttpStatusRequestRejectedHandler(HttpStatus.BAD_REQUEST.value())); |
| 246 | + } |
| 247 | + |
| 248 | + } |
| 249 | + |
226 | 250 | }
|
0 commit comments