首页 > 解决方案 > 将 Liquibase 与 Azure SQL 和 Azure Active Directory 身份验证结合使用

问题描述

如何将 Liquibase 与 Azure SQL 数据库和 Azure Active Directory 身份验证一起使用?具体来说,我想使用此处记录的 ActiveDirectoryPassword 身份验证模式进行连接:

https://docs.microsoft.com/en-us/sql/connect/jdbc/connecting-using-azure-active-directory-authentication?view=sql-server-ver15#connecting-using-activedirectorypassword-authentication-mode

我不知道如何调用 Liquibase CLI 来实现这一点。

这可能吗?

标签: azureazure-active-directoryliquibase

解决方案


我能够让它工作。我对 Java 不是很熟悉(我们将 Liquibase 与 C# 项目一起使用),所以我认为一些 Java 部分让我大吃一惊。

为了完成这项工作,我必须做几件事:

  1. 我需要在发送到 Liquibase 的 URL 中添加一些属性:

--url="jdbc:sqlserver://REDACTED.database.windows.net;databaseName=REDACTED;authentication=ActiveDirectoryPassword;encrypt=true;trustServerCertificate=true"

ActiveDirectoryPassword告诉驱动程序使用我想要的身份验证机制。我还必须添加 encrypt=true 和 trustServerCertificate=true 以避免我遇到的一些 SSL 错误(来自: https ://docs.microsoft.com/en-us/sql/connect/jdbc/connecting-with-ssl-encryption ?view=sql-server-ver15)。

  1. 我的类路径中需要 MSAL4J(Azure Active Directory)库。我将它们添加到 liquibase/lib 目录,以便默认的 Liquibase 启动器脚本为我添加它们。我也被抓住了,因为我需要使用我们不使用的 Maven。下载Maven后,我使用copy-dependencies插件下载了我需要的依赖。

    mvn 依赖:复制依赖

这是我使用的简单 pom.xml:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1</version>
    <dependencies>
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>adal4j</artifactId>
            <version>1.6.3</version>
        </dependency>
    </dependencies>
</project>

我还将这些依赖项放在 liquibase/lib 目录中,以便它们自动包含在类路径中。Microsoft 的说明有助于将我带到正确的位置:

https://docs.microsoft.com/en-us/sql/connect/jdbc/connecting-using-azure-active-directory-authentication?view=sql-server-ver15#connecting-using-activedirectorypassword-authentication-mode

此外,不确定是否需要它来实现我的目标,但我升级到了最新的 Liquibase (3.8.7) 和最新的 SQL Server 驱动程序 (8.2):

https://docs.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15


推荐阅读