Skip to content

Commit 8a6e129

Browse files
committed
Add Warning Message for Missing Leading Slashes
Closes gh-16020
1 parent 1d32263 commit 8a6e129

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ public C requestMatchers(RequestMatcher... requestMatchers) {
199199
* @since 5.8
200200
*/
201201
public C requestMatchers(HttpMethod method, String... patterns) {
202+
if (anyPathsDontStartWithLeadingSlash(patterns)) {
203+
this.logger.warn("One of the patterns in " + Arrays.toString(patterns)
204+
+ " is missing a leading slash. This is discouraged; please include the "
205+
+ "leading slash in all your request matcher patterns. In future versions of "
206+
+ "Spring Security, leaving out the leading slash will result in an exception.");
207+
}
202208
if (!mvcPresent) {
203209
return requestMatchers(RequestMatchers.antMatchersAsArray(method, patterns));
204210
}
@@ -219,6 +225,15 @@ public C requestMatchers(HttpMethod method, String... patterns) {
219225
return requestMatchers(matchers.toArray(new RequestMatcher[0]));
220226
}
221227

228+
private boolean anyPathsDontStartWithLeadingSlash(String... patterns) {
229+
for (String pattern : patterns) {
230+
if (!pattern.startsWith("/")) {
231+
return true;
232+
}
233+
}
234+
return false;
235+
}
236+
222237
private RequestMatcher resolve(AntPathRequestMatcher ant, MvcRequestMatcher mvc, ServletContext servletContext) {
223238
Map<String, ? extends ServletRegistration> registrations = mappableServletRegistrations(servletContext);
224239
if (registrations.isEmpty()) {

0 commit comments

Comments
 (0)