在Spring Boot中从pom中的属性文件传递数据库信息

问题描述 投票:0回答:2

我想使用flyway插件,需要在pom中进行如下配置

        <plugin>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-maven-plugin</artifactId>
            <configuration>
                <url>${db.url}</url>
                <user>${db.username}</user>
                <password>${db.password}</password>
            </configuration>
        </plugin>

我尝试创建这样的属性

<properties>
    <db.url>${spring.datasource.url}</db.url>
    <db.username>${spring.datasource.username}</db.username>
    <db.password>${spring.datasource.password}</db.password>
</properties>

但是,它们不会从属性文件中获取。我不能在 pom 中将这些值静态化,因为它们在每个环境中都不同,而且我也不希望将凭证存储到 git 中。

如何设置 Flyway 插件并从属性中选择这些信息?

spring-boot maven flyway
2个回答
0
投票

利用环境变量是配置凭据的有效策略。该方法允许您在POM文件中进行引用,如下所示:

<configuration>
    <url>${env.DB_URL}</url>
    <user>${env.DB_USERNAME}</user>
    <password>${env.DB_PASSWORD}</password>
</configuration>

在此上下文中,

DB_URL
DB_USERNAME
DB_PASSWORD
表示环境变量,应在构建环境中建立。


0
投票

在推送到存储库时保护文件中的敏感数据的最有效方法是利用默认的 git 过滤机制。使用 sed,可以识别特定的字符串(例如文件中的纯文本密码),并将其替换为模糊值。这个过程在

staging
阶段执行,称为
clean filter
。相反,在
checkout
阶段,在文件中搜索混淆值并替换回原始内容的反向操作称为
smudge filter

虽然将此方法应用于各种文件类型(例如 .java、.js、.c、.cpp、.txt 等)很简单,但处理 .xml 文件(例如

pom.xml
文件)会带来一些挑战。

在插件需要在 POM 文件中指定并呈现一个值的情况下(如您的场景),保持 POM 的整洁和可管理性至关重要。为了实现这一点,建议将这些值从其原始位置提取到上层并在属性部分下定义它们。然而,仅此并不能防止敏感数据被无意中推送到存储库。

尽管如此,这种方法允许我们通过另一种方法(采用较小的解决方法)来实现我们的目标。通过将静态值存储在属性下,我们可以从 POM 文件中提取它们并将它们存储在常规 .properties 文件中。随后,我们可以使用 Properties Maven Plugin 将这些值重新集成回我们的 POM 中。

要实现此解决方案,需要将以下插件配置添加到我们的 POM 的

build/plugins
部分下。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.2.1</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>read-project-properties</goal>
            </goals>
            <configuration>
                <override>false</override>
                <useDefaultValues>true</useDefaultValues>
                <files>
                    <file>etc/config/dev.properties</file>
                </files>
            </configuration>
        </execution>
    </executions>
</plugin> 

假设示例中使用的文件地址,只需将静态值存储在位于

dev.properties
下的名为
${basedir}/etc/config/
的文件中。因此,在初始化阶段,Properties Maven 插件将读取保存的(名称,值)属性对并调整我们的
pom.xml
,就好像这些值是直接在 POM 本身中定义的一样。

通过此设置,可以在

dev.properties file
上应用 git 过滤机制。此外,为了从长远来看增强灵活性,建议使用类似于 TomerFi 建议的脚本。该脚本可以保存在 .sh 文件夹下的
./scripts
文件中。
declare -A mapArr

mapArr["prodPassword"]="obfuscatedProductionPassword"
mapArr["devPassword"]="obfuscatedDevelopmentPassword"

# mac users: use gsed instead of sed
sedcmd="sed"
if [[ "$1" == "clean" ]]; then
  for key in ${!mapArr[@]}; do
    sedcmd+=" -e \"s/${key}/${mapArr[${key}]}/g\""
  done  
elif [[ "$1" == "smudge" ]]; then
  for key in ${!mapArr[@]}; do
    sedcmd+=" -e \"s/${mapArr[${key}]}/${key}/g\""
  done  
else  
  echo "use smudge/clean as the first argument"
  exit 1
fi

eval $sedcmd

对于在暂存和签出阶段调用的脚本,如前所述,应使用以下命令更新本地 git 配置文件:

git config filter.cleanPass.smudge "./scripts/git-smudge-clean-filter.sh smudge" git config filter.cleanPass.clean "./scripts/git-smudge-clean-filter.sh clean"

稍后,
.gitattribute

s 文件应包含在存储库的根目录中。这指示 git 在指定的文件类型上应用添加的干净和污迹过滤器。

*.properties text eol=lf filter=cleanPass

因此,避免了敏感数据的公开曝光。

此外,该解决方案可以轻松扩展,以便为各种环境/平台设置具有不同静态值的插件。在这种情况下,在

pom.xml

中定义不同的配置文件变得至关重要。对于每个配置文件,应根据配置文件的用途单独定义和配置 Properties Maven 插件。这与之前在

pom.xml
的全局构建/插件部分中演示的定义不同。通过采用这种方法,每个配置文件都可以加载其相关属性集。简而言之,对于我们的开发目标构建,我们可以加载保存在
dev.properties
文件中的相关数据库密码。相反,当针对生产环境进行构建时,将加载保存在
prod.properties
中的密码。
最终结构的粗略概述如下所示:

root | |- .git/ |- etc/config | |- dev.properties | |- prod.properties |- scripts/ | |- git-smudge-clean-filter.sh |- pom.xml |- .gitattribute

 
pom.xml

大致如下所示:

<project>
    <profiles>
        <profile>
            <id>dev-env</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>properties-maven-plugin</artifactId>
                        <version>1.2.1</version>
                        <executions>
                            <execution>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>read-project-properties</goal>
                                </goals>
                                <configuration>
                                    <override>false</override>
                                    <useDefaultValues>true</useDefaultValues>
                                    <files>
                                        <file>etc/config/dev.properties</file>
                                    </files>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin> 
                </plugins>
            </build>
        </profile>
        <profile>
            <id>prod-end</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>properties-maven-plugin</artifactId>
                        <version>1.2.1</version>
                        <executions>
                            <execution>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>read-project-properties</goal>
                                </goals>
                                <configuration>
                                    <override>false</override>
                                    <useDefaultValues>true</useDefaultValues>
                                    <files>
                                        <file>etc/config/prod.properties</file>
                                    </files>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin> 
                </plugins>
            </build>
        </profile>
    </profiles>
    <build>
        <plugins>
            <plugin>
                <groupId>org.flywaydb</groupId>
                <artifactId>flyway-maven-plugin</artifactId>
                <configuration>
                    <url>${db.url}</url>
                    <user>${db.username}</user>
                    <password>${db.password}</password>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

最后,
dev.properties

应该看起来像这样:

db.url=localhost:8088
db.username=root
db.password=devPassword

prod.propertie

看起来像这样:

db.url=https://secure-server.i0:3534
db.username=dbManager
db.password=prodPassword

您还可以在这个 git 存储库下看到准备检查/运行的
示例。

© www.soinside.com 2019 - 2024. All rights reserved.