I am developing a Spring Boot application using STS with the Gradle plugin. I have a different configuration for tests, to prevent our Selenium tests from having to login.
So in src/test/java/etc
I have something like this:
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public static class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests().anyRequest().permitAll();
}
}
Whereas in src/main/java
I have an equivalent class that configures login etc, requiring login for all pages.
If I run the application through the Gradle plugin (bootRun), everything works fine.
However, if I run or debug it through Eclipse directly (e.g. right clicking on the project, Run As->Spring Boot App or by clicking the run/debug buttons in the Spring or Java view) then the test config is applied, so access is granted to all pages without login.
I'm guessing that the test classes are being included in the classpath when I start the application this way. Is there an easy way to prevent this from happening?
@Profile("test")
to this class +@ActiveProfiles("test")
on the test. That would make the bean condtional on that profile. See docs.spring.io/spring/docs/current/spring-framework-reference/… – Patronymic