如何管理不同服务器的java spring boot应用程序中的属性文件。
我有一个 springboot 应用程序,在
src/main/resources
文件夹中我有大约 15 个属性文件。我使用类似的东西。
@PropertySource(value = "classpath:properties/myfunctional.properties”)
所以我能够用它来完成我的工作,因为这是简单的 Spring Boot 功能。
现在将所有这些保留在
src/main/resources
中并不是一个好主意,因为
因此我有几个问题。
(我尝试的技术很少,比如 springboot 配置或 git 秘密变量。但我再次不确定它有多安全,我有 15 个属性文件,所以在寻找边界方面。)
每个国家的开发者都有不同的java工具,在中国我们经常使用nacos来存储敏感信息 这是一个分布式命名和配置中心
本地
一般情况下,我们常将弹簧型材命名为以下类型
application.properties
application-dev.properties
application-uat.properties
application-uat.properties
当需要不同的服务器环境时,我们在启动jar之前添加额外的命令。尝试使用以下命令
// the application shoule be your jar name
java -jar -Dspring.profiles.active=dev application.jar
远程
基本会有一个配置属性来指定配置中心主机的属性,例如(只是示例,不在实际使用中)
// you deploy tools server ip
remote.config.host = http://192.168.2.1:8848
// the env properties which you would like to active
remote.config.namespace = dev
请参考nacos用法,我想对你的场景可能会有更多有用的知识
我将展示如何管理多个包含秘密的
.properties
文件。
分步过程(用于演示):
创建单独的
.properties
文件,以防止机密/敏感属性被泄露。
.properties
创建了 secrets
文件,并且为 dev、stage、test 和 prod 创建了四个
.properties
文件。secrets-dev.properties:
secrets.api-key=<dev-api-key>
secrets.token=<dev-token>
secrets-stage.properties:
secrets.api-key=<stage-api-key>
secrets.token=<stage-token>
秘密测试.属性:
secrets.api-key=<test-api-key>
secrets.token=<test-token>
secrets-prod.properties:
secrets.api-key=<prod-api-key>
secrets.token=<prod-token>
注意: 此位置应完全受到保护,免受外界影响。只有应用程序才有权访问。
项目结构:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-test-externalize-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-test-externalize-config</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
现在,您需要为
dev、stage、test 和 prod 创建
application-{env}.properties
文件。
application-dev.properties:
spring.config.import=optional:/Users/anish/secrets/secrets-dev.properties
应用程序阶段.属性:
spring.config.import=optional:/Users/anish/secrets/secrets-stage.properties
应用程序测试.属性:
spring.config.import=optional:/Users/anish/secrets/secrets-test.properties
application-prod.properties:
spring.config.import=optional:/Users/anish/secrets/secrets-prod.properties
注意: 您必须通过
secrets-{env}.properties
导入带有 optional
关键字的 spring.config.import
,这样即使属性文件丢失,代码仍然可以工作并且不会导致应用程序崩溃。
秘密属性:
package com.example.demo;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("secrets")
public class SecretsProperties {
private String apiKey;
private String token;
public SecretsProperties(String apiKey, String token) {
this.apiKey = apiKey;
this.token = token;
}
@Override
public String toString() {
return "SecretsProperties [apiKey=" + apiKey + ", token=" + token + "]";
}
public String getApiKey() {
return apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
SpringTestExternalizeConfigApplication(主):
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@SpringBootApplication
@EnableConfigurationProperties(SecretsProperties.class)
public class SpringTestExternalizeConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringTestExternalizeConfigApplication.class, args);
}
}
测试控制器:
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
private SecretsProperties properties;
public TestController(SecretsProperties properties) {
this.properties = properties;
}
@GetMapping("test-external-properties")
public SecretsProperties testExternalProperties() {
return properties;
}
}
输出:
如果您使用
-Dspring.profiles.active=dev
运行应用程序,则会显示:
其他环境也一样。
假设您提供了错误的位置或删除了 Secrets-{env}.properties 文件,那么应用程序仍将运行,但不会有任何数据。这意味着您的条件/敏感属性受到保护。