2021-10-29 12:34:29 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								[[test-mockmvc-setup]]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								= Setting Up MockMvc and Spring Security
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[NOTE]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								====
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Spring Security's testing support requires spring-test-4.1.3.RELEASE or greater.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								====
							 
						 
					
						
							
								
									
										
										
										
											2021-10-29 12:34:29 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								To use Spring Security with Spring MVC Test, add the Spring Security `FilterChainProxy` as a `Filter`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You also need to add Spring Security's `TestSecurityContextHolderPostProcessor` to support xref:servlet/test/mockmvc/setup.adoc#test-mockmvc-withmockuser[Running as a User in Spring MVC Test with Annotations].
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								To do so, use Spring Security's `SecurityMockMvcConfigurers.springSecurity()`:
							 
						 
					
						
							
								
									
										
										
										
											2021-10-29 12:34:29 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								====
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								.Java
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-03-22 20:30:10 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								@ExtendWith(SpringExtension.class)
							 
						 
					
						
							
								
									
										
										
										
											2021-10-29 12:34:29 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								@ContextConfiguration(classes = SecurityConfig.class)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WebAppConfiguration
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public class CsrfShowcaseTests {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									@Autowired
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									private WebApplicationContext context;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									private MockMvc mvc;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-03-22 20:30:10 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									@BeforeEach
							 
						 
					
						
							
								
									
										
										
										
											2021-10-29 12:34:29 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									public void setup() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										mvc = MockMvcBuilders
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												.webAppContextSetup(context)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												.apply(springSecurity()) // <1>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												.build();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									}
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2021-10-29 12:34:29 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								.Kotlin
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2022-03-22 20:30:10 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								@ExtendWith(SpringExtension.class)
							 
						 
					
						
							
								
									
										
										
										
											2021-10-29 12:34:29 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								@ContextConfiguration(classes = [SecurityConfig::class])
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WebAppConfiguration
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class CsrfShowcaseTests {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @Autowired
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    private lateinit var context: WebApplicationContext
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    private var mvc: MockMvc? = null
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-03-22 20:30:10 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    @BeforeEach
							 
						 
					
						
							
								
									
										
										
										
											2021-10-29 12:34:29 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    fun setup() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        mvc = MockMvcBuilders
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .webAppContextSetup(context)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .apply<DefaultMockMvcBuilder>(springSecurity()) // <1>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .build()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2021-10-29 12:34:29 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								<1> `SecurityMockMvcConfigurers.springSecurity()` will perform all of the initial setup we need to integrate Spring Security with Spring MVC Test
							 
						 
					
						
							
								
									
										
										
										
											2021-10-29 12:34:29 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								====