java - 如何通过 Flyway 使用 Spring Boot/Spring Security 的 JDBC 身份验证

标签 java spring-security spring-boot flyway

我正在尝试设置我的 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 种可能的解决方案

  1. 扩展 FlywayMigrationStrategy 并将 baselineOnMigrate 属性设置为 true
  2. 更好的是让 Flyway 完成所有数据库迁移。要启用该功能,请删除对 withDefaultSchema 的调用,只需添加 sql 即可将 Spring Security 表创建到 Flyway。 SQL 文件是 Spring Security 发行版的一部分。

关于java - 如何通过 Flyway 使用 Spring Boot/Spring Security 的 JDBC 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30013953/

相关文章:

java - 在接口(interface)和抽象类中访问同名值-Java

java - 为什么我的矩形没有在我的 Canvas 方法中更新?(android java)

grails - Grails 3 spring。安全性替代注册表格

java - 用于访问当前登录用户对象的 ACEGI 标记

java - 找到请求类型 [java.lang.Long] 的 HttpMessageConverter

java - 显示 MySQL 数据库中保存的消息

java - 如何使用 BlueCove 在装有 Java 的 MacBook 上使用蓝牙?

java - 如何将JSON数据插入到sql server而不转换为Java bean

java - 如何使用 spring boot security 配置多个 HttpSecurity 和 UserDetailsS​​ervice ?

spring-boot - Spring Boot 不使用 Spring data cassandra