Skip to content

Add match result for servlet requests #7150

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 @@ -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 @@ -40,6 +40,7 @@
* Expression-based {@code FilterInvocationSecurityMetadataSource}.
*
* @author Luke Taylor
* @author Eddú Meléndez
* @since 3.0
*/
public final class ExpressionBasedFilterInvocationSecurityMetadataSource
Expand Down Expand Up @@ -111,7 +112,7 @@ public AntPathMatcherEvaluationContextPostProcessor(

@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 @@ -93,13 +93,18 @@ private MatchableHandlerMapping getMapping(HttpServletRequest request) {
*/
@Override
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 @@ -160,12 +165,18 @@ private boolean matches(String lookupPath) {
@Override
public Map<String, String> extractUriTemplateVariables(
HttpServletRequest request) {
return matcher(request).getVariables();
}

@Override
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 @@ -182,11 +183,16 @@ public boolean matches(HttpServletRequest request) {

@Override
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
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
*/
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
*/
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