diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationMappingUtils.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationMappingUtils.java index a6c7e41fe8..a091ad199c 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationMappingUtils.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationMappingUtils.java @@ -127,12 +127,21 @@ abstract class ServletAnnotationMappingUtils { } } + if (negated) { + found = !found; + } if (!found) { - return negated; + return false; } } - else if (!value.equals(request.getHeader(key))) { - return negated; + else { + boolean match = value.equals(request.getHeader(key)); + if (negated) { + match = !match; + } + if (!match) { + return false; + } } } } diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationMappingUtilsTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationMappingUtilsTests.java index ca645ec89b..f6b777bd03 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationMappingUtilsTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationMappingUtilsTests.java @@ -143,6 +143,17 @@ public class ServletAnnotationMappingUtilsTests { assertFalse("Invalid request method result", result); } + // SPR-8862 + + @Test + public void checkHeadersKeyValueNoMatchWithNegation() { + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); + request.addHeader("header1", "value1"); + String[] headers = new String[]{"header1!=value1"}; + boolean result = ServletAnnotationMappingUtils.checkHeaders(headers, request); + assertFalse("Invalid request method result", result); + } + @Test public void checkHeadersAcceptMatch() { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); @@ -161,4 +172,13 @@ public class ServletAnnotationMappingUtilsTests { assertFalse("Invalid request method result", result); } + @Test + public void checkHeadersAcceptNoMatchWithNegation() { + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); + request.addHeader("Accept", "application/pdf"); + String[] headers = new String[]{"accept!=application/pdf"}; + boolean result = ServletAnnotationMappingUtils.checkHeaders(headers, request); + assertFalse("Invalid request method result", result); + } + } diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/condition/HeadersRequestConditionTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/condition/HeadersRequestConditionTests.java index 3956ff0659..98de4ce518 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/condition/HeadersRequestConditionTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/condition/HeadersRequestConditionTests.java @@ -111,6 +111,15 @@ public class HeadersRequestConditionTests { assertNotNull(condition.getMatchingCondition(request)); } + @Test + public void headerValueNoMatchNegated() { + HeadersRequestCondition condition = new HeadersRequestCondition("foo!=bar"); + MockHttpServletRequest request = new MockHttpServletRequest(); + request.addHeader("foo", "bar"); + + assertNull(condition.getMatchingCondition(request)); + } + @Test public void compareTo() { MockHttpServletRequest request = new MockHttpServletRequest();