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
FlywayMigrationStrategy
and before calling the super method do what the error message tells you to (setbaselineOnMigrate
totrue
). After extending register it as a bean. However better would be to let FlyWay do all the work, so removewithDefaultSchema()
and add the sql for creating the security schema to flyway. – M. DeinumwithDefaultSchema()
will try to initialize the database. I removed that statement an it works like a charm. Extending theFlywayMigrationStrategy
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