Skip to content

Commit 0386fbe

Browse files
committed
Don't use the 'ignoring()' method in CloudFoundry security
Update `IgnoredCloudFoundryPathsWebSecurityConfiguration` to use a `SecurityFilterChain` and `permit...` methods rather than `ignoring()` which is no longer recommended. Fixes gh-32622
1 parent da50120 commit 0386fbe

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryActuatorAutoConfiguration.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import org.springframework.boot.autoconfigure.condition.ConditionalOnCloudPlatform;
4949
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
5050
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
51-
import org.springframework.boot.autoconfigure.security.SecurityProperties;
5251
import org.springframework.boot.cloud.CloudPlatform;
5352
import org.springframework.boot.info.GitProperties;
5453
import org.springframework.boot.web.client.RestTemplateBuilder;
@@ -60,8 +59,10 @@
6059
import org.springframework.http.HttpHeaders;
6160
import org.springframework.http.HttpMethod;
6261
import org.springframework.security.config.annotation.web.WebSecurityConfigurer;
62+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
6363
import org.springframework.security.config.annotation.web.builders.WebSecurity;
6464
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
65+
import org.springframework.security.web.SecurityFilterChain;
6566
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
6667
import org.springframework.security.web.util.matcher.OrRequestMatcher;
6768
import org.springframework.security.web.util.matcher.RequestMatcher;
@@ -158,38 +159,33 @@ private CorsConfiguration getCorsConfiguration() {
158159
}
159160

160161
/**
161-
* {@link WebSecurityConfigurer} to tell Spring Security to ignore cloudfoundry
162+
* {@link WebSecurityConfigurer} to tell Spring Security to permit cloudfoundry
162163
* specific paths. The Cloud foundry endpoints are protected by their own security
163164
* interceptor.
164165
*/
165166
@ConditionalOnClass({ WebSecurityCustomizer.class, WebSecurity.class })
166167
@Configuration(proxyBeanMethods = false)
167168
public static class IgnoredCloudFoundryPathsWebSecurityConfiguration {
168169

169-
@Bean
170-
IgnoredCloudFoundryPathsWebSecurityCustomizer ignoreCloudFoundryPathsWebSecurityCustomizer(
171-
CloudFoundryWebEndpointServletHandlerMapping handlerMapping) {
172-
return new IgnoredCloudFoundryPathsWebSecurityCustomizer(handlerMapping);
173-
}
174-
175-
}
170+
private static final int FILTER_CHAIN_ORDER = -1;
176171

177-
@Order(SecurityProperties.IGNORED_ORDER)
178-
static class IgnoredCloudFoundryPathsWebSecurityCustomizer implements WebSecurityCustomizer {
179-
180-
private final PathMappedEndpoints pathMappedEndpoints;
181-
182-
IgnoredCloudFoundryPathsWebSecurityCustomizer(CloudFoundryWebEndpointServletHandlerMapping handlerMapping) {
183-
this.pathMappedEndpoints = new PathMappedEndpoints(BASE_PATH, handlerMapping::getAllEndpoints);
172+
@Bean
173+
@Order(FILTER_CHAIN_ORDER)
174+
SecurityFilterChain cloudFoundrySecurityFilterChain(HttpSecurity http,
175+
CloudFoundryWebEndpointServletHandlerMapping handlerMapping) throws Exception {
176+
RequestMatcher cloudFoundryRequest = getRequestMatcher(handlerMapping);
177+
http.securityMatchers((matches) -> matches.requestMatchers(cloudFoundryRequest))
178+
.authorizeHttpRequests((authorize) -> authorize.anyRequest().permitAll());
179+
return http.build();
184180
}
185181

186-
@Override
187-
public void customize(WebSecurity web) {
182+
private RequestMatcher getRequestMatcher(CloudFoundryWebEndpointServletHandlerMapping handlerMapping) {
183+
PathMappedEndpoints endpoints = new PathMappedEndpoints(BASE_PATH, handlerMapping::getAllEndpoints);
188184
List<RequestMatcher> matchers = new ArrayList<>();
189-
this.pathMappedEndpoints.getAllPaths().forEach((path) -> matchers.add(pathMatcher(path + "/**")));
185+
endpoints.getAllPaths().forEach((path) -> matchers.add(pathMatcher(path + "/**")));
190186
matchers.add(pathMatcher(BASE_PATH));
191187
matchers.add(pathMatcher(BASE_PATH + "/"));
192-
web.ignoring().requestMatchers(new OrRequestMatcher(matchers));
188+
return new OrRequestMatcher(matchers);
193189
}
194190

195191
private PathPatternRequestMatcher pathMatcher(String path) {

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryActuatorAutoConfigurationTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ void cloudFoundryPathsIgnoredBySpringSecurity() {
175175
.withPropertyValues("VCAP_APPLICATION:---", "vcap.application.application_id:my-app-id")
176176
.run((context) -> {
177177
SecurityFilterChain chain = getSecurityFilterChain(context);
178-
assertThat(chain.getFilters()).isEmpty();
179178
MockHttpServletRequest request = new MockHttpServletRequest();
180179
testCloudFoundrySecurity(request, BASE_PATH, chain);
181180
testCloudFoundrySecurity(request, BASE_PATH + "/", chain);

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/SecurityProperties.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ public class SecurityProperties {
5050
/**
5151
* Order applied to the {@code WebSecurityCustomizer} that ignores standard static
5252
* resource paths.
53+
* @deprecated since 3.5.0 for removal in 4.0.0 since Spring Security no longer
54+
* recommends using the {@code .ignoring()} method
5355
*/
56+
@Deprecated(since = "3.5.0", forRemoval = true)
5457
public static final int IGNORED_ORDER = Ordered.HIGHEST_PRECEDENCE;
5558

5659
/**

0 commit comments

Comments
 (0)