浏览代码

Minor cleanup on Ant / Regex Request Matchers

 - Removed duplicative code for transforming String into HttpMethod
 - Removed an unnecessary array initialization
Nick McKinney 4 年之前
父节点
当前提交
5306d4c4d5

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

@@ -87,7 +87,7 @@ public abstract class AbstractRequestMatcherRegistry<C> {
 	 * @return the object that is chained after creating the {@link RequestMatcher}
 	 * @return the object that is chained after creating the {@link RequestMatcher}
 	 */
 	 */
 	public C antMatchers(HttpMethod method) {
 	public C antMatchers(HttpMethod method) {
-		return antMatchers(method, new String[] { "/**" });
+		return antMatchers(method, "/**");
 	}
 	}
 
 
 	/**
 	/**

+ 1 - 16
web/src/main/java/org/springframework/security/web/util/matcher/AntPathRequestMatcher.java

@@ -141,7 +141,7 @@ public final class AntPathRequestMatcher implements RequestMatcher, RequestVaria
 	@Override
 	@Override
 	public boolean matches(HttpServletRequest request) {
 	public boolean matches(HttpServletRequest request) {
 		if (this.httpMethod != null && StringUtils.hasText(request.getMethod())
 		if (this.httpMethod != null && StringUtils.hasText(request.getMethod())
-				&& this.httpMethod != valueOf(request.getMethod())) {
+				&& this.httpMethod != HttpMethod.resolve(request.getMethod())) {
 			return false;
 			return false;
 		}
 		}
 		if (this.pattern.equals(MATCH_ALL)) {
 		if (this.pattern.equals(MATCH_ALL)) {
@@ -211,21 +211,6 @@ public final class AntPathRequestMatcher implements RequestMatcher, RequestVaria
 		return sb.toString();
 		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 {
 	private interface Matcher {
 
 
 		boolean matches(String path);
 		boolean matches(String path);

+ 2 - 16
web/src/main/java/org/springframework/security/web/util/matcher/RegexRequestMatcher.java

@@ -81,7 +81,8 @@ public final class RegexRequestMatcher implements RequestMatcher {
 	 */
 	 */
 	@Override
 	@Override
 	public boolean matches(HttpServletRequest request) {
 	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;
 			return false;
 		}
 		}
 		String url = request.getServletPath();
 		String url = request.getServletPath();
@@ -101,21 +102,6 @@ public final class RegexRequestMatcher implements RequestMatcher {
 		return this.pattern.matcher(url).matches();
 		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
 	@Override
 	public String toString() {
 	public String toString() {
 		StringBuilder sb = new StringBuilder();
 		StringBuilder sb = new StringBuilder();