Skip to content

Commit 9ee6281

Browse files
committed
Better logging support by id in RequestMatcher
1. Added default method getId in RequestMatcher and marked it as FunctionalInterface 2. Created Class AbstractRequestMatcher with default support for getId logging things 3. Implemented build in matchers using AbstractRequestMatcher 4. Changed toString methods of build in matchers to reflect id. 5. Modified Regex and El RequestMatcher toString tests to reflect new toString. Fixes: gh-6274
1 parent a919b4e commit 9ee6281

File tree

24 files changed

+112
-53
lines changed

24 files changed

+112
-53
lines changed

config/src/integration-test/groovy/org/springframework/security/config/ldap/LdapProviderBeanDefinitionParserTests.groovy

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ package org.springframework.security.config.ldap
22

33
import org.springframework.security.crypto.password.NoOpPasswordEncoder
44

5-
import static org.mockito.Mockito.*
65

76
import java.text.MessageFormat
87

98
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
109
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
11-
import org.springframework.security.crypto.password.PasswordEncoder
1210
import org.springframework.security.config.AbstractXmlConfigTests
1311
import org.springframework.security.config.BeanIds
1412
import org.springframework.security.util.FieldUtils

config/src/main/java/org/springframework/security/config/annotation/web/configurers/PermitAllSupport.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.springframework.security.access.SecurityConfig;
2121
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
2222
import org.springframework.security.config.annotation.web.configurers.AbstractConfigAttributeRequestMatcherRegistry.UrlMapping;
23+
import org.springframework.security.web.util.matcher.AbstractRequestMatcher;
2324
import org.springframework.security.web.util.matcher.RequestMatcher;
2425

2526
/**
@@ -64,7 +65,7 @@ public static void permitAll(
6465
}
6566
}
6667

67-
private final static class ExactUrlRequestMatcher implements RequestMatcher {
68+
private final static class ExactUrlRequestMatcher extends AbstractRequestMatcher {
6869
private String processUrl;
6970

7071
private ExactUrlRequestMatcher(String processUrl) {
@@ -89,11 +90,11 @@ public boolean matches(HttpServletRequest request) {
8990
@Override
9091
public String toString() {
9192
StringBuilder sb = new StringBuilder();
92-
sb.append("ExactUrl [processUrl='").append(processUrl).append("']");
93+
sb.append("ExactUrl [processUrl='").append(processUrl).append("', id=").append(getId()).append("]");
9394
return sb.toString();
9495
}
9596
}
9697

9798
private PermitAllSupport() {
9899
}
99-
}
100+
}

config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/resource/OAuth2ResourceServerConfigurer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import org.springframework.security.oauth2.server.resource.web.access.BearerTokenAccessDeniedHandler;
4141
import org.springframework.security.web.AuthenticationEntryPoint;
4242
import org.springframework.security.web.access.AccessDeniedHandler;
43-
import org.springframework.security.web.util.matcher.RequestMatcher;
43+
import org.springframework.security.web.util.matcher.AbstractRequestMatcher;
4444
import org.springframework.util.Assert;
4545

4646
import static org.springframework.security.oauth2.jwt.JwtProcessors.withJwkSetUri;
@@ -294,7 +294,7 @@ BearerTokenResolver getBearerTokenResolver() {
294294
return this.bearerTokenResolver;
295295
}
296296

297-
private static final class BearerTokenRequestMatcher implements RequestMatcher {
297+
private static final class BearerTokenRequestMatcher extends AbstractRequestMatcher {
298298
private BearerTokenResolver bearerTokenResolver;
299299

300300
@Override

config/src/test/java/org/springframework/security/config/annotation/web/builders/NamespaceHttpTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
import org.springframework.security.web.context.NullSecurityContextRepository;
4242
import org.springframework.security.web.jaasapi.JaasApiIntegrationFilter;
4343
import org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper;
44+
import org.springframework.security.web.util.matcher.AbstractRequestMatcher;
4445
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
4546
import org.springframework.security.web.util.matcher.RegexRequestMatcher;
46-
import org.springframework.security.web.util.matcher.RequestMatcher;
4747
import org.springframework.stereotype.Controller;
4848
import org.springframework.test.web.servlet.MockMvc;
4949
import org.springframework.test.web.servlet.MvcResult;
@@ -386,7 +386,7 @@ protected void configure(HttpSecurity http) throws Exception {
386386
.requestMatcher(new MyRequestMatcher());
387387
}
388388

389-
static class MyRequestMatcher implements RequestMatcher {
389+
static class MyRequestMatcher extends AbstractRequestMatcher {
390390
public boolean matches(HttpServletRequest request) {
391391
return true;
392392
}

web/src/main/java/org/springframework/security/web/DefaultSecurityFilterChain.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,27 @@
1515
*/
1616
package org.springframework.security.web;
1717

