Spring Boot 2, Spring Security 5 and @WithMockUser

后端 未结 1 1154
礼貌的吻别
礼貌的吻别 2021-01-18 09:08

Since I migrated to Spring Boot 2.0.5 from 1.x, with no mean to disable security, I can\'t get test roles to work on mock MVC tests :

@RunWith(SpringRunner.c         


        
相关标签:
1条回答
  • 2021-01-18 09:39

    Ok... the good old CSRF stuff, then...

    logging.level.org.springframework.security=DEBUG
    

    2018-10-02 10:11:41.285 DEBUG 12992 --- [ main] o.s.security.web.csrf.CsrfFilter : Invalid CSRF token found for http://localhost/applications/foo

        Application app = Application.builder()
                .code(APP_CODE).name(APP_NAME)
                .build();
        mockMvc.perform(post("/applications").with(csrf())    // oups...
                .accept(MediaType.APPLICATION_JSON_UTF8)
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(mapper.writeValueAsString(app)))
                .andExpect(authenticated())
                .andExpect(status().isOk());    // there we go!
    
    0 讨论(0)
提交回复
热议问题