Integration tests with spring security

前端 未结 1 1231
悲&欢浪女
悲&欢浪女 2020-12-31 01:17

I need to send a get request to the API, but despite having placed the administrator annotation get error @WithMockUser(roles=\"ADMINISTRADOR\").
Ho

相关标签:
1条回答
  • 2020-12-31 01:36

    In the Spring security Reference, section 10.1 states that in order to be able to test the spring security features, you need to integrate the security filter chain in your MockMvc object, as shown in this example in the @Before setup method.

    import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration
    @WebAppConfiguration
    public class CsrfShowcaseTests {
    
        @Autowired
        private WebApplicationContext context;
        private MockMvc mvc;
    
        @Before
        public void setup() {
            mvc = MockMvcBuilders
                .webAppContextSetup(context)
                .apply(springSecurity())
                .build();
        }
    
    ...
    
    }
    
    0 讨论(0)
提交回复
热议问题