Skip to content

Commit 0ac1176

Browse files
committed
Polish RequestMatcher logging and toString
1 parent 76a8bbe commit 0ac1176

File tree

5 files changed

+80
-4
lines changed

5 files changed

+80
-4
lines changed

web/src/main/java/org/springframework/security/web/authentication/DelegatingAuthenticationEntryPoint.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import javax.servlet.http.HttpServletRequest;
2323
import javax.servlet.http.HttpServletResponse;
2424

25+
import org.apache.commons.logging.Log;
26+
import org.apache.commons.logging.LogFactory;
2527
import org.springframework.beans.factory.InitializingBean;
2628
import org.springframework.security.core.AuthenticationException;
2729
import org.springframework.security.web.AuthenticationEntryPoint;
@@ -55,6 +57,7 @@
5557
* @since 3.0.2
5658
*/
5759
public class DelegatingAuthenticationEntryPoint implements AuthenticationEntryPoint, InitializingBean {
60+
private final Log logger = LogFactory.getLog(getClass());
5861

5962
private final LinkedHashMap<RequestMatcher, AuthenticationEntryPoint> entryPoints;
6063
private AuthenticationEntryPoint defaultEntryPoint;
@@ -68,12 +71,23 @@ public void commence(HttpServletRequest request,
6871
throws IOException, ServletException {
6972

7073
for (RequestMatcher requestMatcher : entryPoints.keySet()) {
74+
if(logger.isDebugEnabled()) {
75+
logger.debug("Trying to match using " + requestMatcher);
76+
}
7177
if (requestMatcher.matches(request)) {
72-
entryPoints.get(requestMatcher).commence(request, response, authException);
78+
AuthenticationEntryPoint entryPoint = entryPoints.get(requestMatcher);
79+
if(logger.isDebugEnabled()) {
80+
logger.debug("Match found! Executing " + entryPoint);
81+
}
82+
entryPoint.commence(request, response, authException);
7383
return;
7484
}
7585
}
7686

87+
if(logger.isDebugEnabled()) {
88+
logger.debug("No match found. Using default entry point " + defaultEntryPoint);
89+
}
90+
7791
// No EntryPoint matched, use defaultEntryPoint
7892
defaultEntryPoint.commence(request, response, authException);
7993
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import javax.servlet.http.HttpServletRequest;
2222

23+
import org.apache.commons.logging.Log;
24+
import org.apache.commons.logging.LogFactory;
2325
import org.springframework.util.Assert;
2426

2527

@@ -31,6 +33,8 @@
3133
* @since 3.2
3234
*/
3335
public final class AndRequestMatcher implements RequestMatcher {
36+
private final Log logger = LogFactory.getLog(getClass());
37+
3438
private final List<RequestMatcher> requestMatchers;
3539

3640
/**
@@ -57,10 +61,20 @@ public AndRequestMatcher(RequestMatcher... requestMatchers) {
5761

5862
public boolean matches(HttpServletRequest request) {
5963
for(RequestMatcher matcher : requestMatchers) {
64+
if(logger.isDebugEnabled()) {
65+
logger.debug("Trying to match using " + matcher);
66+
}
6067
if(!matcher.matches(request)) {
68+
logger.debug("Did not match");
6169
return false;
6270
}
6371
}
72+
logger.debug("All requestMatchers returned true");
6473
return true;
6574
}
75+
76+
@Override
77+
public String toString() {
78+
return "AndRequestMatcher [requestMatchers=" + requestMatchers + "]";
79+
}
6680
}

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,33 @@ public boolean matches(HttpServletRequest request) {
175175
logger.debug("Failed to parse MediaTypes, returning false", e);
176176
return false;
177177
}
178+
if(logger.isDebugEnabled()) {
179+
logger.debug("httpRequestMediaTypes=" + httpRequestMediaTypes);
180+
}
178181
for(MediaType httpRequestMediaType : httpRequestMediaTypes) {
182+
if(logger.isDebugEnabled()) {
183+
logger.debug("Processing " + httpRequestMediaType);
184+
}
179185
if(shouldIgnore(httpRequestMediaType)) {
186+
logger.debug("Ignoring");
180187
continue;
181188
}
182189
if(useEquals) {
183-
return matchingMediaTypes.contains(httpRequestMediaType);
190+
boolean isEqualTo = matchingMediaTypes.contains(httpRequestMediaType);
191+
logger.debug("isEqualTo " + isEqualTo);
192+
return isEqualTo;
184193
}
185194
for(MediaType matchingMediaType : matchingMediaTypes) {
186-
if(matchingMediaType.isCompatibleWith(httpRequestMediaType)) {
195+
boolean isCompatibleWith = matchingMediaType.isCompatibleWith(httpRequestMediaType);
196+
if(logger.isDebugEnabled()) {
197+
logger.debug(matchingMediaType + " .isCompatibleWith " + httpRequestMediaType + " = " + isCompatibleWith);
198+
}
199+
if(isCompatibleWith) {
187200
return true;
188201
}
189202
}
190203
}
204+
logger.debug("Did not match any media types");
191205
return false;
192206
}
193207

@@ -224,4 +238,12 @@ public void setUseEquals(boolean useEquals) {
224238
public void setIgnoredMediaTypes(Set<MediaType> ignoredMediaTypes) {
225239
this.ignoredMediaTypes = ignoredMediaTypes;
226240
}
241+
242+
@Override
243+
public String toString() {
244+
return "MediaTypeRequestMatcher [contentNegotiationStrategy="
245+
+ contentNegotiationStrategy + ", matchingMediaTypes="
246+
+ matchingMediaTypes + ", useEquals=" + useEquals
247+
+ ", ignoredMediaTypes=" + ignoredMediaTypes + "]";
248+
}
227249
}

web/src/main/java/org/springframework/security/web/util/NegatedRequestMatcher.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import javax.servlet.http.HttpServletRequest;
1919

20+
import org.apache.commons.logging.Log;
21+
import org.apache.commons.logging.LogFactory;
2022
import org.springframework.util.Assert;
2123

2224
/**
@@ -29,6 +31,8 @@
2931
* @since 3.2
3032
*/
3133
public class NegatedRequestMatcher implements RequestMatcher {
34+
private final Log logger = LogFactory.getLog(getClass());
35+
3236
private final RequestMatcher requestMatcher;
3337

3438
/**
@@ -41,6 +45,15 @@ public NegatedRequestMatcher(RequestMatcher requestMatcher) {
4145
}
4246

4347
public boolean matches(HttpServletRequest request) {
44-
return !requestMatcher.matches(request);
48+
boolean result = !requestMatcher.matches(request);
49+
if(logger.isDebugEnabled()) {
50+
logger.debug("matches = " + result);
51+
}
52+
return result;
53+
}
54+
55+
@Override
56+
public String toString() {
57+
return "NegatedRequestMatcher [requestMatcher=" + requestMatcher + "]";
4558
}
4659
}

web/src/main/java/org/springframework/security/web/util/OrRequestMatcher.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import javax.servlet.http.HttpServletRequest;
2222

23+
import org.apache.commons.logging.Log;
24+
import org.apache.commons.logging.LogFactory;
2325
import org.springframework.util.Assert;
2426

2527

@@ -31,6 +33,7 @@
3133
* @since 3.2
3234
*/
3335
public final class OrRequestMatcher implements RequestMatcher {
36+
private final Log logger = LogFactory.getLog(getClass());
3437
private final List<RequestMatcher> requestMatchers;
3538

3639
/**
@@ -57,10 +60,20 @@ public OrRequestMatcher(RequestMatcher... requestMatchers) {
5760

5861
public boolean matches(HttpServletRequest request) {
5962
for(RequestMatcher matcher : requestMatchers) {
63+
if(logger.isDebugEnabled()) {
64+
logger.debug("Trying to match using " + matcher);
65+
}
6066
if(matcher.matches(request)) {
67+
logger.debug("matched");
6168
return true;
6269
}
6370
}
71+
logger.debug("No matches found");
6472
return false;
6573
}
74+
75+
@Override
76+
public String toString() {
77+
return "OrRequestMatcher [requestMatchers=" + requestMatchers + "]";
78+
}
6679
}

0 commit comments

Comments
 (0)