Spring Boot integration test ignoring secure=false in AutoConfigureMockMvc annotation, get 401

前端 未结 2 639
借酒劲吻你
借酒劲吻你 2020-12-11 02:54

Can\'t make my @SpringBootTest work. It says authentication is on, which I do not want.

I\'ve set it up with @AutoConfigureMockMvc(secure = false

相关标签:
2条回答
  • 2020-12-11 03:20

    Changing

    @AutoConfigureMockMvc(secure = false)
    

    to

    @AutoConfigureMockMvc(addFilters=false)
    

    works for me.

    0 讨论(0)
  • 2020-12-11 03:22

    Adam.

    Since I also recently ran into this problem after updating Spring Boot to 2.1.3.RELEASE and Spring Framework to 5.1.4.RELEASE, which forces to add Spring Web Security and If someone wants to not provide security resolver then they are required to disable security in Test Environment, so I decided to share how I ended up resolving this issue.

    I was also scratching head while working up an application and writing Integration Test Cases with @SpringBootTest. Using @WebMvcTest is way less painful than this, tbh.

    In my case following worked.

    @EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class)//This annotation was required to run it successfully
    @DisplayName("UserControllerTest_SBT - SpringBootTest")
    class UserControllerTest_SBT extends BaseTest_SBT {
    
        @Autowired
        private MockMvc mockMvc;
    
        @Test
        void getUsersList() throws Exception {
            this.mockMvc.perform(MockMvcRequestBuilders.get("/user/listAll")
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andDo(print());
    
        }
    
    }
    
    @ExtendWith(SpringExtension.class) //This is not mandatory
    @SpringBootTest
    @AutoConfigureMockMvc(secure = false) // Secure false is required to by pass security for Test Cases
    @ContextConfiguration //This is also not mandatory just to remove annoying warning, i added it
    public class BaseTest_SBT {
    
    }
    

    What didn't work:

    1- @SpringBootTest(properties = {"security.basic.enabled=false"}) <-- This solution is deprecated! More details here

    2- \src\test\resources\application.properties** -> security.basic.enabled=false

    Hopefully, this will be helpful to someone.

    0 讨论(0)
提交回复
热议问题