@Order on WebSecurityConfigurers must be unique. Order of 100 was already used on org.engine.security.WebSecurityConfig

Viewed 3726

I'm trying to run Spring Boot app which connects to remote MySQL server. I get exception during startup time:

Caused by: java.lang.IllegalStateException: @Order on WebSecurityConfigurers must be unique. Order of 100 was already used on org.engine.security.WebSecurityConfig$$EnhancerBySpringCGLIB$$de8b459d@49e2b3c5, so it cannot be used on org.engine.security.WebSecurityConfiguration$$EnhancerBySpringCGLIB$$e6562c81@5a484ce1 too.
        at org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.setFilterChainProxySecurityConfigurer(WebSecurityConfiguration.java:147

Full log: https://pastebin.com/xDguJ0Vz

Locally it's working fine. Probably the connect time to MySQL is the issue. Do you know how I can increase the connect time in order to fix this issue?

3 Answers

Wrong Spring Security configurations.

Default WebSecurityConfigurerAdapter's @Order value is 100. When creating 2 or more WebSecurityConfigurerAdapter implementations, set unique value explicitly.

On your environment, org.engine.security.WebSecurityConfig and org.engine.security.WebSecurityConfiguration are ones.

You need annotate the Configurers with a different @Order value--so like @Order(101) or something.

This error occurs when your SpringBoot application has two classes that 'extends' WebSecurityConfigurerAdapter.

Option 1: Keep only one class that extends WebSecurityConfigurerAdapter.

Option 2: If you want to extend WebSecurityConfigurerAdapter multiple times then set explicit @Order(1) annotation on the class that you wish to be considered first.

    @Configuration
    @Order(1)                                                        
    public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
        protected void configure(HttpSecurity http) throws Exception {
            http
                .antMatcher("/api/**")                               
                .authorizeRequests()
                    .anyRequest().hasRole("ADMIN")
                    .and()
                .httpBasic();
        }
    }

    @Configuration                                                   
    public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                    .anyRequest().authenticated()
                    .and()
                .formLogin();
        }
    } 

Link to Spring Docs: https://docs.spring.io/spring-security/site/docs/4.2.x/reference/html/jc.html

Related