spring: datasource: url: ${DB_URL} username: ${DB_USERNAME} password: ${DB_PASSWORD}步骤二:设置环境变量
Linux/macOS: export DB_URL=jdbc:mysql://localhost:3306/dbname export DB_USERNAME=user export DB_PASSWORD=password Windows: set DB_URL=jdbc:mysql://localhost:3306/dbname set DB_USERNAME=user set DB_PASSWORD=password二.使用 Jasypt 加密
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.4</version> </dependency>步骤二:配置Jasypt
jasypt.encryptor.password=mysecretkey步骤三:加密和解密
jasypt encrypt --password=mysecretkey --algorithm=PBEWithMD5AndTripleDES input="mysecretpassword"在配置文件中使用:
spring: datasource: password: ENC(encryptedpassword)
步骤四:管理密钥
使用环境变量或密钥管理系统存储加密密钥
三.使用 Spring Cloud Config
步骤一:引入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> <version>3.1.3</version> </dependency>步骤二:配置 Config Server
spring: cloud: config: server: git: uri: https://github.com/your-repo/config-repo search-paths: config步骤三:安全存储配置
spring: cloud: config: uri: http://localhost:8888实践指南