最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何用Java實(shí)現(xiàn).env文件讀取敏感數(shù)據(jù)

 更新時(shí)間:2025年02月06日 11:44:39   作者:S-X-S  
這篇文章主要介紹了如何用Java實(shí)現(xiàn).env文件讀取敏感數(shù)據(jù),并提供了一個(gè)自動(dòng)配置類(lèi)EnvAutoConfiguration,common-env-starter-demo模塊展示了如何配置和啟動(dòng)一個(gè)簡(jiǎn)單的Spring Boot應(yīng)用程序,需要的朋友可以參考下

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)配置類(lèi)

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)配置類(lèi)
 *
 * @Author sun
 * @Create 2025/1/10 19:57
 * @Version 1.0
 */
@Configuration
@EnableConfigurationProperties({EnvProperties.class}) // 啟用配置類(lèi)
@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ù)根目錄開(kāi)始(默認(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配置類(lèi),測(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)類(lèi)

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)文章

  • 使用FormData上傳二進(jìn)制文件、對(duì)象、對(duì)象數(shù)組方式

    使用FormData上傳二進(jìn)制文件、對(duì)象、對(duì)象數(shù)組方式

    這篇文章主要介紹了使用FormData上傳二進(jìn)制文件、對(duì)象、對(duì)象數(shù)組方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • SpringBoot項(xiàng)目啟動(dòng)時(shí)增加自定義Banner的簡(jiǎn)單方法

    SpringBoot項(xiàng)目啟動(dòng)時(shí)增加自定義Banner的簡(jiǎn)單方法

    最近看到springboot可以自定義啟動(dòng)時(shí)的banner,然后自己試了一下,下面這篇文章主要給大家介紹了SpringBoot項(xiàng)目啟動(dòng)時(shí)增加自定義Banner的簡(jiǎn)單方法,需要的朋友可以參考下
    2022-01-01
  • SpringBoot+Redis+布隆過(guò)濾器防止緩存穿透

    SpringBoot+Redis+布隆過(guò)濾器防止緩存穿透

    本文介紹了一個(gè)基于Spring Boot、Redis和Guava布隆過(guò)濾器的高并發(fā)系統(tǒng)緩存穿透解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-09-09
  • SpringBoot使用Redis對(duì)用戶IP進(jìn)行接口限流的項(xiàng)目實(shí)踐

    SpringBoot使用Redis對(duì)用戶IP進(jìn)行接口限流的項(xiàng)目實(shí)踐

    本文主要介紹了SpringBoot使用Redis對(duì)用戶IP進(jìn)行接口限流,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • Java 生成二維碼的工具資料整理

    Java 生成二維碼的工具資料整理

    本文主要介紹Java 生成二維碼的幾種方法,這里給大家詳細(xì)介紹了java生成二維碼的三種工具,并附有示例代碼供大家參考,開(kāi)發(fā)java 二維碼的朋友可以參考下
    2016-08-08
  • Java并發(fā)工具類(lèi)LongAdder原理實(shí)例解析

    Java并發(fā)工具類(lèi)LongAdder原理實(shí)例解析

    這篇文章主要介紹了Java并發(fā)工具類(lèi)LongAdder原理實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Java打成各種壓縮包的方法詳細(xì)匯總

    Java打成各種壓縮包的方法詳細(xì)匯總

    在工作過(guò)程中,需要將一個(gè)文件夾生成壓縮文件,然后提供給用戶下載,下面這篇文章主要給大家介紹了關(guān)于Java打成各種壓縮包的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-06-06
  • spring boot配置攔截器代碼實(shí)例

    spring boot配置攔截器代碼實(shí)例

    這篇文章主要介紹了spring boot配置攔截器代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Springboot整合阿里巴巴SMS的實(shí)現(xiàn)示例

    Springboot整合阿里巴巴SMS的實(shí)現(xiàn)示例

    本文主要介紹了Springboot整合阿里巴巴SMS的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-12-12
  • spring boot thymeleaf 圖片上傳web項(xiàng)目根目錄操作步驟

    spring boot thymeleaf 圖片上傳web項(xiàng)目根目錄操作步驟

    這篇文章主要介紹了spring boot thymeleaf 圖片上傳web項(xiàng)目根目錄步驟,本文給大家提到了thymeleaf的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2018-03-03

最新評(píng)論

徐闻县| 临汾市| 阳信县| 明光市| 博客| 铜陵市| 志丹县| 多伦县| 信阳市| 凤庆县| 冀州市| 信丰县| 绵阳市| 阜新| 神池县| 尚志市| 都昌县| 高碑店市| 巨鹿县| 西盟| 涿鹿县| 宜州市| 平果县| 临邑县| 武汉市| 桐城市| 安多县| 桂东县| 淮滨县| 清水县| 凌云县| 高台县| 彩票| 东宁县| 竹溪县| 康定县| 绍兴县| 永宁县| 邵阳县| 大连市| 绥江县|