I have a Java application that uses Spring Security via Java configuration.
What is the easiest method of switching the whole Spring Security on/off in compilation?
So something like this, but for a configuration that uses no XML.
EDIT:
After applying @Profile my code looks like:
@Configuration
@Profile("SecurityOn")
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
The problem is that if the profile "SecurityOn" is not activated, Spring Security uses some default configuration. Instead, how to turn Spring Security completely off in that case?
To disable that behavior, you can add another class that looks like this:
@Configuration
@EnableWebMvcSecurity
@Profile("!SecurityOn")
public class WebSecurityConfigDisable extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/**").permitAll();
}
}
Then, when you run your application, the only time you'll need to login will be when the SecurityOn
profile is active. If you're using Maven and Spring Boot, the command to enable login would be the following.
mvn spring-boot:run -Dspring.profiles.active=SecurityOn
Running it without a profile, or a different profile, will disable login. This is useful for local development.
I found this was necessary when using spring-boot-starter-security
because there was default configuration which required login.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With