Skip to content

Commit 7e0302b

Browse files
Manuel Jordanjzheaux
authored andcommitted
Print ignore message DefaultSecurityFilterChain
When either `web.ignoring().mvcMatchers(...)` or `web.ignoring().antMatchers(...)` methods are used, for all their variations, the DefaultSecurityFilterChain class now indicates correctly through its ouput what paths are ignored according the `ignoring()` settings. Closes gh-9334
1 parent 7554ee8 commit 7e0302b

File tree

9 files changed

+7415
-10
lines changed

9 files changed

+7415
-10
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public abstract class AbstractRequestMatcherRegistry<C> {
5454

5555
private ApplicationContext context;
5656

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

5959
protected final void setApplicationContext(ApplicationContext context) {
6060
this.context = context;
@@ -166,7 +166,8 @@ protected final List<MvcRequestMatcher> createMvcMatchers(HttpMethod method, Str
166166
if (!this.context.containsBean(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME)) {
167167
throw new NoSuchBeanDefinitionException("A Bean named " + HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME
168168
+ " of type " + HandlerMappingIntrospector.class.getName()
169-
+ " is required to use MvcRequestMatcher. Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext.");
169+
+ " is required to use MvcRequestMatcher."
170+
+ " Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext.");
170171
}
171172
HandlerMappingIntrospector introspector = this.context.getBean(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME,
172173
HandlerMappingIntrospector.class);
@@ -266,7 +267,7 @@ public C requestMatchers(RequestMatcher... requestMatchers) {
266267
* @author Rob Winch
267268
* @since 3.2
268269
*/
269-
private static final class RequestMatchers {
270+
public static final class RequestMatchers {
270271

271272
private RequestMatchers() {
272273
}
@@ -279,7 +280,7 @@ private RequestMatchers() {
279280
* from
280281
* @return a {@link List} of {@link AntPathRequestMatcher} instances
281282
*/
282-
static List<RequestMatcher> antMatchers(HttpMethod httpMethod, String... antPatterns) {
283+
public static List<RequestMatcher> antMatchers(HttpMethod httpMethod, String... antPatterns) {
283284
String method = (httpMethod != null) ? httpMethod.toString() : null;
284285
List<RequestMatcher> matchers = new ArrayList<>();
285286
for (String pattern : antPatterns) {
@@ -295,7 +296,7 @@ static List<RequestMatcher> antMatchers(HttpMethod httpMethod, String... antPatt
295296
* from
296297
* @return a {@link List} of {@link AntPathRequestMatcher} instances
297298
*/
298-
static List<RequestMatcher> antMatchers(String... antPatterns) {
299+
public static List<RequestMatcher> antMatchers(String... antPatterns) {
299300
return antMatchers(null, antPatterns);
300301
}
301302

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

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

1919
import java.util.ArrayList;
20+
import java.util.Arrays;
2021
import java.util.List;
2122

2223
import javax.servlet.Filter;
@@ -30,6 +31,7 @@
3031
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3132
import org.springframework.context.ApplicationContext;
3233
import org.springframework.context.ApplicationContextAware;
34+
import org.springframework.core.log.LogMessage;
3335
import org.springframework.http.HttpMethod;
3436
import org.springframework.security.access.PermissionEvaluator;
3537
import org.springframework.security.access.expression.SecurityExpressionHandler;
@@ -60,6 +62,7 @@
6062
import org.springframework.security.web.firewall.HttpFirewall;
6163
import org.springframework.security.web.firewall.RequestRejectedHandler;
6264
import org.springframework.security.web.firewall.StrictHttpFirewall;
65+
import org.springframework.security.web.server.restriction.IgnoreRequestMatcher;
6366
import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
6467
import org.springframework.security.web.util.matcher.RequestMatcher;
6568
import org.springframework.security.web.util.matcher.RequestMatcherEntry;
@@ -108,7 +111,7 @@ public final class WebSecurity extends AbstractConfiguredSecurityBuilder<Filter,
108111

109112
private WebInvocationPrivilegeEvaluator privilegeEvaluator;
110113

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

113116
private SecurityExpressionHandler<FilterInvocation> expressionHandler = this.defaultWebSecurityExpressionHandler;
114117

@@ -420,6 +423,8 @@ public class IgnoredRequestConfigurer extends AbstractRequestMatcherRegistry<Ign
420423
@Override
421424
public MvcMatchersIgnoredRequestConfigurer mvcMatchers(HttpMethod method, String... mvcPatterns) {
422425
List<MvcRequestMatcher> mvcMatchers = createMvcMatchers(method, mvcPatterns);
426+
Arrays.asList(mvcPatterns).stream().forEach((t) -> printWarnSecurityMessage(method, t));
427+
mvcMatchers.stream().forEach((t) -> t.ignore());
423428
WebSecurity.this.ignoredRequests.addAll(mvcMatchers);
424429
return new MvcMatchersIgnoredRequestConfigurer(getApplicationContext(), mvcMatchers);
425430
}
@@ -429,6 +434,38 @@ public MvcMatchersIgnoredRequestConfigurer mvcMatchers(String... mvcPatterns) {
429434
return mvcMatchers(null, mvcPatterns);
430435
}
431436

437+
/**
438+
* @since 5.5
439+
*/
440+
@Override
441+
public IgnoredRequestConfigurer antMatchers(HttpMethod method) {
442+
return antMatchers(method, "/**");
443+
}
444+
445+
/**
446+
* @since 5.5
447+
*/
448+
@Override
449+
public IgnoredRequestConfigurer antMatchers(HttpMethod method, String... antPatterns) {
450+
Assert.state(!this.anyRequestConfigured, "Can't configure antMatchers after anyRequest");
451+
List<RequestMatcher> antMatchers = RequestMatchers.antMatchers(method, antPatterns);
452+
Arrays.asList(antPatterns).stream().forEach((t) -> printWarnSecurityMessage(method, t));
453+
antMatchers.stream().forEach((t) -> ((IgnoreRequestMatcher) t).ignore());
454+
return chainRequestMatchers(antMatchers);
455+
}
456+
457+
/**
458+
* @since 5.5
459+
*/
460+
@Override
461+
public IgnoredRequestConfigurer antMatchers(String... antPatterns) {
462+
Assert.state(!this.anyRequestConfigured, "Can't configure antMatchers after anyRequest");
463+
List<RequestMatcher> antMatchers = RequestMatchers.antMatchers(antPatterns);
464+
Arrays.asList(antPatterns).stream().forEach((t) -> printWarnSecurityMessage(null, t));
465+
antMatchers.stream().forEach((t) -> ((IgnoreRequestMatcher) t).ignore());
466+
return chainRequestMatchers(RequestMatchers.antMatchers(antPatterns));
467+
}
468+
432469
@Override
433470
protected IgnoredRequestConfigurer chainRequestMatchers(List<RequestMatcher> requestMatchers) {
434471
WebSecurity.this.ignoredRequests.addAll(requestMatchers);
@@ -442,6 +479,33 @@ public WebSecurity and() {
442479
return WebSecurity.this;
443480
}
444481

482+
/**
483+
* @param method the HttpMethod, it could be null too.
484+
* @param pathPattern the path pattern to be ignored
485+
* @since 5.5
486+
*/
487+
private void printWarnSecurityMessage(HttpMethod method, String pathPattern) {
488+
if (pathPattern.equals("/**")) {
489+
WebSecurity.this.logger
490+
.warn("**********************************************************************************");
491+
if (method != null) {
492+
WebSecurity.this.logger.warn(LogMessage.format(
493+
"Applying explicit instruction to ignore the '/**' path for the HttpMethod: %s", method));
494+
WebSecurity.this.logger.warn("You're disabling practically all the paths for that HttpMethod");
495+
WebSecurity.this.logger
496+
.warn("Therefore any path for that HttpMethod is completely ignored by Spring Security");
497+
}
498+
else {
499+
WebSecurity.this.logger.warn("Applying explicit instruction to ignore the '/**' path");
500+
WebSecurity.this.logger.warn("You're disabling practically all the paths");
501+
WebSecurity.this.logger.warn("Therefore any path is completely ignored by Spring Security");
502+
}
503+
WebSecurity.this.logger.warn("It is not recomended for production");
504+
WebSecurity.this.logger
505+
.warn("**********************************************************************************");
506+
}
507+
}
508+
445509
}
446510

447511
}

0 commit comments

Comments
 (0)