Skip to content

Commit ad2abd3

Browse files
Merge branch '5.8.x'
Closes gh-11347 in 6.0.x Closes gh-11945
2 parents ea777a3 + 039e032 commit ad2abd3

File tree

18 files changed

+1391
-38
lines changed

18 files changed

+1391
-38
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/AbstractRequestMatcherRegistry.java

+102-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@
3535
import org.springframework.security.web.util.matcher.RegexRequestMatcher;
3636
import org.springframework.security.web.util.matcher.RequestMatcher;
3737
import org.springframework.util.Assert;
38+
import org.springframework.util.ClassUtils;
3839
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
3940

4041
/**
@@ -50,12 +51,21 @@ public abstract class AbstractRequestMatcherRegistry<C> {
5051

5152
private static final String HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME = "mvcHandlerMappingIntrospector";
5253

54+
private static final String HANDLER_MAPPING_INTROSPECTOR = "org.springframework.web.servlet.handler.HandlerMappingIntrospector";
55+
56+
private static final boolean mvcPresent;
57+
5358
private static final RequestMatcher ANY_REQUEST = AnyRequestMatcher.INSTANCE;
5459

5560
private ApplicationContext context;
5661

5762
private boolean anyRequestConfigured = false;
5863

64+
static {
65+
mvcPresent = ClassUtils.isPresent(HANDLER_MAPPING_INTROSPECTOR,
66+
AbstractRequestMatcherRegistry.class.getClassLoader());
67+
}
68+
5969
protected final void setApplicationContext(ApplicationContext context) {
6070
this.context = context;
6171
}
@@ -85,7 +95,9 @@ public C anyRequest() {
8595
* instances.
8696
* @param method the {@link HttpMethod} to use for any {@link HttpMethod}.
8797
* @return the object that is chained after creating the {@link RequestMatcher}
98+
* @deprecated use {@link #requestMatchers(HttpMethod)} instead
8899
*/
100+
@Deprecated
89101
public C antMatchers(HttpMethod method) {
90102
return antMatchers(method, "/**");
91103
}
@@ -99,7 +111,9 @@ public C antMatchers(HttpMethod method) {
99111
* @param antPatterns the ant patterns to create. If {@code null} or empty, then
100112
* matches on nothing.
101113
* @return the object that is chained after creating the {@link RequestMatcher}
114+
* @deprecated use {@link #requestMatchers(HttpMethod, String...)} instead
102115
*/
116+
@Deprecated
103117
public C antMatchers(HttpMethod method, String... antPatterns) {
104118
Assert.state(!this.anyRequestConfigured, "Can't configure antMatchers after anyRequest");
105119
return chainRequestMatchers(RequestMatchers.antMatchers(method, antPatterns));
@@ -112,7 +126,9 @@ public C antMatchers(HttpMethod method, String... antPatterns) {
112126
* @param antPatterns the ant patterns to create
113127
* {@link org.springframework.security.web.util.matcher.AntPathRequestMatcher} from
114128
* @return the object that is chained after creating the {@link RequestMatcher}
129+
* @deprecated use {@link #requestMatchers(String...)} instead
115130
*/
131+
@Deprecated
116132
public C antMatchers(String... antPatterns) {
117133
Assert.state(!this.anyRequestConfigured, "Can't configure antMatchers after anyRequest");
118134
return chainRequestMatchers(RequestMatchers.antMatchers(antPatterns));
@@ -132,7 +148,9 @@ public C antMatchers(String... antPatterns) {
132148
* @param mvcPatterns the patterns to match on. The rules for matching are defined by
133149
* Spring MVC
134150
* @return the object that is chained after creating the {@link RequestMatcher}.
151+
* @deprecated use {@link #requestMatchers(String...)} instead
135152
*/
153+
@Deprecated
136154
public abstract C mvcMatchers(String... mvcPatterns);
137155

138156
/**
@@ -150,7 +168,9 @@ public C antMatchers(String... antPatterns) {
150168
* @param mvcPatterns the patterns to match on. The rules for matching are defined by
151169
* Spring MVC
152170
* @return the object that is chained after creating the {@link RequestMatcher}.
171+
* @deprecated use {@link #requestMatchers(HttpMethod, String...)} instead
153172
*/
173+
@Deprecated
154174
public abstract C mvcMatchers(HttpMethod method, String... mvcPatterns);
155175

156176
/**
@@ -190,7 +210,10 @@ protected final List<MvcRequestMatcher> createMvcMatchers(HttpMethod method, Str
190210
* @param regexPatterns the regular expressions to create
191211
* {@link org.springframework.security.web.util.matcher.RegexRequestMatcher} from
192212
* @return the object that is chained after creating the {@link RequestMatcher}
213+
* @deprecated use {@link #requestMatchers(RequestMatcher...)} with a
214+
* {@link RegexRequestMatcher} instead
193215
*/
216+
@Deprecated
194217
public C regexMatchers(HttpMethod method, String... regexPatterns) {
195218
Assert.state(!this.anyRequestConfigured, "Can't configure regexMatchers after anyRequest");
196219
return chainRequestMatchers(RequestMatchers.regexMatchers(method, regexPatterns));
@@ -203,7 +226,10 @@ public C regexMatchers(HttpMethod method, String... regexPatterns) {
203226
* @param regexPatterns the regular expressions to create
204227
* {@link org.springframework.security.web.util.matcher.RegexRequestMatcher} from
205228
* @return the object that is chained after creating the {@link RequestMatcher}
229+
* @deprecated use {@link #requestMatchers(RequestMatcher...)} with a
230+
* {@link RegexRequestMatcher} instead
206231
*/
232+
@Deprecated
207233
public C regexMatchers(String... regexPatterns) {
208234
Assert.state(!this.anyRequestConfigured, "Can't configure regexMatchers after anyRequest");
209235
return chainRequestMatchers(RequestMatchers.regexMatchers(regexPatterns));
@@ -250,6 +276,81 @@ public C requestMatchers(RequestMatcher... requestMatchers) {
250276
return chainRequestMatchers(Arrays.asList(requestMatchers));
251277
}
252278

279+
/**
280+
* <p>
281+
* If the {@link HandlerMappingIntrospector} is available in the classpath, maps to an
282+
* {@link MvcRequestMatcher} that also specifies a specific {@link HttpMethod} to
283+
* match on. This matcher will use the same rules that Spring MVC uses for matching.
284+
* For example, often times a mapping of the path "/path" will match on "/path",
285+
* "/path/", "/path.html", etc. If the {@link HandlerMappingIntrospector} is not
286+
* available, maps to an {@link AntPathRequestMatcher}.
287+
* </p>
288+
* <p>
289+
* If a specific {@link RequestMatcher} must be specified, use
290+
* {@link #requestMatchers(RequestMatcher...)} instead
291+
* </p>
292+
* @param method the {@link HttpMethod} to use or {@code null} for any
293+
* {@link HttpMethod}.
294+
* @param patterns the patterns to match on. The rules for matching are defined by
295+
* Spring MVC if {@link MvcRequestMatcher} is used
296+
* @return the object that is chained after creating the {@link RequestMatcher}.
297+
* @since 5.8
298+
*/
299+
public C requestMatchers(HttpMethod method, String... patterns) {
300+
List<RequestMatcher> matchers = new ArrayList<>();
301+
if (mvcPresent) {
302+
matchers.addAll(createMvcMatchers(method, patterns));
303+
}
304+
else {
305+
matchers.addAll(RequestMatchers.antMatchers(method, patterns));
306+
}
307+
return requestMatchers(matchers.toArray(new RequestMatcher[0]));
308+
}
309+
310+
/**
311+
* <p>
312+
* If the {@link HandlerMappingIntrospector} is available in the classpath, maps to an
313+
* {@link MvcRequestMatcher} that does not care which {@link HttpMethod} is used. This
314+
* matcher will use the same rules that Spring MVC uses for matching. For example,
315+
* often times a mapping of the path "/path" will match on "/path", "/path/",
316+
* "/path.html", etc. If the {@link HandlerMappingIntrospector} is not available, maps
317+
* to an {@link AntPathRequestMatcher}.
318+
* </p>
319+
* <p>
320+
* If a specific {@link RequestMatcher} must be specified, use
321+
* {@link #requestMatchers(RequestMatcher...)} instead
322+
* </p>
323+
* @param patterns the patterns to match on. The rules for matching are defined by
324+
* Spring MVC if {@link MvcRequestMatcher} is used
325+
* @return the object that is chained after creating the {@link RequestMatcher}.
326+
* @since 5.8
327+
*/
328+
public C requestMatchers(String... patterns) {
329+
return requestMatchers(null, patterns);
330+
}
331+
332+
/**
333+
* <p>
334+
* If the {@link HandlerMappingIntrospector} is available in the classpath, maps to an
335+
* {@link MvcRequestMatcher} that matches on a specific {@link HttpMethod}. This
336+
* matcher will use the same rules that Spring MVC uses for matching. For example,
337+
* often times a mapping of the path "/path" will match on "/path", "/path/",
338+
* "/path.html", etc. If the {@link HandlerMappingIntrospector} is not available, maps
339+
* to an {@link AntPathRequestMatcher}.
340+
* </p>
341+
* <p>
342+
* If a specific {@link RequestMatcher} must be specified, use
343+
* {@link #requestMatchers(RequestMatcher...)} instead
344+
* </p>
345+
* @param method the {@link HttpMethod} to use or {@code null} for any
346+
* {@link HttpMethod}.
347+
* @return the object that is chained after creating the {@link RequestMatcher}.
348+
* @since 5.8
349+
*/
350+
public C requestMatchers(HttpMethod method) {
351+
return requestMatchers(method, "/**");
352+
}
353+
253354
/**
254355
* Subclasses should implement this method for returning the object that is chained to
255356
* the creation of the {@link RequestMatcher} instances.

0 commit comments

Comments
 (0)