2
votes

I am trying to set up my spring boot application that authenticates its users using the jdbcAuthentication and the default database scheme provided in the appendix of the spring security documentation. But i am stuck getting this exception during the database initialization:

org.flywaydb.core.api.FlywayException: Found non-empty schema "PUBLIC" without metadata table! Use baseline() or set baselineOnMigrate to true to initialize the metadata table.

The configuration of the authentication manager looks like this:

    @Configuration
    @EnableWebMvcSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

        @Autowired
        private DataSource dataSource;

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                    .anyRequest().authenticated()
                    .and()
                .httpBasic()
                    .realmName("shipment2rss")
                    .and()
                .logout()
                    .permitAll();
        }

        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
            auth
                .jdbcAuthentication()
                    .dataSource(dataSource)
                        .withDefaultSchema();
        }

    }

I have read that the problem seams to be that the method configureGlobal(AuthenticationManagerBuilder ) is invoked before the Flyway-related code gets executed (see How to use Flyway in Spring Boot with JDBC Security?) but found no step-by-step guide how to work around this specific problem.

Can anyone give me such a guide or point me to a website that does?

EDIT I uploaded a project to show the problem at github: https://github.com/smilingj/springboot-authentication-flyway-sample/tree/e48ce63568776d99e49a9548d8362168cc3a3367

1
Extend FlywayMigrationStrategy and before calling the super method do what the error message tells you to (set baselineOnMigrate to true). After extending register it as a bean. However better would be to let FlyWay do all the work, so remove withDefaultSchema() and add the sql for creating the security schema to flyway.M. Deinum
Thanks for your response. I didn't realize that calling withDefaultSchema() will try to initialize the database. I removed that statement an it works like a charm. Extending the FlywayMigrationStrategy was not necessary at all. I updated the github project to show the solution: github.com/smilingj/springboot-authentication-flyway-sample/… . I would like to mark your answer as correct but that would require an answer.Johannes Grimm

1 Answers

3
votes

When configuring the jdbcAuthentication and calling withDefaultSchema that directly creates the schema and does so before Flyway has any change to create the schema.

Flyway now detects it is already there instead of it being allowed to create the schema and it complains about that.

You have 2 possible solutions

  1. Extend the FlywayMigrationStrategy and set the baselineOnMigrate property to true.
  2. Better is to let Flyway do all the database migrations. To enable that remove the call to withDefaultSchema and simply add the sql to create the Spring Security tables to Flyway. The SQL files are part of the Spring Security distribution.