18-
import org.apache.commons.logging.Log;
19-
import org.apache.commons.logging.LogFactory;
20-
import org.springframework.security.web.util.matcher.RequestMatcher;
21-
2218
import javax.servlet.Filter;
2319
import javax.servlet.http.HttpServletRequest;
2420
import java.util.*;
2521

22+
import org.apache.commons.logging.Log;
23+
import org.apache.commons.logging.LogFactory;
24+
25+
import org.springframework.security.web.util.matcher.RequestMatcher;
26+
2627
/**
2728
* Standard implementation of {@code SecurityFilterChain}.
2829
*
2930
* @author Luke Taylor
3031
*
3132
* @since 3.1
3233
*/
33-
public final class DefaultSecurityFilterChain implements SecurityFilterChain {
34+
public final class DefaultSecurityFilterChain implements SecurityFilterChain{
3435
private static final Log logger = LogFactory.getLog(DefaultSecurityFilterChain.class);
3536
private final RequestMatcher requestMatcher;
3637
private final List<Filter> filters;
38+
private String beanName;
3739

3840
public DefaultSecurityFilterChain(RequestMatcher requestMatcher, Filter... filters) {
3941
this(requestMatcher, Arrays.asList(filters));
@@ -61,4 +63,5 @@ public boolean matches(HttpServletRequest request) {
6163
public String toString() {
6264
return "[ " + requestMatcher + ", " + filters + "]";
6365
}
66+
6467
}

web/src/main/java/org/springframework/security/web/csrf/CsrfFilter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.security.web.access.AccessDeniedHandler;
3232
import org.springframework.security.web.access.AccessDeniedHandlerImpl;
3333
import org.springframework.security.web.util.UrlUtils;
34+
import org.springframework.security.web.util.matcher.AbstractRequestMatcher;
3435
import org.springframework.security.web.util.matcher.RequestMatcher;
3536
import org.springframework.util.Assert;
3637
import org.springframework.web.filter.OncePerRequestFilter;
@@ -159,7 +160,7 @@ public void setAccessDeniedHandler(AccessDeniedHandler accessDeniedHandler) {
159160
this.accessDeniedHandler = accessDeniedHandler;
160161
}
161162

162-
private static final class DefaultRequiresCsrfMatcher implements RequestMatcher {
163+
private static final class DefaultRequiresCsrfMatcher extends AbstractRequestMatcher {
163164
private final HashSet<String> allowedMethods = new HashSet<>(
164165
Arrays.asList("GET", "HEAD", "TRACE", "OPTIONS"));
165166

web/src/main/java/org/springframework/security/web/header/writers/HpkpHeaderWriter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.apache.commons.logging.Log;
1919
import org.apache.commons.logging.LogFactory;
2020
import org.springframework.security.web.header.HeaderWriter;
21+
import org.springframework.security.web.util.matcher.AbstractRequestMatcher;
2122
import org.springframework.security.web.util.matcher.RequestMatcher;
2223
import org.springframework.util.Assert;
2324

@@ -430,7 +431,7 @@ private void updateHpkpHeaderValue() {
430431
this.hpkpHeaderValue = headerValue;
431432
}
432433

433-
private static final class SecureRequestMatcher implements RequestMatcher {
434+
private static final class SecureRequestMatcher extends AbstractRequestMatcher {
434435
public boolean matches(HttpServletRequest request) {
435436
return request.isSecure();
436437
}

web/src/main/java/org/springframework/security/web/header/writers/HstsHeaderWriter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.commons.logging.LogFactory;
2323

2424
import org.springframework.security.web.header.HeaderWriter;
25+
import org.springframework.security.web.util.matcher.AbstractRequestMatcher;
2526
import org.springframework.security.web.util.matcher.RequestMatcher;
2627
import org.springframework.util.Assert;
2728

@@ -195,7 +196,7 @@ private void updateHstsHeaderValue() {
195196
this.hstsHeaderValue = headerValue;
196197
}
197198

198-
private static final class SecureRequestMatcher implements RequestMatcher {
199+
private static final class SecureRequestMatcher extends AbstractRequestMatcher {
199200
public boolean matches(HttpServletRequest request) {
200201
return request.isSecure();
201202
}

web/src/main/java/org/springframework/security/web/servlet/util/matcher/MvcRequestMatcher.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.servlet.http.HttpServletRequest;
2323

2424
import org.springframework.http.HttpMethod;
25+
import org.springframework.security.web.util.matcher.AbstractRequestMatcher;
2526
import org.springframework.security.web.util.matcher.RequestMatcher;
2627
import org.springframework.security.web.util.matcher.RequestVariablesExtractor;
2728
import org.springframework.util.AntPathMatcher;
@@ -45,7 +46,7 @@
4546
* @author Rob Winch
4647
* @since 4.1.1
4748
*/
48-
public class MvcRequestMatcher implements RequestMatcher, RequestVariablesExtractor {
49+
public class MvcRequestMatcher extends AbstractRequestMatcher implements RequestVariablesExtractor {
4950

5051
private final DefaultMatcher defaultMatcher = new DefaultMatcher();
5152

@@ -141,7 +142,7 @@ public String toString() {
141142
return sb.toString();
142143
}
143144

144-
private class DefaultMatcher implements RequestMatcher, RequestVariablesExtractor {
145+
private class DefaultMatcher extends AbstractRequestMatcher implements RequestVariablesExtractor {
145146

146147
private final UrlPathHelper pathHelper = new UrlPathHelper();
147148

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2002-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+
package org.springframework.security.web.util.matcher;
17+
18+
19+
import java.util.UUID;
20+
/**
21+
* Abstract simple strategy to match an <tt>HttpServletRequest</tt>.
22+
*
23+
* @author Ankur Pathak
24+
* @since 5.2.0
25+
*/
26+
public abstract class AbstractRequestMatcher implements RequestMatcher {
27+
private final String id;
28+
29+
public AbstractRequestMatcher(String id) {
30+
this.id = id;
31+
}
32+
33+
public AbstractRequestMatcher() {
34+
this(UUID.randomUUID().toString().replace("-", "").substring(0, 8));
35+
}
36+
37+
@Override
38+
public String getId() {
39+
return id;
40+
}
41+
42+
@Override
43+
public String toString() {
44+
return "RequestMatcher{" +
45+
"id='" + id + '\'' +
46+
'}';
47+
}
48+
}

web/src/main/java/org/springframework/security/web/util/matcher/AndRequestMatcher.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import org.apache.commons.logging.Log;
2424
import org.apache.commons.logging.LogFactory;
25-
import org.springframework.security.web.util.matcher.RequestMatcher;
2625
import org.springframework.util.Assert;
2726

2827
/**
@@ -32,7 +31,7 @@
3231
* @author Rob Winch
3332
* @since 3.2
3433
*/
35-
public final class AndRequestMatcher implements RequestMatcher {
34+
public final class AndRequestMatcher extends AbstractRequestMatcher {
3635
private final Log logger = LogFactory.getLog(getClass());
3736

3837
private final List<RequestMatcher> requestMatchers;
@@ -76,6 +75,6 @@ public boolean matches(HttpServletRequest request) {
7675

7776
@Override
7877
public String toString() {
79-
return "AndRequestMatcher [requestMatchers=" + requestMatchers + "]";
78+
return "AndRequestMatcher [requestMatchers=" + requestMatchers + ", id=" + getId()+"]";
8079
}
81-
}
80+
}

web/src/main/java/org/springframework/security/web/util/matcher/AntPathRequestMatcher.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
*
5454
* @see org.springframework.util.AntPathMatcher
5555
*/
56-
public final class AntPathRequestMatcher
57-
implements RequestMatcher, RequestVariablesExtractor {
56+
public final class AntPathRequestMatcher extends AbstractRequestMatcher
57+
implements RequestVariablesExtractor {
5858
private static final Log logger = LogFactory.getLog(AntPathRequestMatcher.class);
5959
private static final String MATCH_ALL = "/**";
6060

@@ -236,6 +236,7 @@ public String toString() {
236236
sb.append(", ").append(this.httpMethod);
237237
}
238238

239+
sb.append(", id=").append(getId());
239240
sb.append("]");
240241

241242
return sb.toString();

web/src/main/java/org/springframework/security/web/util/matcher/AnyRequestMatcher.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717

1818
import javax.servlet.http.HttpServletRequest;
1919

20-
import org.springframework.security.web.util.matcher.RequestMatcher;
21-
2220
/**
2321
* Matches any supplied request.
2422
*
2523
* @author Luke Taylor
2624
* @since 3.1
2725
*/
28-
public final class AnyRequestMatcher implements RequestMatcher {
26+
public final class AnyRequestMatcher extends AbstractRequestMatcher {
2927
public static final RequestMatcher INSTANCE = new AnyRequestMatcher();
3028

3129
public boolean matches(HttpServletRequest request) {
@@ -46,7 +44,7 @@ public int hashCode() {
4644

4745
@Override
4846
public String toString() {
49-
return "any request";
47+
return "any request[id=" + getId() + "]";
5048
}
5149

5250
private AnyRequestMatcher() {

web/src/main/java/org/springframework/security/web/util/matcher/ELRequestMatcher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.springframework.expression.spel.standard.SpelExpressionParser;
2424
import org.springframework.expression.spel.support.StandardEvaluationContext;
2525
import org.springframework.security.web.authentication.DelegatingAuthenticationEntryPoint;
26-
import org.springframework.security.web.util.matcher.RequestMatcher;
2726

2827
/**
2928
* A RequestMatcher implementation which uses a SpEL expression
@@ -41,7 +40,7 @@
4140
* @author Mike Wiesner
4241
* @since 3.0.2
4342
*/
44-
public class ELRequestMatcher implements RequestMatcher {
43+
public class ELRequestMatcher extends AbstractRequestMatcher {
4544

4645
private final Expression expression;
4746

@@ -69,6 +68,7 @@ public EvaluationContext createELContext(HttpServletRequest request) {
6968
public String toString() {
7069
StringBuilder sb = new StringBuilder();
7170
sb.append("EL [el=\"").append(this.expression.getExpressionString()).append("\"");
71+
sb.append(", id=").append(getId());
7272
sb.append("]");
7373
return sb.toString();
7474
}

web/src/main/java/org/springframework/security/web/util/matcher/IpAddressMatcher.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import javax.servlet.http.HttpServletRequest;
2323

24-
import org.springframework.security.web.util.matcher.RequestMatcher;
2524
import org.springframework.util.StringUtils;
2625

2726
/**
@@ -34,7 +33,7 @@
3433
* @author Luke Taylor
3534
* @since 3.0.2
3635
*/
37-
public final class IpAddressMatcher implements RequestMatcher {
36+
public final class IpAddressMatcher extends AbstractRequestMatcher {
3837
private final int nMaskBits;
3938
private final InetAddress requiredAddress;
4039

web/src/main/java/org/springframework/security/web/util/matcher/MediaTypeRequestMatcher.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
* @since 3.2
139139
*/
140140

141-
public final class MediaTypeRequestMatcher implements RequestMatcher {
141+
public final class MediaTypeRequestMatcher extends AbstractRequestMatcher {
142142
private final Log logger = LogFactory.getLog(getClass());
143143
private final ContentNegotiationStrategy contentNegotiationStrategy;
144144
private final Collection<MediaType> matchingMediaTypes;
@@ -250,6 +250,6 @@ public String toString() {
250250
return "MediaTypeRequestMatcher [contentNegotiationStrategy="
251251
+ this.contentNegotiationStrategy + ", matchingMediaTypes="
252252
+ this.matchingMediaTypes + ", useEquals=" + this.useEquals
253-
+ ", ignoredMediaTypes=" + this.ignoredMediaTypes + "]";
253+
+ ", ignoredMediaTypes=" + this.ignoredMediaTypes + ", id="+ getId()+"]";
254254
}
255-
}
255+
}

0 commit comments

Comments
 (0)