Skip to content

Better logging support by id in RequestMatcher #6308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package org.springframework.security.config.ldap

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

import static org.mockito.Mockito.*

import java.text.MessageFormat

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.security.config.AbstractXmlConfigTests
import org.springframework.security.config.BeanIds
import org.springframework.security.util.FieldUtils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
import org.springframework.security.config.annotation.web.configurers.AbstractConfigAttributeRequestMatcherRegistry.UrlMapping;
import org.springframework.security.web.util.matcher.AbstractRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;

/**
Expand Down Expand Up @@ -64,7 +65,7 @@ public static void permitAll(
}
}

private final static class ExactUrlRequestMatcher implements RequestMatcher {
private final static class ExactUrlRequestMatcher extends AbstractRequestMatcher {
private String processUrl;

private ExactUrlRequestMatcher(String processUrl) {
Expand All @@ -89,11 +90,11 @@ public boolean matches(HttpServletRequest request) {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("ExactUrl [processUrl='").append(processUrl).append("']");
sb.append("ExactUrl [processUrl='").append(processUrl).append("', id=").append(getId()).append("]");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can live away with String builder now, by-default string concatenations internally invoke string builder only. Plus it will result in cleaner code

return sb.toString();
}
}

private PermitAllSupport() {
}
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use the existing formatter so this does not occur

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import org.springframework.security.oauth2.server.resource.web.access.BearerTokenAccessDeniedHandler;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.security.web.util.matcher.AbstractRequestMatcher;
import org.springframework.util.Assert;

import static org.springframework.security.oauth2.jwt.JwtProcessors.withJwkSetUri;
Expand Down Expand Up @@ -294,7 +294,7 @@ BearerTokenResolver getBearerTokenResolver() {
return this.bearerTokenResolver;
}

private static final class BearerTokenRequestMatcher implements RequestMatcher {
private static final class BearerTokenRequestMatcher extends AbstractRequestMatcher {
private BearerTokenResolver bearerTokenResolver;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
import org.springframework.security.web.context.NullSecurityContextRepository;
import org.springframework.security.web.jaasapi.JaasApiIntegrationFilter;
import org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper;
import org.springframework.security.web.util.matcher.AbstractRequestMatcher;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RegexRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.stereotype.Controller;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
Expand Down Expand Up @@ -386,7 +386,7 @@ protected void configure(HttpSecurity http) throws Exception {
.requestMatcher(new MyRequestMatcher());
}

static class MyRequestMatcher implements RequestMatcher {
static class MyRequestMatcher extends AbstractRequestMatcher {
public boolean matches(HttpServletRequest request) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,27 @@
*/
package org.springframework.security.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.web.util.matcher.RequestMatcher;

import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.security.web.util.matcher.RequestMatcher;

