如何用Java實(shí)現(xiàn).env文件讀取敏感數(shù)據(jù)
1.common-env-starter模塊
1.目錄結(jié)構(gòu)
2.DotenvEnvironmentPostProcessor.java 在${xxx}解析之前執(zhí)行,提前讀取配置
package com.sunxiansheng.env.processor; import io.github.cdimascio.dotenv.Dotenv; import org.springframework.boot.SpringApplication; import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.core.env.ConfigurableEnvironment; /** * Description: 在${xxx}解析之前執(zhí)行,可以讀取xxx,設(shè)置到環(huán)境中,在后續(xù)的解析時(shí)就會(huì)進(jìn)行替換了 * * @Author sun * @Create 2025/1/10 19:40 * @Version 1.0 */ public class DotenvEnvironmentPostProcessor implements EnvironmentPostProcessor{ @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { // 從 Spring 的配置中獲取 sun-rays.env.path String dotenvPath = environment.getProperty("sun-rays.env.path"); if (dotenvPath != null) { // 加載 .env 文件 Dotenv dotenv = Dotenv.configure() .directory(dotenvPath) .filename(".env") .load(); // 將 .env 中的值注入到系統(tǒng)屬性中,確保占位符解析時(shí)可用 dotenv.entries().forEach(entry -> environment.getSystemProperties().put(entry.getKey(), entry.getValue()) ); System.out.println("Loaded .env from path: " + dotenvPath); } else { System.err.println("sun-rays.env.path not configured!"); } } }
3.EnvProperties.java 這里的path只是為了代碼提示
package com.sunxiansheng.env.config.properties; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; /** * Description: 這里的path只是為了代碼提示,實(shí)際上DotenvEnvironmentPostProcessor.java不從這里獲取配置 * * @Author sun * @Create 2025/1/10 20:04 * @Version 1.0 */ @ConfigurationProperties(prefix = "sun-rays.env") @Data public class EnvProperties { /** * .env文件的絕對(duì)路徑 */ private String path; }
4.EnvAutoConfiguration.java Env模塊自動(dòng)配置類
package com.sunxiansheng.env.config; import com.sunxiansheng.env.config.properties.EnvProperties; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration; import javax.annotation.PostConstruct; /** * Description: Env模塊自動(dòng)配置類 * * @Author sun * @Create 2025/1/10 19:57 * @Version 1.0 */ @Configuration @EnableConfigurationProperties({EnvProperties.class}) // 啟用配置類 @Slf4j public class EnvAutoConfiguration { /** * 自動(dòng)配置成功日志 */ @PostConstruct public void logConfigSuccess() { log.info("EnvAutoConfiguration has been loaded successfully!"); } }
5.spring.factories 自動(dòng)配置和注冊(cè)EnvironmentPostProcessor
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.sunxiansheng.env.config.EnvAutoConfiguration org.springframework.boot.env.EnvironmentPostProcessor=\ com.sunxiansheng.env.processor.DotenvEnvironmentPostProcessor
6.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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.sunxiansheng</groupId> <artifactId>sunrays-common</artifactId> <version>2.0.0</version> </parent> <artifactId>common-env-starter</artifactId> <dependencies> <!-- 可以使用.env文件提前加載配置,確保數(shù)據(jù)安全 --> <dependency> <groupId>io.github.cdimascio</groupId> <artifactId>java-dotenv</artifactId> </dependency> </dependencies> </project>
2.common-env-starter-demo模塊
1.目錄結(jié)構(gòu)
2.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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.sunxiansheng</groupId> <artifactId>sunrays-common-demo</artifactId> <version>2.0.0</version> </parent> <artifactId>common-env-starter-demo</artifactId> <dependencies> <!-- common-env-starter --> <dependency> <groupId>com.sunxiansheng</groupId> <artifactId>common-env-starter</artifactId> <version>2.0.0</version> </dependency> </dependencies> </project>
3.application.yml 配置測(cè)試的數(shù)據(jù)
sun-rays: log4j2: home: /Users/sunxiansheng/IdeaProjects/sunrays-framework/sunrays-common-demo/common-env-starter-demo/logs # 日志根目錄(默認(rèn)./logs) module: sunrays-common-demo/common-env-starter-demo # 模塊根目錄從倉(cāng)庫(kù)根目錄開始(默認(rèn)defaultModule) env: path: /Users/sunxiansheng/IdeaProjects/sunrays-framework/sunrays-common-demo/common-env-starter-demo # .env文件的絕對(duì)路徑 env: test: ${ENV_TEST}
4.EnvConfig.java
package com.sunxiansheng.env.config; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import javax.annotation.PostConstruct; /** * Description: Env配置類,測(cè)試讀取數(shù)據(jù) * * @Author sun * @Create 2025/1/10 20:55 * @Version 1.0 */ @Configuration @Slf4j public class EnvConfig { @Value("${env.test}") private String test; @PostConstruct public void init() { // 測(cè)試讀取 log.info("test={}", test); } }
5.EnvApplication.java 啟動(dòng)類
package com.sunxiansheng.env; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Description: Env * * @Author sun * @Create 2025/1/10 20:53 * @Version 1.0 */ @SpringBootApplication public class EnvApplication { public static void main(String[] args) { SpringApplication.run(EnvApplication.class, args); } }
6.測(cè)試
總結(jié)
到此這篇關(guān)于Java實(shí)現(xiàn).env文件讀取敏感數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Java .env文件讀取敏感數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用Spring Native將SpringBoot程序轉(zhuǎn)換為GraalVM
這篇文章主要介紹了用Spring Native將SpringBoot程序轉(zhuǎn)換為GraalVM的方法,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot,感興趣的朋友可以了解下2021-04-04哲學(xué)家就餐問題中的JAVA多線程學(xué)習(xí)
哲學(xué)家就餐問題是1965年由Dijkstra提出的一種線程同步的問題,下面我們就看一下JAVA多線程如何做2013-11-11Java 內(nèi)存模型中的happen-before關(guān)系詳解
這篇文章主要為大家介紹了Java 內(nèi)存模型中的happen-before關(guān)系示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10SpringBoot中的響應(yīng)式web應(yīng)用詳解
這篇文章主要介紹了SpringBoot中的響應(yīng)式web應(yīng)用詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11Spring?Boot項(xiàng)目部署命令java?-jar的各種參數(shù)及作用詳解
這篇文章主要介紹了Spring?Boot項(xiàng)目部署命令java?-jar的各種參數(shù)及作用的相關(guān)資料,包括設(shè)置內(nèi)存大小、垃圾回收器、線程棧大小、系統(tǒng)屬性等,還介紹了SpringBoot專用參數(shù),如修改端口、指定配置文件等,需要的朋友可以參考下2025-04-04Java實(shí)現(xiàn)Excel文件轉(zhuǎn)PDF(無水印無限制)
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言實(shí)現(xiàn)Excel文件轉(zhuǎn)PDF的效果,并可以無水印、無限制。文中的示例代碼講解詳細(xì),需要的可以參考一下2022-06-06