diff --git a/config/src/main/java/org/springframework/security/config/annotation/web/configurers/CsrfConfigurer.java b/config/src/main/java/org/springframework/security/config/annotation/web/configurers/CsrfConfigurer.java
index a1143cc3d5..9529703af6 100644
--- a/config/src/main/java/org/springframework/security/config/annotation/web/configurers/CsrfConfigurer.java
+++ b/config/src/main/java/org/springframework/security/config/annotation/web/configurers/CsrfConfigurer.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2013 the original author or authors.
+ * Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -128,7 +128,7 @@ public final class CsrfConfigurer>
*
*
*
- * The following will ensure CSRF protection ignores:
+ * For example, the following configuration will ensure CSRF protection ignores:
*
*
* - Any GET, HEAD, TRACE, OPTIONS (this is the default)
@@ -150,6 +150,35 @@ public final class CsrfConfigurer>
.and();
}
+ /**
+ *
+ * Allows specifying {@link HttpServletRequest}s that should not use CSRF Protection
+ * even if they match the {@link #requireCsrfProtectionMatcher(RequestMatcher)}.
+ *
+ *
+ *
+ * For example, the following configuration will ensure CSRF protection ignores:
+ *
+ *
+ * - Any GET, HEAD, TRACE, OPTIONS (this is the default)
+ * - We also explicitly state to ignore any request that has a "X-Requested-With: XMLHttpRequest" header
+ *
+ *
+ *
+ * http
+ * .csrf()
+ * .ignoringRequestMatchers(request -> "XMLHttpRequest".equals(request.getHeader("X-Requested-With")))
+ * .and()
+ * ...
+ *
+ *
+ * @since 5.1
+ */
+ public CsrfConfigurer ignoringRequestMatchers(RequestMatcher... requestMatchers) {
+ return new IgnoreCsrfProtectionRegistry(this.context).requestMatchers(requestMatchers)
+ .and();
+ }
+
@SuppressWarnings("unchecked")
@Override
public void configure(H http) throws Exception {
diff --git a/config/src/test/java/org/springframework/security/config/annotation/web/configurers/CsrfConfigurerIgnoringRequestMatchersTests.java b/config/src/test/java/org/springframework/security/config/annotation/web/configurers/CsrfConfigurerIgnoringRequestMatchersTests.java
new file mode 100644
index 0000000000..75f04401d0
--- /dev/null
+++ b/config/src/test/java/org/springframework/security/config/annotation/web/configurers/CsrfConfigurerIgnoringRequestMatchersTests.java
@@ -0,0 +1,127 @@
+/*
+ * Copyright 2002-2018 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.security.config.annotation.web.configurers;
+
+import org.junit.Rule;
+import org.junit.Test;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpMethod;
+import org.springframework.security.config.annotation.web.builders.HttpSecurity;
+import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
+import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
+import org.springframework.security.config.test.SpringTestRule;
+import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
+import org.springframework.security.web.util.matcher.RequestMatcher;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+/**
+ * @author Josh Cummings
+ */
+public class CsrfConfigurerIgnoringRequestMatchersTests {
+
+ @Autowired
+ MockMvc mvc;
+
+ @Rule
+ public final SpringTestRule spring = new SpringTestRule();
+
+ @Test
+ public void requestWhenIgnoringRequestMatchersThenAugmentedByConfiguredRequestMatcher()
+ throws Exception {
+ this.spring.register(IgnoringRequestMatchers.class, BasicController.class).autowire();
+
+ this.mvc.perform(get("/path"))
+ .andExpect(status().isForbidden());
+
+ this.mvc.perform(post("/path"))
+ .andExpect(status().isOk());
+ }
+
+ @EnableWebSecurity
+ static class IgnoringRequestMatchers extends WebSecurityConfigurerAdapter {
+ RequestMatcher requestMatcher =
+ request -> HttpMethod.POST.name().equals(request.getMethod());
+
+ @Override
+ protected void configure(HttpSecurity http) throws Exception {
+ // @formatter:off
+ http
+ .csrf()
+ .requireCsrfProtectionMatcher(new AntPathRequestMatcher("/path"))
+ .ignoringRequestMatchers(this.requestMatcher);
+ // @formatter:on
+ }
+ }
+
+ @Test
+ public void requestWhenIgnoringRequestMatcherThenUnionsWithConfiguredIgnoringAntMatchers()
+ throws Exception {
+
+ this.spring.register(IgnoringPathsAndMatchers.class, BasicController.class).autowire();
+
+ this.mvc.perform(put("/csrf"))
+ .andExpect(status().isForbidden());
+
+ this.mvc.perform(post("/csrf"))
+ .andExpect(status().isOk());
+
+ this.mvc.perform(put("/no-csrf"))
+ .andExpect(status().isOk());
+ }
+
+ @EnableWebSecurity
+ static class IgnoringPathsAndMatchers extends WebSecurityConfigurerAdapter {
+ RequestMatcher requestMatcher =
+ request -> HttpMethod.POST.name().equals(request.getMethod());
+
+ @Override
+ protected void configure(HttpSecurity http) throws Exception {
+ // @formatter:off
+ http
+ .csrf()
+ .ignoringAntMatchers("/no-csrf")
+ .ignoringRequestMatchers(this.requestMatcher);
+ // @formatter:on
+ }
+ }
+
+ @RestController
+ public static class BasicController {
+ @RequestMapping("/path")
+ public String path() {
+ return "path";
+ }
+
+ @RequestMapping("/csrf")
+ public String csrf() {
+ return "csrf";
+ }
+
+ @RequestMapping("/no-csrf")
+ public String noCsrf() {
+ return "no-csrf";
+ }
+ }
+}