/**
* Standard implementation of {@code SecurityFilterChain}.
*
* @author Luke Taylor
*
* @since 3.1
*/
public final class DefaultSecurityFilterChain implements SecurityFilterChain {
public final class DefaultSecurityFilterChain implements SecurityFilterChain{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use the existing formatter so this does not occur

private static final Log logger = LogFactory.getLog(DefaultSecurityFilterChain.class);
private final RequestMatcher requestMatcher;
private final List<Filter> filters;
private String beanName;

public DefaultSecurityFilterChain(RequestMatcher requestMatcher, Filter... filters) {
this(requestMatcher, Arrays.asList(filters));
Expand Down Expand Up @@ -61,4 +63,5 @@ public boolean matches(HttpServletRequest request) {
public String toString() {
return "[ " + requestMatcher + ", " + filters + "]";
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use the existing formatter so this does not occur

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by existing formatter??

Copy link

@dkapil dkapil Mar 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant, these whitespaces means that the code formatter we have used for this PR does not match the one used originally. Formatter can also mean any settings in your IDE that indents/formats/structures code. Please see the section on whitespaces in contributing guidelines. Hope this helps

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.access.AccessDeniedHandlerImpl;
import org.springframework.security.web.util.UrlUtils;
import org.springframework.security.web.util.matcher.AbstractRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.util.Assert;
import org.springframework.web.filter.OncePerRequestFilter;
Expand Down Expand Up @@ -159,7 +160,7 @@ public void setAccessDeniedHandler(AccessDeniedHandler accessDeniedHandler) {
this.accessDeniedHandler = accessDeniedHandler;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.web.header.HeaderWriter;
import org.springframework.security.web.util.matcher.AbstractRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -430,7 +431,7 @@ private void updateHpkpHeaderValue() {
this.hpkpHeaderValue = headerValue;
}

private static final class SecureRequestMatcher implements RequestMatcher {
private static final class SecureRequestMatcher extends AbstractRequestMatcher {
public boolean matches(HttpServletRequest request) {
return request.isSecure();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.commons.logging.LogFactory;

import org.springframework.security.web.header.HeaderWriter;
import org.springframework.security.web.util.matcher.AbstractRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -195,7 +196,7 @@ private void updateHstsHeaderValue() {
this.hstsHeaderValue = headerValue;
}

private static final class SecureRequestMatcher implements RequestMatcher {
private static final class SecureRequestMatcher extends AbstractRequestMatcher {
public boolean matches(HttpServletRequest request) {
return request.isSecure();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpMethod;
import org.springframework.security.web.util.matcher.AbstractRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.security.web.util.matcher.RequestVariablesExtractor;
import org.springframework.util.AntPathMatcher;
Expand All @@ -45,7 +46,7 @@
* @author Rob Winch
* @since 4.1.1
*/
public class MvcRequestMatcher implements RequestMatcher, RequestVariablesExtractor {
public class MvcRequestMatcher extends AbstractRequestMatcher implements RequestVariablesExtractor {

private final DefaultMatcher defaultMatcher = new DefaultMatcher();

Expand Down Expand Up @@ -141,7 +142,7 @@ public String toString() {
return sb.toString();
}

private class DefaultMatcher implements RequestMatcher, RequestVariablesExtractor {
private class DefaultMatcher extends AbstractRequestMatcher implements RequestVariablesExtractor {

private final UrlPathHelper pathHelper = new UrlPathHelper();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.web.util.matcher;


import java.util.UUID;
/**
* Abstract simple strategy to match an <tt>HttpServletRequest</tt>.
*
* @author Ankur Pathak
* @since 5.2.0
*/
public abstract class AbstractRequestMatcher implements RequestMatcher {
private final String id;

public AbstractRequestMatcher(String id) {
this.id = id;
}

public AbstractRequestMatcher() {
this(UUID.randomUUID().toString().replace("-", "").substring(0, 8));
}

@Override
public String getId() {
return id;
}

@Override
public String toString() {
return "RequestMatcher{" +
"id='" + id + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.util.Assert;

/**
Expand All @@ -32,7 +31,7 @@
* @author Rob Winch
* @since 3.2
*/
public final class AndRequestMatcher implements RequestMatcher {
public final class AndRequestMatcher extends AbstractRequestMatcher {
private final Log logger = LogFactory.getLog(getClass());

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

@Override
public String toString() {
return "AndRequestMatcher [requestMatchers=" + requestMatchers + "]";
return "AndRequestMatcher [requestMatchers=" + requestMatchers + ", id=" + getId()+"]";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is code formatted correctly?

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
*
* @see org.springframework.util.AntPathMatcher
*/
public final class AntPathRequestMatcher
implements RequestMatcher, RequestVariablesExtractor {
public final class AntPathRequestMatcher extends AbstractRequestMatcher
implements RequestVariablesExtractor {
private static final Log logger = LogFactory.getLog(AntPathRequestMatcher.class);
private static final String MATCH_ALL = "/**";

Expand Down Expand Up @@ -236,6 +236,7 @@ public String toString() {
sb.append(", ").append(this.httpMethod);
}

sb.append(", id=").append(getId());
sb.append("]");

return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.web.util.matcher.RequestMatcher;

/**
* Matches any supplied request.
*
* @author Luke Taylor
* @since 3.1
*/
public final class AnyRequestMatcher implements RequestMatcher {
public final class AnyRequestMatcher extends AbstractRequestMatcher {
public static final RequestMatcher INSTANCE = new AnyRequestMatcher();

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

@Override
public String toString() {
return "any request";
return "any request[id=" + getId() + "]";
}

private AnyRequestMatcher() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.security.web.authentication.DelegatingAuthenticationEntryPoint;
import org.springframework.security.web.util.matcher.RequestMatcher;

/**
* A RequestMatcher implementation which uses a SpEL expression
Expand All @@ -41,7 +40,7 @@
* @author Mike Wiesner
* @since 3.0.2
*/
public class ELRequestMatcher implements RequestMatcher {
public class ELRequestMatcher extends AbstractRequestMatcher {

private final Expression expression;

Expand Down Expand Up @@ -69,6 +68,7 @@ public EvaluationContext createELContext(HttpServletRequest request) {
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("EL [el=\"").append(this.expression.getExpressionString()).append("\"");
sb.append(", id=").append(getId());
sb.append("]");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import javax.servlet.http.HttpServletRequest;

import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.util.StringUtils;

/**
Expand All @@ -34,7 +33,7 @@
* @author Luke Taylor
* @since 3.0.2
*/
public final class IpAddressMatcher implements RequestMatcher {
public final class IpAddressMatcher extends AbstractRequestMatcher {
private final int nMaskBits;
private final InetAddress requiredAddress;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
* @since 3.2
*/

public final class MediaTypeRequestMatcher implements RequestMatcher {
public final class MediaTypeRequestMatcher extends AbstractRequestMatcher {
private final Log logger = LogFactory.getLog(getClass());
private final ContentNegotiationStrategy contentNegotiationStrategy;
private final Collection<MediaType> matchingMediaTypes;
Expand Down Expand Up @@ -250,6 +250,6 @@ public String toString() {
return "MediaTypeRequestMatcher [contentNegotiationStrategy="
+ this.contentNegotiationStrategy + ", matchingMediaTypes="
+ this.matchingMediaTypes + ", useEquals=" + this.useEquals
+ ", ignoredMediaTypes=" + this.ignoredMediaTypes + "]";
+ ", ignoredMediaTypes=" + this.ignoredMediaTypes + ", id="+ getId()+"]";
}
}
}
Loading