【问题标题】:How to use JDBC-Authentication of Spring Boot/Spring Security with Flyway如何通过 Flyway 使用 Spring Boot/Spring Security 的 JDBC-Authentication
【发布时间】:2015-07-12 21:12:48
【问题描述】:

我正在尝试设置我的 spring boot 应用程序,该应用程序使用 jdbcAuthentication 和 spring 安全文档附录中提供的默认数据库方案对其用户进行身份验证。但是我在数据库初始化期间遇到了这个异常:

org.flywaydb.core.api.FlywayException:发现没有元数据表的非空模式“PUBLIC”!使用 baseline() 或将 baselineOnMigrate 设置为 true 来初始化元数据表。

身份验证管理器的配置如下:

    @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

【问题讨论】:

  • 扩展 FlywayMigrationStrategy 并在调用超级方法之前执行错误消息告诉您的操作(将 baselineOnMigrate 设置为 true)。扩展后将其注册为 bean。但是最好让 FlyWay 完成所有工作,因此删除 withDefaultSchema() 并将用于创建安全模式的 sql 添加到 flyway。
  • 感谢您的回复。我没有意识到调用withDefaultSchema() 会尝试初始化数据库。我删除了该声明,它就像一个魅力。根本不需要扩展FlywayMigrationStrategy。我更新了 github 项目以显示解决方案:github.com/smilingj/springboot-authentication-flyway-sample/…。我想将您的答案标记为正确,但这需要一个答案。

标签: java spring-security spring-boot flyway


【解决方案1】:

在配置 jdbcAuthentication 并调用 withDefaultSchema 时,直接创建架构并在 Flyway 进行任何更改以创建架构之前执行此操作。

Flyway 现在检测到它已经存在,而不是允许它创建架构并抱怨这一点。

您有两种可能的解决方案

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

【讨论】:

    猜你喜欢
    • 2015-12-21
    • 2019-01-17
    • 2015-04-16
    • 2014-08-02
    • 2021-08-27
    • 2015-12-04
    • 2018-09-06
    • 2018-01-15
    • 2015-11-23
    相关资源
    最近更新 更多