Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -50,6 +50,7 @@
*
* @author Joe Grandja
* @author Rob Winch
* @author Eddú Meléndez
* @since 5.1
* @see OAuth2AuthorizationRequestResolver
* @see OAuth2AuthorizationRequestRedirectFilter
Expand Down Expand Up @@ -147,7 +148,7 @@ private OAuth2AuthorizationRequest resolve(HttpServletRequest request, String re
private String resolveRegistrationId(HttpServletRequest request) {
if (this.authorizationRequestMatcher.matches(request)) {
return this.authorizationRequestMatcher
.extractUriTemplateVariables(request).get(REGISTRATION_ID_URI_VARIABLE_NAME);
.matcher(request).getVariables().get(REGISTRATION_ID_URI_VARIABLE_NAME);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
Expand Down Expand Up @@ -33,13 +33,13 @@
import org.springframework.security.web.access.intercept.DefaultFilterInvocationSecurityMetadataSource;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.security.web.util.matcher.RequestVariablesExtractor;
import org.springframework.util.Assert;

/**
* Expression-based {@code FilterInvocationSecurityMetadataSource}.
*
* @author Luke Taylor
* @author Eddú Meléndez
* @since 3.0
*/
public final class ExpressionBasedFilterInvocationSecurityMetadataSource
Expand Down Expand Up @@ -91,13 +91,8 @@ private static LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> proces
return requestToExpressionAttributesMap;
}

private static AbstractVariableEvaluationContextPostProcessor createPostProcessor(
Object request) {
if (request instanceof RequestVariablesExtractor) {
return new RequestVariablesExtractorEvaluationContextPostProcessor(
(RequestVariablesExtractor) request);
}
return null;
private static AbstractVariableEvaluationContextPostProcessor createPostProcessor(RequestMatcher request) {
return new RequestVariablesExtractorEvaluationContextPostProcessor(request);
}

static class AntPathMatcherEvaluationContextPostProcessor
Expand All @@ -111,22 +106,22 @@ public AntPathMatcherEvaluationContextPostProcessor(

@Override
Map<String, String> extractVariables(HttpServletRequest request) {
return this.matcher.extractUriTemplateVariables(request);
return this.matcher.matcher(request).getVariables();
}
}

static class RequestVariablesExtractorEvaluationContextPostProcessor
extends AbstractVariableEvaluationContextPostProcessor {
private final RequestVariablesExtractor matcher;
private final RequestMatcher matcher;

public RequestVariablesExtractorEvaluationContextPostProcessor(
RequestVariablesExtractor matcher) {
RequestMatcher matcher) {
this.matcher = matcher;
}

@Override
Map<String, String> extractVariables(HttpServletRequest request) {
return this.matcher.extractUriTemplateVariables(request);
return this.matcher.matcher(request).getVariables();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2019 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.
Expand All @@ -16,7 +16,6 @@

package org.springframework.security.web.servlet.util.matcher;

import java.util.Collections;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
Expand All @@ -43,6 +42,7 @@
* </p>
*
* @author Rob Winch
* @author Eddú Meléndez
* @since 4.1.1
*/
public class MvcRequestMatcher implements RequestMatcher, RequestVariablesExtractor {
Expand Down Expand Up @@ -92,14 +92,20 @@ private MatchableHandlerMapping getMapping(HttpServletRequest request) {
* extractUriTemplateVariables(javax.servlet.http.HttpServletRequest)
*/
@Override
@Deprecated
public Map<String, String> extractUriTemplateVariables(HttpServletRequest request) {
return matcher(request).getVariables();
}

@Override
public MatchResult matcher(HttpServletRequest request) {
MatchableHandlerMapping mapping = getMapping(request);
if (mapping == null) {
return this.defaultMatcher.extractUriTemplateVariables(request);
return this.defaultMatcher.matcher(request);
}
RequestMatchResult result = mapping.match(request, this.pattern);
return result == null ? Collections.<String, String>emptyMap()
: result.extractUriTemplateVariables();
return result == null ? MatchResult.notMatch()
: MatchResult.match(result.extractUriTemplateVariables());
}

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

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

private final UrlPathHelper pathHelper = new UrlPathHelper();

Expand All @@ -158,14 +164,14 @@ private boolean matches(String lookupPath) {
}

@Override
public Map<String, String> extractUriTemplateVariables(
HttpServletRequest request) {
public MatchResult matcher(HttpServletRequest request) {
String lookupPath = this.pathHelper.getLookupPathForRequest(request);
if (matches(lookupPath)) {
return this.pathMatcher.extractUriTemplateVariables(
Map<String, String> variables = this.pathMatcher.extractUriTemplateVariables(
MvcRequestMatcher.this.pattern, lookupPath);
return MatchResult.match(variables);
}
return Collections.emptyMap();
return MatchResult.notMatch();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
Expand Down Expand Up @@ -49,6 +49,7 @@
*
* @author Luke Taylor
* @author Rob Winch
* @author Eddú Meléndez
* @since 3.1
*
* @see org.springframework.util.AntPathMatcher
Expand Down Expand Up @@ -181,12 +182,18 @@ public boolean matches(HttpServletRequest request) {
}

@Override
@Deprecated
public Map<String, String> extractUriTemplateVariables(HttpServletRequest request) {
return matcher(request).getVariables();
}

@Override
public MatchResult matcher(HttpServletRequest request) {
if (this.matcher == null || !matches(request)) {
return Collections.emptyMap();
return MatchResult.notMatch();
}
String url = getRequestPath(request);
return this.matcher.extractUriTemplateVariables(url);
return MatchResult.match(this.matcher.extractUriTemplateVariables(url));
}

private String getRequestPath(HttpServletRequest request) {
Expand Down Expand Up @@ -258,7 +265,7 @@ private static HttpMethod valueOf(String method) {
return null;
}

private static interface Matcher {
private interface Matcher {
boolean matches(String path);

Map<String, String> extractUriTemplateVariables(String path);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.
Expand All @@ -15,12 +15,16 @@
*/
package org.springframework.security.web.util.matcher;

import java.util.Collections;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
* Simple strategy to match an <tt>HttpServletRequest</tt>.
*
* @author Luke Taylor
* @author Eddú Meléndez
* @since 3.0.2
*/
public interface RequestMatcher {
Expand All @@ -33,4 +37,61 @@ public interface RequestMatcher {
*/
boolean matches(HttpServletRequest request);

/**
* @since 5.2
*/
default MatchResult matcher(HttpServletRequest request) {
boolean match = matches(request);
return new MatchResult(match, Collections.emptyMap());
}

/**
* The result of matching
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add @since here as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Completed

*/
class MatchResult {
private final boolean match;
private final Map<String, String> variables;

MatchResult(boolean match, Map<String, String> variables) {
this.match = match;
this.variables = variables;
}

public boolean isMatch() {
return this.match;
}

public Map<String, String> getVariables() {
return this.variables;
}

/**
* Creates an instance of {@link MatchResult} that is a match with no variables
*
* @return
*/
public static MatchResult match() {
return new MatchResult(true, Collections.emptyMap());
}

/**
* Creates an instance of {@link MatchResult} that is a match with the specified variables
*
* @param variables
* @return
*/
public static MatchResult match(Map<String, String> variables) {
return new MatchResult(true, variables);
}

/**
* Creates an instance of {@link MatchResult} that is not a match.
*
* @return
*/
public static MatchResult notMatch() {
return new MatchResult(false, Collections.emptyMap());
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2019 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.
Expand All @@ -25,6 +25,7 @@
*
* @author Rob Winch
* @since 4.1.1
* @deprecated use {@link RequestMatcher.MatchResult} from {@link RequestMatcher#matcher(HttpServletRequest)}
*/
public interface RequestVariablesExtractor {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2019 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.
Expand Down Expand Up @@ -40,6 +40,7 @@

/**
* @author Rob Winch
* @author Eddú Meléndez
*/
@RunWith(MockitoJUnitRunner.class)
public class MvcRequestMatcherTests {
Expand Down Expand Up @@ -73,6 +74,8 @@ public void extractUriTemplateVariablesSuccess() throws Exception {

assertThat(this.matcher.extractUriTemplateVariables(this.request))
.containsEntry("p", "path");
assertThat(this.matcher.matcher(this.request).getVariables())
.containsEntry("p", "path");
}

@Test
Expand All @@ -85,6 +88,7 @@ public void extractUriTemplateVariablesFail() throws Exception {
.thenReturn(this.result);

assertThat(this.matcher.extractUriTemplateVariables(this.request)).isEmpty();
assertThat(this.matcher.matcher(this.request).getVariables()).isEmpty();
}

@Test
Expand All @@ -94,6 +98,8 @@ public void extractUriTemplateVariablesDefaultSuccess() throws Exception {

assertThat(this.matcher.extractUriTemplateVariables(this.request))
.containsEntry("p", "path");
assertThat(this.matcher.matcher(this.request).getVariables())
.containsEntry("p", "path");
}

@Test
Expand All @@ -102,6 +108,7 @@ public void extractUriTemplateVariablesDefaultFail() throws Exception {
when(this.introspector.getMatchableHandlerMapping(this.request)).thenReturn(null);

assertThat(this.matcher.extractUriTemplateVariables(this.request)).isEmpty();
assertThat(this.matcher.matcher(this.request).getVariables()).isEmpty();
}

@Test
Expand Down