我正在尝试设置我的 Spring Boot 应用程序,该应用程序使用 jdbcAuthentication 和 Spring 安全文档附录中提供的默认数据库方案对其用户进行身份验证。但我在数据库初始化期间遇到了这个异常:
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.
身份验证管理器的配置如下所示:
@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();
}
}
我读到问题似乎是在执行 Flyway 相关代码之前调用了方法 configureGlobal(AuthenticationManagerBuilder )
(请参阅 How to use Flyway in Spring Boot with JDBC Security? ),但没有发现任何步骤指导如何解决这个特定问题。
任何人都可以给我这样的指南或指向我一个网站吗?
编辑我上传了一个项目来在github上显示问题:https://github.com/smilingj/springboot-authentication-flyway-sample/tree/e48ce63568776d99e49a9548d8362168cc3a3367
最佳答案
当配置jdbcAuthentication
并调用withDefaultSchema
时,直接创建架构,并在Flyway进行任何更改以创建架构之前执行此操作。
Flyway 现在检测到它已经存在,而不是允许它创建架构,并且它对此进行了提示。
您有 2 种可能的解决方案
- 扩展
FlywayMigrationStrategy
并将baselineOnMigrate
属性设置为true
。 - 更好的是让 Flyway 完成所有数据库迁移。要启用该功能,请删除对
withDefaultSchema
的调用,只需添加 sql 即可将 Spring Security 表创建到 Flyway。 SQL 文件是 Spring Security 发行版的一部分。
关于java - 如何通过 Flyway 使用 Spring Boot/Spring Security 的 JDBC 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30013953/