I followed these 2 articles to implement multiple authentication profiles in a Spring Boot application:
- https://medium.com/@igor.bonny/multiple-spring-boot-security-configuration-c876f1b6061e
- https://dev.to/trexinc/spring-boot-and-multiple-authentication-profiles-none-password-okta-5bce
and the final SecurityConfiguration
class looks as follows:
@Configuration
@EnableWebSecurity
public class SecurityConfiguration {
@Configuration
@Profile({"qa", "prod"})
public static class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors()
.and()
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/someUrl").permitAll()
.antMatchers("/someUrl").fullyAuthenticated()
.antMatchers("/api/ping").permitAll()
.antMatchers("/**").fullyAuthenticated()
...
.
}
}
@Configuration
public static class LocalSecurityConfiguration extends WebSecurityConfigurerAdapter {
...
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors()
.and()
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/someUrl").permitAll()
.antMatchers("/someUrl").fullyAuthenticated()
.antMatchers("/api/ping").permitAll()
.antMatchers("/**").permitAll().
...
.
}
}
}
I also have separate application-{profile}.yaml
files for both qa
and prod
profiles (environments).
When starting the app with
mvn spring-boot:run -Dspring.profiles.active=qa
I see that the right profile was applied (qa
):
Running with Spring Boot v2.1.3.RELEASE, Spring v5.1.5.RELEASE
...
The following profiles are active: a
...
but it fails later with:
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'authenticationManagerBean', defined in class path resource [com/.../SecurityConfiguration$WebSecurityConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/../SecurityConfiguration$LocalSecurityConfiguration.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
[WARNING]
java.lang.reflect.InvocationTargetException
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:566)
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run (AbstractRunMojo.java:558)
at java.lang.Thread.run (Thread.java:834)
Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'authenticationManagerBean' defined in class path resource [com/../SecurityConfiguration$WebSecurityConfiguration.class]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=securityConfiguration.WebSecurityConfiguration; factoryMethodName=authenticationManagerBean; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/.../SecurityConfiguration$WebSecurityConfiguration.class]] for bean 'authenticationManagerBean': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=securityConfiguration.LocalSecurityConfiguration; factoryMethodName=authenticationManagerBean; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/..../SecurityConfiguration$LocalSecurityConfiguration.class]] bound.
When running without specifying any profiles it starts without errors. What's wrong with that?
Thank you.