Skip to content

Commit a09f6e1

Browse files
committed
Polish ignoring() log messaging
- Public API remains unchanged Issue gh-9334
1 parent 7e0302b commit a09f6e1

File tree

9 files changed

+12
-7409
lines changed

9 files changed

+12
-7409
lines changed

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

Lines changed: 5 additions & 6 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-
protected boolean anyRequestConfigured = false;
57+
private boolean anyRequestConfigured = false;
5858

5959
protected final void setApplicationContext(ApplicationContext context) {
6060
this.context = context;
@@ -166,8 +166,7 @@ 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."
170-
+ " Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext.");
169+
+ " is required to use MvcRequestMatcher. Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext.");
171170
}
172171
HandlerMappingIntrospector introspector = this.context.getBean(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME,
173172
HandlerMappingIntrospector.class);
@@ -267,7 +266,7 @@ public C requestMatchers(RequestMatcher... requestMatchers) {
267266
* @author Rob Winch
268267
* @since 3.2
269268
*/
270-
public static final class RequestMatchers {
269+
private static final class RequestMatchers {
271270

272271
private RequestMatchers() {
273272
}
@@ -280,7 +279,7 @@ private RequestMatchers() {
280279
* from
281280
* @return a {@link List} of {@link AntPathRequestMatcher} instances
282281
*/
283-
public static List<RequestMatcher> antMatchers(HttpMethod httpMethod, String... antPatterns) {
282+
static List<RequestMatcher> antMatchers(HttpMethod httpMethod, String... antPatterns) {
284283
String method = (httpMethod != null) ? httpMethod.toString() : null;
285284
List<RequestMatcher> matchers = new ArrayList<>();
286285
for (String pattern : antPatterns) {
@@ -296,7 +295,7 @@ public static List<RequestMatcher> antMatchers(HttpMethod httpMethod, String...
296295
* from
297296
* @return a {@link List} of {@link AntPathRequestMatcher} instances
298297
*/
299-
public static List<RequestMatcher> antMatchers(String... antPatterns) {
298+
static List<RequestMatcher> antMatchers(String... antPatterns) {
300299
return antMatchers(null, antPatterns);
301300
}
302301

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 javax.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

@@ -294,6 +291,8 @@ protected Filter performBuild() throws Exception {
294291
List<SecurityFilterChain> securityFilterChains = new ArrayList<>(chainSize);
295292
List<RequestMatcherEntry<List<WebInvocationPrivilegeEvaluator>>> requestMatcherPrivilegeEvaluatorsEntries = new ArrayList<>();
296293
for (RequestMatcher ignoredRequest : this.ignoredRequests) {
294+
WebSecurity.this.logger.warn("You are asking Spring Security to ignore " + ignoredRequest
295+
+ ". This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead.");
297296
SecurityFilterChain securityFilterChain = new DefaultSecurityFilterChain(ignoredRequest);
298297
securityFilterChains.add(securityFilterChain);
299298
requestMatcherPrivilegeEvaluatorsEntries
@@ -423,8 +422,6 @@ public class IgnoredRequestConfigurer extends AbstractRequestMatcherRegistry<Ign
423422
@Override
424423
public MvcMatchersIgnoredRequestConfigurer mvcMatchers(HttpMethod method, String... mvcPatterns) {
425424
List<MvcRequestMatcher> mvcMatchers = createMvcMatchers(method, mvcPatterns);
426-
Arrays.asList(mvcPatterns).stream().forEach((t) -> printWarnSecurityMessage(method, t));
427-
mvcMatchers.stream().forEach((t) -> t.ignore());
428425
WebSecurity.this.ignoredRequests.addAll(mvcMatchers);
429426
return new MvcMatchersIgnoredRequestConfigurer(getApplicationContext(), mvcMatchers);
430427
}
@@ -434,38 +431,6 @@ public MvcMatchersIgnoredRequestConfigurer mvcMatchers(String... mvcPatterns) {
434431
return mvcMatchers(null, mvcPatterns);
435432
}
436433

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-
469434
@Override
470435
protected IgnoredRequestConfigurer chainRequestMatchers(List<RequestMatcher> requestMatchers) {
471436
WebSecurity.this.ignoredRequests.addAll(requestMatchers);
@@ -479,33 +444,6 @@ public WebSecurity and() {
479444
return WebSecurity.this;
480445
}
481446

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-
509447
}
510448

511449
}

0 commit comments

Comments
 (0)