1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
35
35
import org .springframework .security .web .util .matcher .RegexRequestMatcher ;
36
36
import org .springframework .security .web .util .matcher .RequestMatcher ;
37
37
import org .springframework .util .Assert ;
38
+ import org .springframework .util .ClassUtils ;
38
39
import org .springframework .web .servlet .handler .HandlerMappingIntrospector ;
39
40
40
41
/**
@@ -50,12 +51,21 @@ public abstract class AbstractRequestMatcherRegistry<C> {
50
51
51
52
private static final String HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME = "mvcHandlerMappingIntrospector" ;
52
53
54
+ private static final String HANDLER_MAPPING_INTROSPECTOR = "org.springframework.web.servlet.handler.HandlerMappingIntrospector" ;
55
+
56
+ private static final boolean mvcPresent ;
57
+
53
58
private static final RequestMatcher ANY_REQUEST = AnyRequestMatcher .INSTANCE ;
54
59
55
60
private ApplicationContext context ;
56
61
57
62
private boolean anyRequestConfigured = false ;
58
63
64
+ static {
65
+ mvcPresent = ClassUtils .isPresent (HANDLER_MAPPING_INTROSPECTOR ,
66
+ AbstractRequestMatcherRegistry .class .getClassLoader ());
67
+ }
68
+
59
69
protected final void setApplicationContext (ApplicationContext context ) {
60
70
this .context = context ;
61
71
}
@@ -85,7 +95,9 @@ public C anyRequest() {
85
95
* instances.
86
96
* @param method the {@link HttpMethod} to use for any {@link HttpMethod}.
87
97
* @return the object that is chained after creating the {@link RequestMatcher}
98
+ * @deprecated use {@link #requestMatchers(HttpMethod)} instead
88
99
*/
100
+ @ Deprecated
89
101
public C antMatchers (HttpMethod method ) {
90
102
return antMatchers (method , "/**" );
91
103
}
@@ -99,7 +111,9 @@ public C antMatchers(HttpMethod method) {
99
111
* @param antPatterns the ant patterns to create. If {@code null} or empty, then
100
112
* matches on nothing.
101
113
* @return the object that is chained after creating the {@link RequestMatcher}
114
+ * @deprecated use {@link #requestMatchers(HttpMethod, String...)} instead
102
115
*/
116
+ @ Deprecated
103
117
public C antMatchers (HttpMethod method , String ... antPatterns ) {
104
118
Assert .state (!this .anyRequestConfigured , "Can't configure antMatchers after anyRequest" );
105
119
return chainRequestMatchers (RequestMatchers .antMatchers (method , antPatterns ));
@@ -112,7 +126,9 @@ public C antMatchers(HttpMethod method, String... antPatterns) {
112
126
* @param antPatterns the ant patterns to create
113
127
* {@link org.springframework.security.web.util.matcher.AntPathRequestMatcher} from
114
128
* @return the object that is chained after creating the {@link RequestMatcher}
129
+ * @deprecated use {@link #requestMatchers(String...)} instead
115
130
*/
131
+ @ Deprecated
116
132
public C antMatchers (String ... antPatterns ) {
117
133
Assert .state (!this .anyRequestConfigured , "Can't configure antMatchers after anyRequest" );
118
134
return chainRequestMatchers (RequestMatchers .antMatchers (antPatterns ));
@@ -132,7 +148,9 @@ public C antMatchers(String... antPatterns) {
132
148
* @param mvcPatterns the patterns to match on. The rules for matching are defined by
133
149
* Spring MVC
134
150
* @return the object that is chained after creating the {@link RequestMatcher}.
151
+ * @deprecated use {@link #requestMatchers(String...)} instead
135
152
*/
153
+ @ Deprecated
136
154
public abstract C mvcMatchers (String ... mvcPatterns );
137
155
138
156
/**
@@ -150,7 +168,9 @@ public C antMatchers(String... antPatterns) {
150
168
* @param mvcPatterns the patterns to match on. The rules for matching are defined by
151
169
* Spring MVC
152
170
* @return the object that is chained after creating the {@link RequestMatcher}.
171
+ * @deprecated use {@link #requestMatchers(HttpMethod, String...)} instead
153
172
*/
173
+ @ Deprecated
154
174
public abstract C mvcMatchers (HttpMethod method , String ... mvcPatterns );
155
175
156
176
/**
@@ -190,7 +210,10 @@ protected final List<MvcRequestMatcher> createMvcMatchers(HttpMethod method, Str
190
210
* @param regexPatterns the regular expressions to create
191
211
* {@link org.springframework.security.web.util.matcher.RegexRequestMatcher} from
192
212
* @return the object that is chained after creating the {@link RequestMatcher}
213
+ * @deprecated use {@link #requestMatchers(RequestMatcher...)} with a
214
+ * {@link RegexRequestMatcher} instead
193
215
*/
216
+ @ Deprecated
194
217
public C regexMatchers (HttpMethod method , String ... regexPatterns ) {
195
218
Assert .state (!this .anyRequestConfigured , "Can't configure regexMatchers after anyRequest" );
196
219
return chainRequestMatchers (RequestMatchers .regexMatchers (method , regexPatterns ));
@@ -203,7 +226,10 @@ public C regexMatchers(HttpMethod method, String... regexPatterns) {
203
226
* @param regexPatterns the regular expressions to create
204
227
* {@link org.springframework.security.web.util.matcher.RegexRequestMatcher} from
205
228
* @return the object that is chained after creating the {@link RequestMatcher}
229
+ * @deprecated use {@link #requestMatchers(RequestMatcher...)} with a
230
+ * {@link RegexRequestMatcher} instead
206
231
*/
232
+ @ Deprecated
207
233
public C regexMatchers (String ... regexPatterns ) {
208
234
Assert .state (!this .anyRequestConfigured , "Can't configure regexMatchers after anyRequest" );
209
235
return chainRequestMatchers (RequestMatchers .regexMatchers (regexPatterns ));
@@ -250,6 +276,81 @@ public C requestMatchers(RequestMatcher... requestMatchers) {
250
276
return chainRequestMatchers (Arrays .asList (requestMatchers ));
251
277
}
252
278
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
+
253
354
/**
254
355
* Subclasses should implement this method for returning the object that is chained to
255
356
* the creation of the {@link RequestMatcher} instances.
0 commit comments