小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

EnvironmentPostProcessor怎么做單元測試?阿里P7解答

 路人甲Java 2021-05-17

簡介

從Spring Boot 1.3開始,我們可以在應用程序上下文刷新之前使用EnvironmentPostProcessor來自定義應用程序的Environment。Environment表示當前應用程序運行的環(huán)境,它可以統(tǒng)一訪問各種屬性源中的屬性,如屬性文件、JVM系統(tǒng)屬性、系統(tǒng)環(huán)境變量和Servlet上下文參數(shù)。使用EnvironmentPostProcessor可以在bean初始化之前對Environment進行修改。

文章持續(xù)更新,微信搜索「萬貓學社」第一時間閱讀,關注后回復「電子書」,免費獲取12本Java必讀技術書籍。

使用示例

讓我們設想一個需求,配置文件中的數(shù)據(jù)庫密碼是加密后的密文,如:

spring.datasource.password=js8sbAwkduzPTEWQrlDbTw==

在應用啟動時,對密文進行解密后再進行數(shù)據(jù)庫的連接。

針對這種需求,就可以通過EnvironmentPostProcessor對密文進行解密,重新放到Environment中。

文章持續(xù)更新,微信搜索「萬貓學社」第一時間閱讀,關注后回復「電子書」,免費獲取12本Java必讀技術書籍。

1.實現(xiàn)EnvironmentPostProcessor

package one.more;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;

import java.util.Properties;

public class DecodeEnvironmentPostProcessor implements EnvironmentPostProcessor {

    public static final String SPRING_DATASOURCE_PASSWORD = "spring.datasource.password";
    public static final String AES_SECRET = "OneMore";

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        String password = environment.getProperty(SPRING_DATASOURCE_PASSWORD);
        Properties properties = new Properties();
        properties.setProperty(SPRING_DATASOURCE_PASSWORD, AESUtil.decrypt(password, AES_SECRET));
        PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource(SPRING_DATASOURCE_PASSWORD,
                properties);
        environment.getPropertySources().addFirst(propertiesPropertySource);
    }
}

如果你希望EnvironmentPostProcessor按照特定的順序被調用,可以實現(xiàn)Ordered接口,或者使用@Order注解。

2.注冊實現(xiàn)類

想要在Spring Boot啟動過程中調用這個實現(xiàn)類,我們還需要在META-INF/ Spring .factories中注冊這個實現(xiàn)類:

org.springframework.boot.env.EnvironmentPostProcessor=
  one.more.DecodeEnvironmentPostProcessor

文章持續(xù)更新,微信搜索「萬貓學社」第一時間閱讀,關注后回復「電子書」,免費獲取12本Java必讀技術書籍。

單元測試

下面介紹本文的重點:怎么做EnvironmentPostProcessor實現(xiàn)類的單元測試,話不多說,直接上代碼:

package one.more;

import org.junit.Assert;
import org.junit.Test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;

import java.util.Properties;

public class DecodeEnvironmentPostProcessorTest {

    @Test
    public void testPostProcessEnvironment() {
        DecodeEnvironmentPostProcessor processor = new DecodeEnvironmentPostProcessor();
        String password = "one-more";
        Properties properties = new Properties();
        properties.setProperty(DecodeEnvironmentPostProcessor.SPRING_DATASOURCE_PASSWORD,
                AESUtil.encrypt(password, DecodeEnvironmentPostProcessor.AES_SECRET));
        ConfigurableEnvironment environment = getEnvironment(processor, properties);

        Assert.assertEquals(password,
                environment.getProperty(DecodeEnvironmentPostProcessor.SPRING_DATASOURCE_PASSWORD));

    }

    /**
     * 獲取一個經過EnvironmentPostProcessor處理過的Environment
     *
     * @param processor  EnvironmentPostProcessor實現(xiàn)類的實例
     * @param properties 預置準備做單元測試的屬性
     * @return 處理過的Environment
     */
    private ConfigurableEnvironment getEnvironment(EnvironmentPostProcessor processor, Properties properties) {
        // 創(chuàng)建一個SpringApplication
        SpringApplication springApplication = new SpringApplicationBuilder()
                .sources(DecodeEnvironmentPostProcessor.class)
                .web(WebApplicationType.NONE).build();
        // 獲取應用上下文
        ConfigurableApplicationContext context = springApplication.run();
        // 獲取Environment
        ConfigurableEnvironment environment = context.getEnvironment();
        //添加準備做單元測試的屬性
        environment.getPropertySources()
                .addFirst(new PropertiesPropertySource("test", properties));
        processor.postProcessEnvironment(environment, springApplication);
        context.close();
        return environment;
    }
}

文章持續(xù)更新,微信搜索「萬貓學社」第一時間閱讀,關注后回復「電子書」,免費獲取12本Java必讀技術書籍。

附:加解密工具類代碼

package one.more;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class AESUtil {

    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
    private static final String KEY_ALGORITHM = "AES";

    public static String encrypt(String content, String password) {
        try {
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            byte[] byteContent = content.getBytes("utf-8");
            cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(password));
            byte[] result = cipher.doFinal(byteContent);
            return Base64.encodeBase64String(result);
        } catch (Exception ex) {

        }
        return null;
    }

    public static String decrypt(String content, String password) {
        try {
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, getSecretKey(password));
            byte[] result = cipher.doFinal(Base64.decodeBase64(content));
            return new String(result, "utf-8");
        } catch (Exception ex) {
        }
        return null;
    }

    private static SecretKeySpec getSecretKey(final String password) {
        KeyGenerator kg = null;
        try {
            kg = KeyGenerator.getInstance(KEY_ALGORITHM);
            kg.init(128, new SecureRandom(password.getBytes()));
            SecretKey secretKey = kg.generateKey();
            return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);
        } catch (NoSuchAlgorithmException ex) {

        }
        return null;
    }
}

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多