mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-05-31 09:12:14 +00:00
Polish RequestMatcher logging and toString
This commit is contained in:
parent
76a8bbe98d
commit
0ac1176152
@ -22,6 +22,8 @@ import javax.servlet.ServletException;
|
|||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.security.core.AuthenticationException;
|
import org.springframework.security.core.AuthenticationException;
|
||||||
import org.springframework.security.web.AuthenticationEntryPoint;
|
import org.springframework.security.web.AuthenticationEntryPoint;
|
||||||
@ -55,6 +57,7 @@ import org.springframework.util.Assert;
|
|||||||
* @since 3.0.2
|
* @since 3.0.2
|
||||||
*/
|
*/
|
||||||
public class DelegatingAuthenticationEntryPoint implements AuthenticationEntryPoint, InitializingBean {
|
public class DelegatingAuthenticationEntryPoint implements AuthenticationEntryPoint, InitializingBean {
|
||||||
|
private final Log logger = LogFactory.getLog(getClass());
|
||||||
|
|
||||||
private final LinkedHashMap<RequestMatcher, AuthenticationEntryPoint> entryPoints;
|
private final LinkedHashMap<RequestMatcher, AuthenticationEntryPoint> entryPoints;
|
||||||
private AuthenticationEntryPoint defaultEntryPoint;
|
private AuthenticationEntryPoint defaultEntryPoint;
|
||||||
@ -68,12 +71,23 @@ public class DelegatingAuthenticationEntryPoint implements AuthenticationEntryPo
|
|||||||
throws IOException, ServletException {
|
throws IOException, ServletException {
|
||||||
|
|
||||||
for (RequestMatcher requestMatcher : entryPoints.keySet()) {
|
for (RequestMatcher requestMatcher : entryPoints.keySet()) {
|
||||||
|
if(logger.isDebugEnabled()) {
|
||||||
|
logger.debug("Trying to match using " + requestMatcher);
|
||||||
|
}
|
||||||
if (requestMatcher.matches(request)) {
|
if (requestMatcher.matches(request)) {
|
||||||
entryPoints.get(requestMatcher).commence(request, response, authException);
|
AuthenticationEntryPoint entryPoint = entryPoints.get(requestMatcher);
|
||||||
|
if(logger.isDebugEnabled()) {
|
||||||
|
logger.debug("Match found! Executing " + entryPoint);
|
||||||
|
}
|
||||||
|
entryPoint.commence(request, response, authException);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(logger.isDebugEnabled()) {
|
||||||
|
logger.debug("No match found. Using default entry point " + defaultEntryPoint);
|
||||||
|
}
|
||||||
|
|
||||||
// No EntryPoint matched, use defaultEntryPoint
|
// No EntryPoint matched, use defaultEntryPoint
|
||||||
defaultEntryPoint.commence(request, response, authException);
|
defaultEntryPoint.commence(request, response, authException);
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,8 @@ import java.util.List;
|
|||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
|
||||||
@ -31,6 +33,8 @@ import org.springframework.util.Assert;
|
|||||||
* @since 3.2
|
* @since 3.2
|
||||||
*/
|
*/
|
||||||
public final class AndRequestMatcher implements RequestMatcher {
|
public final class AndRequestMatcher implements RequestMatcher {
|
||||||
|
private final Log logger = LogFactory.getLog(getClass());
|
||||||
|
|
||||||
private final List<RequestMatcher> requestMatchers;
|
private final List<RequestMatcher> requestMatchers;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -57,10 +61,20 @@ public final class AndRequestMatcher implements RequestMatcher {
|
|||||||
|
|
||||||
public boolean matches(HttpServletRequest request) {
|
public boolean matches(HttpServletRequest request) {
|
||||||
for(RequestMatcher matcher : requestMatchers) {
|
for(RequestMatcher matcher : requestMatchers) {
|
||||||
|
if(logger.isDebugEnabled()) {
|
||||||
|
logger.debug("Trying to match using " + matcher);
|
||||||
|
}
|
||||||
if(!matcher.matches(request)) {
|
if(!matcher.matches(request)) {
|
||||||
|
logger.debug("Did not match");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
logger.debug("All requestMatchers returned true");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "AndRequestMatcher [requestMatchers=" + requestMatchers + "]";
|
||||||
|
}
|
||||||
}
|
}
|
@ -175,19 +175,33 @@ public final class MediaTypeRequestMatcher implements RequestMatcher {
|
|||||||
logger.debug("Failed to parse MediaTypes, returning false", e);
|
logger.debug("Failed to parse MediaTypes, returning false", e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if(logger.isDebugEnabled()) {
|
||||||
|
logger.debug("httpRequestMediaTypes=" + httpRequestMediaTypes);
|
||||||
|
}
|
||||||
for(MediaType httpRequestMediaType : httpRequestMediaTypes) {
|
for(MediaType httpRequestMediaType : httpRequestMediaTypes) {
|
||||||
|
if(logger.isDebugEnabled()) {
|
||||||
|
logger.debug("Processing " + httpRequestMediaType);
|
||||||
|
}
|
||||||
if(shouldIgnore(httpRequestMediaType)) {
|
if(shouldIgnore(httpRequestMediaType)) {
|
||||||
|
logger.debug("Ignoring");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(useEquals) {
|
if(useEquals) {
|
||||||
return matchingMediaTypes.contains(httpRequestMediaType);
|
boolean isEqualTo = matchingMediaTypes.contains(httpRequestMediaType);
|
||||||
|
logger.debug("isEqualTo " + isEqualTo);
|
||||||
|
return isEqualTo;
|
||||||
}
|
}
|
||||||
for(MediaType matchingMediaType : matchingMediaTypes) {
|
for(MediaType matchingMediaType : matchingMediaTypes) {
|
||||||
if(matchingMediaType.isCompatibleWith(httpRequestMediaType)) {
|
boolean isCompatibleWith = matchingMediaType.isCompatibleWith(httpRequestMediaType);
|
||||||
|
if(logger.isDebugEnabled()) {
|
||||||
|
logger.debug(matchingMediaType + " .isCompatibleWith " + httpRequestMediaType + " = " + isCompatibleWith);
|
||||||
|
}
|
||||||
|
if(isCompatibleWith) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
logger.debug("Did not match any media types");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,4 +238,12 @@ public final class MediaTypeRequestMatcher implements RequestMatcher {
|
|||||||
public void setIgnoredMediaTypes(Set<MediaType> ignoredMediaTypes) {
|
public void setIgnoredMediaTypes(Set<MediaType> ignoredMediaTypes) {
|
||||||
this.ignoredMediaTypes = ignoredMediaTypes;
|
this.ignoredMediaTypes = ignoredMediaTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "MediaTypeRequestMatcher [contentNegotiationStrategy="
|
||||||
|
+ contentNegotiationStrategy + ", matchingMediaTypes="
|
||||||
|
+ matchingMediaTypes + ", useEquals=" + useEquals
|
||||||
|
+ ", ignoredMediaTypes=" + ignoredMediaTypes + "]";
|
||||||
|
}
|
||||||
}
|
}
|
@ -17,6 +17,8 @@ package org.springframework.security.web.util;
|
|||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,6 +31,8 @@ import org.springframework.util.Assert;
|
|||||||
* @since 3.2
|
* @since 3.2
|
||||||
*/
|
*/
|
||||||
public class NegatedRequestMatcher implements RequestMatcher {
|
public class NegatedRequestMatcher implements RequestMatcher {
|
||||||
|
private final Log logger = LogFactory.getLog(getClass());
|
||||||
|
|
||||||
private final RequestMatcher requestMatcher;
|
private final RequestMatcher requestMatcher;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -41,6 +45,15 @@ public class NegatedRequestMatcher implements RequestMatcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean matches(HttpServletRequest request) {
|
public boolean matches(HttpServletRequest request) {
|
||||||
return !requestMatcher.matches(request);
|
boolean result = !requestMatcher.matches(request);
|
||||||
|
if(logger.isDebugEnabled()) {
|
||||||
|
logger.debug("matches = " + result);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "NegatedRequestMatcher [requestMatcher=" + requestMatcher + "]";
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -20,6 +20,8 @@ import java.util.List;
|
|||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
|
||||||
@ -31,6 +33,7 @@ import org.springframework.util.Assert;
|
|||||||
* @since 3.2
|
* @since 3.2
|
||||||
*/
|
*/
|
||||||
public final class OrRequestMatcher implements RequestMatcher {
|
public final class OrRequestMatcher implements RequestMatcher {
|
||||||
|
private final Log logger = LogFactory.getLog(getClass());
|
||||||
private final List<RequestMatcher> requestMatchers;
|
private final List<RequestMatcher> requestMatchers;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -57,10 +60,20 @@ public final class OrRequestMatcher implements RequestMatcher {
|
|||||||
|
|
||||||
public boolean matches(HttpServletRequest request) {
|
public boolean matches(HttpServletRequest request) {
|
||||||
for(RequestMatcher matcher : requestMatchers) {
|
for(RequestMatcher matcher : requestMatchers) {
|
||||||
|
if(logger.isDebugEnabled()) {
|
||||||
|
logger.debug("Trying to match using " + matcher);
|
||||||
|
}
|
||||||
if(matcher.matches(request)) {
|
if(matcher.matches(request)) {
|
||||||
|
logger.debug("matched");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
logger.debug("No matches found");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "OrRequestMatcher [requestMatchers=" + requestMatchers + "]";
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user