简体   繁体   中英

How to use JDBC-Authentication of Spring Boot/Spring Security with Flyway

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM