Question: Spring Security testing with CookieCsrfTokenRepository


Spring Security testing with CookieCsrfTokenRepository

Answers 0
Added at 2016-12-20 12:12

I created simple project in Spring Security and I'm trying to test my code. I use AngularJS as a frontend layer. My problem is related with CSRF. I get 403 http status when I'm trying test my controllers. Based on the documentation, I am under the impression that CSRF should have been configured, but that seems not to be the case. My configuration files:

In Security config:


In my base web test class I set up MockMvc with Security settings based on Spring documentation ( )

protected MockMvc buildMockMvcWithUserAuthentication() {
    return MockMvcBuilders.webAppContextSetup(wac)

Now, In my test I have something like this:

public void postTest() throws Exception {
    message = Message.newInfoMessage(EXAMPLE_MESSAGE_CONTENT);

    String json = mapper.toJson(message);



What I discovered, .with(csrf()) is using HttpSessionCsrfTokenRepository() object. My question is, if there is any simple way to use CookieCsrfTokenRepository in my JUnit test?

Answers to

Spring Security testing with CookieCsrfTokenRepository

Source Show
◀ Wstecz