Skip to content

Commit 51fd27f

Browse files
artsiombclozel
artsiom
authored andcommitted
Configure HiddenHttpMethodFilter for Spring WebFlux
Closes gh-14008
1 parent 22bc2bd commit 51fd27f

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfiguration.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.boot.autoconfigure.web.format.WebConversionService;
4242
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4343
import org.springframework.boot.web.codec.CodecCustomizer;
44+
import org.springframework.boot.web.reactive.filter.OrderedHiddenHttpMethodFilter;
4445
import org.springframework.context.annotation.Bean;
4546
import org.springframework.context.annotation.Configuration;
4647
import org.springframework.context.annotation.Import;
@@ -55,6 +56,7 @@
5556
import org.springframework.http.codec.ServerCodecConfigurer;
5657
import org.springframework.util.ClassUtils;
5758
import org.springframework.validation.Validator;
59+
import org.springframework.web.filter.reactive.HiddenHttpMethodFilter;
5860
import org.springframework.web.reactive.config.DelegatingWebFluxConfiguration;
5961
import org.springframework.web.reactive.config.EnableWebFlux;
6062
import org.springframework.web.reactive.config.ResourceChainRegistration;
@@ -80,6 +82,7 @@
8082
* @author Andy Wilkinson
8183
* @author Phillip Webb
8284
* @author Eddú Meléndez
85+
* @author Artsiom Yudovin
8386
* @since 2.0.0
8487
*/
8588
@Configuration
@@ -91,6 +94,12 @@
9194
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
9295
public class WebFluxAutoConfiguration {
9396

97+
@Bean
98+
@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
99+
public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
100+
return new OrderedHiddenHttpMethodFilter();
101+
}
102+
94103
@Configuration
95104
@EnableConfigurationProperties({ ResourceProperties.class, WebFluxProperties.class })
96105
@Import({ EnableWebFluxConfiguration.class })

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.boot.autoconfigure.validation.ValidatorAdapter;
3131
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
3232
import org.springframework.boot.web.codec.CodecCustomizer;
33+
import org.springframework.boot.web.reactive.filter.OrderedHiddenHttpMethodFilter;
3334
import org.springframework.context.annotation.Bean;
3435
import org.springframework.context.annotation.Configuration;
3536
import org.springframework.core.Ordered;
@@ -41,6 +42,7 @@
4142
import org.springframework.test.util.ReflectionTestUtils;
4243
import org.springframework.validation.Validator;
4344
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
45+
import org.springframework.web.filter.reactive.HiddenHttpMethodFilter;
4446
import org.springframework.web.reactive.HandlerMapping;
4547
import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
4648
import org.springframework.web.reactive.config.WebFluxConfigurationSupport;
@@ -66,6 +68,7 @@
6668
*
6769
* @author Brian Clozel
6870
* @author Andy Wilkinson
71+
* @author Artsiom Yudovin
6972
*/
7073
public class WebFluxAutoConfigurationTests {
7174

@@ -351,6 +354,22 @@ public void validatorWithCustomJsr303ValidatorExposedAsSpringValidator() {
351354
});
352355
}
353356

357+
@Test
358+
public void hiddenHttpMethodFilterIsAutoConfigured() {
359+
this.contextRunner.run((context) -> assertThat(context)
360+
.hasSingleBean(OrderedHiddenHttpMethodFilter.class));
361+
}
362+
363+
@Test
364+
public void hiddenHttpMethodFilterCanBeOverridden() {
365+
this.contextRunner.withUserConfiguration(CustomHiddenHttpMethodFilter.class)
366+
.run((context) -> {
367+
assertThat(context)
368+
.doesNotHaveBean(OrderedHiddenHttpMethodFilter.class);
369+
assertThat(context).hasSingleBean(HiddenHttpMethodFilter.class);
370+
});
371+
}
372+
354373
@Configuration
355374
protected static class CustomArgumentResolvers {
356375

@@ -456,4 +475,14 @@ public Validator customValidator() {
456475

457476
}
458477

478+
@Configuration
479+
static class CustomHiddenHttpMethodFilter {
480+
481+
@Bean
482+
public HiddenHttpMethodFilter customHiddenHttpMethodFilter() {
483+
return mock(HiddenHttpMethodFilter.class);
484+
}
485+
486+
}
487+
459488
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.web.reactive.filter;
18+
19+
import org.springframework.boot.web.servlet.FilterRegistrationBean;
20+
import org.springframework.core.Ordered;
21+
import org.springframework.web.filter.reactive.HiddenHttpMethodFilter;
22+
23+
/**
24+
* {@link HiddenHttpMethodFilter} that also implements {@link Ordered}.
25+
*
26+
* @author Artsiom Yudovin
27+
* @since 2.0.5
28+
*/
29+
public class OrderedHiddenHttpMethodFilter extends HiddenHttpMethodFilter
30+
implements Ordered {
31+
32+
/**
33+
* The default order is high to ensure the filter is applied before Spring Security.
34+
*/
35+
public static final int DEFAULT_ORDER = FilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER
36+
- 10000;
37+
38+
private int order = DEFAULT_ORDER;
39+
40+
@Override
41+
public int getOrder() {
42+
return this.order;
43+
}
44+
45+
/**
46+
* Set the order for this filter.
47+
* @param order the order to set
48+
*/
49+
public void setOrder(int order) {
50+
this.order = order;
51+
}
52+
53+
}

0 commit comments

Comments
 (0)