Skip to content

Commit 8461654

Browse files
committed
Polish ignoring() log messaging
- Public API remains unchanged Issue gh-9334
1 parent 6ae651b commit 8461654

File tree

9 files changed

+13
-7410
lines changed

9 files changed

+13
-7410
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/AbstractRequestMatcherRegistry.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,7 +54,7 @@ public abstract class AbstractRequestMatcherRegistry<C> {
5454

5555
private ApplicationContext context;
5656

57-
protected boolean anyRequestConfigured = false;
57+
private boolean anyRequestConfigured = false;
5858

5959
protected final void setApplicationContext(ApplicationContext context) {
6060
this.context = context;
@@ -165,8 +165,7 @@ protected final List<MvcRequestMatcher> createMvcMatchers(HttpMethod method, Str
165165
if (!this.context.containsBean(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME)) {
166166
throw new NoSuchBeanDefinitionException("A Bean named " + HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME
167167
+ " of type " + HandlerMappingIntrospector.class.getName()
168-
+ " is required to use MvcRequestMatcher."
169-
+ " Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext.");
168+
+ " is required to use MvcRequestMatcher. Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext.");
170169
}
171170
HandlerMappingIntrospector introspector = this.context.getBean(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME,
172171
HandlerMappingIntrospector.class);
@@ -266,7 +265,7 @@ public C requestMatchers(RequestMatcher... requestMatchers) {
266265
* @author Rob Winch
267266
* @since 3.2
268267
*/
269-
public static final class RequestMatchers {
268+
private static final class RequestMatchers {
270269

271270
private RequestMatchers() {
272271
}
@@ -279,7 +278,7 @@ private RequestMatchers() {
279278
* from
280279
* @return a {@link List} of {@link AntPathRequestMatcher} instances
281280
*/
282-
public static List<RequestMatcher> antMatchers(HttpMethod httpMethod, String... antPatterns) {
281+
static List<RequestMatcher> antMatchers(HttpMethod httpMethod, String... antPatterns) {
283282
String method = (httpMethod != null) ? httpMethod.toString() : null;
284283
List<RequestMatcher> matchers = new ArrayList<>();
285284
for (String pattern : antPatterns) {
@@ -295,7 +294,7 @@ public static List<RequestMatcher> antMatchers(HttpMethod httpMethod, String...
295294
* from
296295
* @return a {@link List} of {@link AntPathRequestMatcher} instances
297296
*/
298-
public static List<RequestMatcher> antMatchers(String... antPatterns) {
297+
static List<RequestMatcher> antMatchers(String... antPatterns) {
299298
return antMatchers(null, antPatterns);
300299
}
301300

config/src/main/java/org/springframework/security/config/annotation/web/builders/WebSecurity.java

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.security.config.annotation.web.builders;
1818

1919
import java.util.ArrayList;
20-
import java.util.Arrays;
2120
import java.util.List;
2221

2322
import jakarta.servlet.Filter;
@@ -31,7 +30,6 @@
3130
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3231
import org.springframework.context.ApplicationContext;
3332
import org.springframework.context.ApplicationContextAware;
34-
import org.springframework.core.log.LogMessage;
3533
import org.springframework.http.HttpMethod;
3634
import org.springframework.security.access.PermissionEvaluator;
3735
import org.springframework.security.access.expression.SecurityExpressionHandler;
@@ -62,7 +60,6 @@
6260
import org.springframework.security.web.firewall.HttpFirewall;
6361
import org.springframework.security.web.firewall.RequestRejectedHandler;
6462
import org.springframework.security.web.firewall.StrictHttpFirewall;
65-
import org.springframework.security.web.server.restriction.IgnoreRequestMatcher;
6663
import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
6764
import org.springframework.security.web.util.matcher.RequestMatcher;
6865
import org.springframework.security.web.util.matcher.RequestMatcherEntry;
@@ -111,7 +108,7 @@ public final class WebSecurity extends AbstractConfiguredSecurityBuilder<Filter,
111108

112109
private WebInvocationPrivilegeEvaluator privilegeEvaluator;
113110

114-
private final DefaultWebSecurityExpressionHandler defaultWebSecurityExpressionHandler = new DefaultWebSecurityExpressionHandler();
111+
private DefaultWebSecurityExpressionHandler defaultWebSecurityExpressionHandler = new DefaultWebSecurityExpressionHandler();
115112

116113
private SecurityExpressionHandler<FilterInvocation> expressionHandler = this.defaultWebSecurityExpressionHandler;
117114

@@ -307,6 +304,8 @@ protected Filter performBuild() throws Exception {
307304
List<SecurityFilterChain> securityFilterChains = new ArrayList<>(chainSize);
308305
List<RequestMatcherEntry<List<WebInvocationPrivilegeEvaluator>>> requestMatcherPrivilegeEvaluatorsEntries = new ArrayList<>();
309306
for (RequestMatcher ignoredRequest : this.ignoredRequests) {
307+
WebSecurity.this.logger.warn("You are asking Spring Security to ignore " + ignoredRequest
308+
+ ". This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead.");
310309
SecurityFilterChain securityFilterChain = new DefaultSecurityFilterChain(ignoredRequest);
311310
securityFilterChains.add(securityFilterChain);
312311
requestMatcherPrivilegeEvaluatorsEntries
@@ -436,8 +435,6 @@ public class IgnoredRequestConfigurer extends AbstractRequestMatcherRegistry<Ign
436435
@Override
437436
public MvcMatchersIgnoredRequestConfigurer mvcMatchers(HttpMethod method, String... mvcPatterns) {
438437
List<MvcRequestMatcher> mvcMatchers = createMvcMatchers(method, mvcPatterns);
439-
Arrays.asList(mvcPatterns).stream().forEach((t) -> printWarnSecurityMessage(method, t));
440-
mvcMatchers.stream().forEach((t) -> t.ignore());
441438
WebSecurity.this.ignoredRequests.addAll(mvcMatchers);
442439
return new MvcMatchersIgnoredRequestConfigurer(getApplicationContext(), mvcMatchers);
443440
}
@@ -447,38 +444,6 @@ public MvcMatchersIgnoredRequestConfigurer mvcMatchers(String... mvcPatterns) {
447444
return mvcMatchers(null, mvcPatterns);
448445
}
449446

450-
/**
451-
* @since 5.5
452-
*/
453-
@Override
454-
public IgnoredRequestConfigurer antMatchers(HttpMethod method) {
455-
return antMatchers(method, "/**");
456-
}
457-
458-
/**
459-
* @since 5.5
460-
*/
461-
@Override
462-
public IgnoredRequestConfigurer antMatchers(HttpMethod method, String... antPatterns) {
463-
Assert.state(!this.anyRequestConfigured, "Can't configure antMatchers after anyRequest");
464-
List<RequestMatcher> antMatchers = RequestMatchers.antMatchers(method, antPatterns);
465-
Arrays.asList(antPatterns).stream().forEach((t) -> printWarnSecurityMessage(method, t));
466-
antMatchers.stream().forEach((t) -> ((IgnoreRequestMatcher) t).ignore());
467-
return chainRequestMatchers(antMatchers);
468-
}
469-
470-
/**
471-
* @since 5.5
472-
*/
473-
@Override
474-
public IgnoredRequestConfigurer antMatchers(String... antPatterns) {
475-
Assert.state(!this.anyRequestConfigured, "Can't configure antMatchers after anyRequest");
476-
List<RequestMatcher> antMatchers = RequestMatchers.antMatchers(antPatterns);
477-
Arrays.asList(antPatterns).stream().forEach((t) -> printWarnSecurityMessage(null, t));
478-
antMatchers.stream().forEach((t) -> ((IgnoreRequestMatcher) t).ignore());
479-
return chainRequestMatchers(RequestMatchers.antMatchers(antPatterns));
480-
}
481-
482447
@Override
483448
protected IgnoredRequestConfigurer chainRequestMatchers(List<RequestMatcher> requestMatchers) {
484449
WebSecurity.this.ignoredRequests.addAll(requestMatchers);
@@ -492,33 +457,6 @@ public WebSecurity and() {
492457
return WebSecurity.this;
493458
}
494459

495-
/**
496-
* @param method the HttpMethod, it could be null too.
497-
* @param pathPattern the path pattern to be ignored
498-
* @since 5.5
499-
*/
500-
private void printWarnSecurityMessage(HttpMethod method, String pathPattern) {
501-
if (pathPattern.equals("/**")) {
502-
WebSecurity.this.logger
503-
.warn("**********************************************************************************");
504-
if (method != null) {
505-
WebSecurity.this.logger.warn(LogMessage.format(
506-
"Applying explicit instruction to ignore the '/**' path for the HttpMethod: %s", method));
507-
WebSecurity.this.logger.warn("You're disabling practically all the paths for that HttpMethod");
508-
WebSecurity.this.logger
509-
.warn("Therefore any path for that HttpMethod is completely ignored by Spring Security");
510-
}
511-
else {
512-
WebSecurity.this.logger.warn("Applying explicit instruction to ignore the '/**' path");
513-
WebSecurity.this.logger.warn("You're disabling practically all the paths");
514-
WebSecurity.this.logger.warn("Therefore any path is completely ignored by Spring Security");
515-
}
516-
WebSecurity.this.logger.warn("It is not recomended for production");
517-
WebSecurity.this.logger
518-
.warn("**********************************************************************************");
519-
}
520-
}
521-
522460
}
523461

524462
}

0 commit comments

Comments
 (0)