Twoja odpowiedź uiroshan jest pokonanie cel CSRF tokena: z konfiguracją będzie teraz być wartością stałą (chyba że konfiguracja jest stosowana tylko w kontekście badań, ale nie określił go) .
Prawo (i łatwiej) sposobem rozwiązania tego problemu jest:
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*;
...
@Test
public void testLogin() throws Exception {
this.mockMvc.perform(post("/login")
.param("username", "...")
.param("password", "...")
.with(csrf()))
.andExpect(status().isFound())
.andExpect(header().string("Location", "redirect-url-on-success-login"));
}
Ważną częścią jest: .with(csrf())
który doda oczekiwany _csrf
parametr do zapytania.
csrf()
metoda statyczna jest przez spring-security-test
:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>4.2.4.RELEASE/5.0.2.RELEASE</version>
<scope>test</scope>
</dependency>
testu urządzenie będzie wymagać następujące import do niego dostęp:
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*;