@@ -166,15 +166,18 @@ public String toString() {
166
166
* a {@link PathPatternRequestMatcher}.
167
167
*
168
168
* <p>
169
- * For example, if Spring MVC is deployed to `/mvc` and another servlet to `/other`,
170
- * then you can use this builder to do:
171
- * </p>
169
+ * To match a request URI like {@code /app/ servlet/my/resource/**} where {@code /app}
170
+ * is the context path, you can do
171
+ * {@code PathPatternRequestMatcher.withDefaults().matcher("/servlet/my/resource/**")}
172
172
*
173
- * <code>
173
+ * <p>
174
+ * If you have many paths that have a common path prefix, you can use
175
+ * {@link #basePath} to reduce repetition like so: <code>
176
+ * PathPatternRequestMatcher.Builder mvc = withDefaults().basePath("/mvc");
174
177
* http
175
178
* .authorizeHttpRequests((authorize) -> authorize
176
- * .requestMatchers(servletPath("/ mvc") .matcher("/user/**")).hasAuthority("user")
177
- * .requestMatchers(servletPath("/other") .matcher("/admin/**")).hasAuthority("admin")
179
+ * .requestMatchers(mvc.matcher("/user/**")).hasAuthority("user")
180
+ * .requestMatchers(mvc .matcher("/admin/**")).hasAuthority("admin")
178
181
* )
179
182
* ...
180
183
* </code>
@@ -183,7 +186,7 @@ public static final class Builder {
183
186
184
187
private final PathPatternParser parser ;
185
188
186
- private final String servletPath ;
189
+ private final String basePath ;
187
190
188
191
Builder () {
189
192
this (PathPatternParser .defaultInstance );
@@ -193,22 +196,26 @@ public static final class Builder {
193
196
this (parser , "" );
194
197
}
195
198
196
- Builder (PathPatternParser parser , String servletPath ) {
199
+ Builder (PathPatternParser parser , String basePath ) {
197
200
this .parser = parser ;
198
- this .servletPath = servletPath ;
201
+ this .basePath = basePath ;
199
202
}
200
203
201
204
/**
202
- * Match requests starting with this {@code servletPath}.
203
- * @param servletPath the servlet path prefix
205
+ * Match requests starting with this {@code basePath}.
206
+ *
207
+ * <p>
208
+ * Prefixes should be of the form {@code /my/prefix}, starting with a slash, not
209
+ * ending in a slash, and not containing and wildcards
210
+ * @param basePath the path prefix
204
211
* @return the {@link Builder} for more configuration
205
212
*/
206
- public Builder servletPath (String servletPath ) {
207
- Assert .notNull (servletPath , "servletPath cannot be null" );
208
- Assert .isTrue (servletPath .startsWith ("/" ), "servletPath must start with '/'" );
209
- Assert .isTrue (!servletPath .endsWith ("/" ), "servletPath must not end with a slash" );
210
- Assert .isTrue (!servletPath .contains ("*" ), "servletPath must not contain a star" );
211
- return new Builder (this .parser , servletPath );
213
+ public Builder basePath (String basePath ) {
214
+ Assert .notNull (basePath , "basePath cannot be null" );
215
+ Assert .isTrue (basePath .startsWith ("/" ), "basePath must start with '/'" );
216
+ Assert .isTrue (!basePath .endsWith ("/" ), "basePath must not end with a slash" );
217
+ Assert .isTrue (!basePath .contains ("*" ), "basePath must not contain a star" );
218
+ return new Builder (this .parser , basePath );
212
219
}
213
220
214
221
/**
@@ -279,7 +286,7 @@ public PathPatternRequestMatcher matcher(String path) {
279
286
public PathPatternRequestMatcher matcher (@ Nullable HttpMethod method , String path ) {
280
287
Assert .notNull (path , "pattern cannot be null" );
281
288
Assert .isTrue (path .startsWith ("/" ), "pattern must start with a /" );
282
- PathPattern pathPattern = this .parser .parse (this .servletPath + path );
289
+ PathPattern pathPattern = this .parser .parse (this .basePath + path );
283
290
PathPatternRequestMatcher requestMatcher = new PathPatternRequestMatcher (pathPattern );
284
291
if (method != null ) {
285
292
requestMatcher .setMethod (new HttpMethodRequestMatcher (method ));
0 commit comments