Disable security for unit tests with spring boot

前端 未结 8 1957
情话喂你
情话喂你 2020-11-27 19:16

I\'m trying to create a simple spring boot web project with security. I can launch the application fine and the security is working fine. However, I have some components t

8条回答
  •  一向
    一向 (楼主)
    2020-11-27 20:00

    FmpdfApplication is likely annotated with @EnableAutoConfiguration (or with @SpringBootApplication which is meta-annotated with @EnableAutoConfiguration), and this will lead to Spring Security being picked up and configured via auto-configuration.

    If you want to see what's being auto-configured, launch your web app and access the autoconfig endpoint (e.g., http://localhost:8080/autoconfig). Then search for 'Security' to see which 'AutoConfiguration' classes are being detected.

    You can then disable auto-configuration of security by excluding those classes like this:

    @EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class, ManagementSecurityAutoConfiguration.class })
    

    Of course, you won't want to exclude them for production deployments. Thus you'll need to have a separate @Configuration class for production and tests.

    Regards,

    Sam

    p.s. You might also find my answer to the following question useful as well: Spring-Boot module based integration testing

提交回复
热议问题