From 5306d4c4d599a41778be2d2f348cccd5aa83ca01 Mon Sep 17 00:00:00 2001 From: Nick McKinney Date: Fri, 11 Dec 2020 17:40:55 -0500 Subject: [PATCH] Minor cleanup on Ant / Regex Request Matchers - Removed duplicative code for transforming String into HttpMethod - Removed an unnecessary array initialization --- .../web/AbstractRequestMatcherRegistry.java | 2 +- .../util/matcher/AntPathRequestMatcher.java | 17 +---------------- .../web/util/matcher/RegexRequestMatcher.java | 18 ++---------------- 3 files changed, 4 insertions(+), 33 deletions(-) diff --git a/config/src/main/java/org/springframework/security/config/annotation/web/AbstractRequestMatcherRegistry.java b/config/src/main/java/org/springframework/security/config/annotation/web/AbstractRequestMatcherRegistry.java index 284769d75f..4f2135a346 100644 --- a/config/src/main/java/org/springframework/security/config/annotation/web/AbstractRequestMatcherRegistry.java +++ b/config/src/main/java/org/springframework/security/config/annotation/web/AbstractRequestMatcherRegistry.java @@ -87,7 +87,7 @@ public abstract class AbstractRequestMatcherRegistry { * @return the object that is chained after creating the {@link RequestMatcher} */ public C antMatchers(HttpMethod method) { - return antMatchers(method, new String[] { "/**" }); + return antMatchers(method, "/**"); } /** diff --git a/web/src/main/java/org/springframework/security/web/util/matcher/AntPathRequestMatcher.java b/web/src/main/java/org/springframework/security/web/util/matcher/AntPathRequestMatcher.java index 9e2968a03f..8e66cceba7 100644 --- a/web/src/main/java/org/springframework/security/web/util/matcher/AntPathRequestMatcher.java +++ b/web/src/main/java/org/springframework/security/web/util/matcher/AntPathRequestMatcher.java @@ -141,7 +141,7 @@ public final class AntPathRequestMatcher implements RequestMatcher, RequestVaria @Override public boolean matches(HttpServletRequest request) { if (this.httpMethod != null && StringUtils.hasText(request.getMethod()) - && this.httpMethod != valueOf(request.getMethod())) { + && this.httpMethod != HttpMethod.resolve(request.getMethod())) { return false; } if (this.pattern.equals(MATCH_ALL)) { @@ -211,21 +211,6 @@ public final class AntPathRequestMatcher implements RequestMatcher, RequestVaria return sb.toString(); } - /** - * Provides a save way of obtaining the HttpMethod from a String. If the method is - * invalid, returns null. - * @param method the HTTP method to use. - * @return the HttpMethod or null if method is invalid. - */ - private static HttpMethod valueOf(String method) { - try { - return HttpMethod.valueOf(method); - } - catch (IllegalArgumentException ex) { - return null; - } - } - private interface Matcher { boolean matches(String path); diff --git a/web/src/main/java/org/springframework/security/web/util/matcher/RegexRequestMatcher.java b/web/src/main/java/org/springframework/security/web/util/matcher/RegexRequestMatcher.java index 6d0bc19a1a..9264b56f21 100644 --- a/web/src/main/java/org/springframework/security/web/util/matcher/RegexRequestMatcher.java +++ b/web/src/main/java/org/springframework/security/web/util/matcher/RegexRequestMatcher.java @@ -81,7 +81,8 @@ public final class RegexRequestMatcher implements RequestMatcher { */ @Override public boolean matches(HttpServletRequest request) { - if (this.httpMethod != null && request.getMethod() != null && this.httpMethod != valueOf(request.getMethod())) { + if (this.httpMethod != null && request.getMethod() != null + && this.httpMethod != HttpMethod.resolve(request.getMethod())) { return false; } String url = request.getServletPath(); @@ -101,21 +102,6 @@ public final class RegexRequestMatcher implements RequestMatcher { return this.pattern.matcher(url).matches(); } - /** - * Provides a save way of obtaining the HttpMethod from a String. If the method is - * invalid, returns null. - * @param method the HTTP method to use. - * @return the HttpMethod or null if method is invalid. - */ - private static HttpMethod valueOf(String method) { - try { - return HttpMethod.valueOf(method); - } - catch (IllegalArgumentException ex) { - return null; - } - } - @Override public String toString() { StringBuilder sb = new StringBuilder();