|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.web.servlet.util.matcher; |
| 18 | + |
| 19 | +import java.util.Objects; |
| 20 | + |
| 21 | +import jakarta.servlet.http.HttpServletRequest; |
| 22 | + |
| 23 | +import org.springframework.http.HttpMethod; |
| 24 | +import org.springframework.http.server.PathContainer; |
| 25 | +import org.springframework.http.server.RequestPath; |
| 26 | +import org.springframework.security.web.util.matcher.RequestMatcher; |
| 27 | +import org.springframework.security.web.util.matcher.RequestMatcherBuilder; |
| 28 | +import org.springframework.util.Assert; |
| 29 | +import org.springframework.web.util.WebUtils; |
| 30 | +import org.springframework.web.util.pattern.PathPattern; |
| 31 | +import org.springframework.web.util.pattern.PathPatternParser; |
| 32 | + |
| 33 | +/** |
| 34 | + * A {@link RequestMatcher} that uses {@link PathPattern}s to match against each |
| 35 | + * {@link HttpServletRequest}. Specifically, this means that the class anticipates that |
| 36 | + * the provided pattern does not include the servlet path in order to align with Spring |
| 37 | + * MVC. |
| 38 | + * |
| 39 | + * <p> |
| 40 | + * Note that the {@link org.springframework.web.servlet.HandlerMapping} that contains the |
| 41 | + * related URI patterns must be using the same |
| 42 | + * {@link org.springframework.web.util.pattern.PathPatternParser} configured in this |
| 43 | + * class. |
| 44 | + * </p> |
| 45 | + * |
| 46 | + * @author Josh Cummings |
| 47 | + * @since 6.5 |
| 48 | + */ |
| 49 | +public final class PathPatternRequestMatcher implements RequestMatcher { |
| 50 | + |
| 51 | + private HttpMethod method; |
| 52 | + |
| 53 | + private final PathPattern pattern; |
| 54 | + |
| 55 | + PathPatternRequestMatcher(PathPattern pattern) { |
| 56 | + this.pattern = pattern; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Create a {@link Builder} for creating {@link PathPattern}-based request matchers. |
| 61 | + * That is, matchers that anticipate patterns do not specify the servlet path. |
| 62 | + * @return the {@link Builder} |
| 63 | + */ |
| 64 | + public static Builder builder() { |
| 65 | + return new Builder(PathPatternParser.defaultInstance); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Create a {@link Builder} for creating {@link PathPattern}-based request matchers. |
| 70 | + * That is, matchers that anticipate patterns do not specify the servlet path. |
| 71 | + * @param parser the {@link PathPatternParser}; only needed when different from |
| 72 | + * {@link PathPatternParser#defaultInstance} |
| 73 | + * @return the {@link Builder} |
| 74 | + */ |
| 75 | + public static Builder withPathPatternParser(PathPatternParser parser) { |
| 76 | + return new Builder(parser); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * {@inheritDoc} |
| 81 | + */ |
| 82 | + @Override |
| 83 | + public boolean matches(HttpServletRequest request) { |
| 84 | + return matcher(request).isMatch(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * {@inheritDoc} |
| 89 | + */ |
| 90 | + @Override |
| 91 | + public MatchResult matcher(HttpServletRequest request) { |
| 92 | + if (this.method != null && !this.method.name().equals(request.getMethod())) { |
| 93 | + return MatchResult.notMatch(); |
| 94 | + } |
| 95 | + PathContainer path = getRequestPath(request).pathWithinApplication(); |
| 96 | + PathPattern.PathMatchInfo info = this.pattern.matchAndExtract(path); |
| 97 | + return (info != null) ? MatchResult.match(info.getUriVariables()) : MatchResult.notMatch(); |
| 98 | + } |
| 99 | + |
| 100 | + void setMethod(HttpMethod method) { |
| 101 | + this.method = method; |
| 102 | + } |
| 103 | + |
| 104 | + private RequestPath getRequestPath(HttpServletRequest request) { |
| 105 | + String requestUri = (String) request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE); |
| 106 | + requestUri = (requestUri != null) ? requestUri : request.getRequestURI(); |
| 107 | + return RequestPath.parse(requestUri, request.getContextPath()); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * {@inheritDoc} |
| 112 | + */ |
| 113 | + @Override |
| 114 | + public boolean equals(Object o) { |
| 115 | + if (!(o instanceof PathPatternRequestMatcher that)) { |
| 116 | + return false; |
| 117 | + } |
| 118 | + return Objects.equals(this.method, that.method) && Objects.equals(this.pattern, that.pattern); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * {@inheritDoc} |
| 123 | + */ |
| 124 | + @Override |
| 125 | + public int hashCode() { |
| 126 | + return Objects.hash(this.method, this.pattern); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * {@inheritDoc} |
| 131 | + */ |
| 132 | + @Override |
| 133 | + public String toString() { |
| 134 | + return "PathPatternRequestMatcher [method=" + this.method + ", pattern=" + this.pattern + "]"; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * A builder for {@link PathPatternRequestMatcher} |
| 139 | + * |
| 140 | + * @author Marcus Da Coregio |
| 141 | + * @since 6.5 |
| 142 | + */ |
| 143 | + public static final class Builder implements RequestMatcherBuilder { |
| 144 | + |
| 145 | + private final PathPatternParser parser; |
| 146 | + |
| 147 | + /** |
| 148 | + * Construct a new instance of this builder |
| 149 | + */ |
| 150 | + public Builder(PathPatternParser parser) { |
| 151 | + Assert.notNull(parser, "pathPatternParser cannot be null"); |
| 152 | + this.parser = parser; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Creates an {@link PathPatternRequestMatcher} that uses the provided |
| 157 | + * {@code pattern} and HTTP {@code method} to match. |
| 158 | + * <p> |
| 159 | + * If the {@code pattern} is a path, it must be specified relative to its servlet |
| 160 | + * path. |
| 161 | + * </p> |
| 162 | + * @param method the {@link HttpMethod}, can be null |
| 163 | + * @param pattern the pattern used to match; if a path, must be relative to its |
| 164 | + * servlet path |
| 165 | + * @return the generated {@link PathPatternRequestMatcher} |
| 166 | + */ |
| 167 | + public PathPatternRequestMatcher pattern(HttpMethod method, String pattern) { |
| 168 | + String parsed = this.parser.initFullPathPattern(pattern); |
| 169 | + PathPattern pathPattern = this.parser.parse(parsed); |
| 170 | + PathPatternRequestMatcher requestMatcher = new PathPatternRequestMatcher(pathPattern); |
| 171 | + requestMatcher.setMethod(method); |
| 172 | + return requestMatcher; |
| 173 | + } |
| 174 | + |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments