Skip to content

Commit b432530

Browse files
committed
Minor cleanup on Ant / Regex Request Matchers
- Removed duplicative code for transforming String into HttpMethod - Removed an unnecessary array initialization
1 parent 0838d4d commit b432530

File tree

3 files changed

+4
-33
lines changed

3 files changed

+4
-33
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public C anyRequest() {
8787
* @return the object that is chained after creating the {@link RequestMatcher}
8888
*/
8989
public C antMatchers(HttpMethod method) {
90-
return antMatchers(method, new String[] { "/**" });
90+
return antMatchers(method, "/**");
9191
}
9292

9393
/**

web/src/main/java/org/springframework/security/web/util/matcher/AntPathRequestMatcher.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public AntPathRequestMatcher(String pattern, String httpMethod, boolean caseSens
141141
@Override
142142
public boolean matches(HttpServletRequest request) {
143143
if (this.httpMethod != null && StringUtils.hasText(request.getMethod())
144-
&& this.httpMethod != valueOf(request.getMethod())) {
144+
&& this.httpMethod != HttpMethod.resolve(request.getMethod())) {
145145
return false;
146146
}
147147
if (this.pattern.equals(MATCH_ALL)) {
@@ -211,21 +211,6 @@ public String toString() {
211211
return sb.toString();
212212
}
213213

214-
/**
215-
* Provides a save way of obtaining the HttpMethod from a String. If the method is
216-
* invalid, returns null.
217-
* @param method the HTTP method to use.
218-
* @return the HttpMethod or null if method is invalid.
219-
*/
220-
private static HttpMethod valueOf(String method) {
221-
try {
222-
return HttpMethod.valueOf(method);
223-
}
224-
catch (IllegalArgumentException ex) {
225-
return null;
226-
}
227-
}
228-
229214
private interface Matcher {
230215

231216
boolean matches(String path);

web/src/main/java/org/springframework/security/web/util/matcher/RegexRequestMatcher.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public RegexRequestMatcher(String pattern, String httpMethod, boolean caseInsens
8181
*/
8282
@Override
8383
public boolean matches(HttpServletRequest request) {
84-
if (this.httpMethod != null && request.getMethod() != null && this.httpMethod != valueOf(request.getMethod())) {
84+
if (this.httpMethod != null && request.getMethod() != null
85+
&& this.httpMethod != HttpMethod.resolve(request.getMethod())) {
8586
return false;
8687
}
8788
String url = request.getServletPath();
@@ -101,21 +102,6 @@ public boolean matches(HttpServletRequest request) {
101102
return this.pattern.matcher(url).matches();
102103
}
103104

104-
/**
105-
* Provides a save way of obtaining the HttpMethod from a String. If the method is
106-
* invalid, returns null.
107-
* @param method the HTTP method to use.
108-
* @return the HttpMethod or null if method is invalid.
109-
*/
110-
private static HttpMethod valueOf(String method) {
111-
try {
112-
return HttpMethod.valueOf(method);
113-
}
114-
catch (IllegalArgumentException ex) {
115-
return null;
116-
}
117-
}
118-
119105
@Override
120106
public String toString() {
121107
StringBuilder sb = new StringBuilder();

0 commit comments

Comments
 (0)