簡體   English   中英

在沒有jasypt的情況下解密application.properties中的密碼

[英]Decrypt password in application.properties WITHOUT jasypt

我有spring-boot 2.0應用程序,其中application.properties中具有加密的屬性,如下所示

spring.jpa.hibernate.ddl-auto=none
spring.jpa.database=mysql
spring.datasource.url=mysql_url
spring.datasource.username=username
spring.datasource.password=ENC:encrypted_password

spring.redis.host=111.1.1.1
spring.redis.port=18729
spring.redis.password=ENC:redis_encrypted_password

我有自己的應用程序自定義加密和解密機制,並且不想使用jasypt lib。

現在,我擴展了PropertyPlaceholderConfigurer並進行了以下配置

/**
 * Decrypt passwrods in properties if they are already decrypted.
 * If property starts with ENC: then only decrypt and return the property value
 *
 */
public class EncryptablePropertyPlaceholderConfigurer
        extends PropertyPlaceholderConfigurer {

    /**
     * Decrypt password if its encrypted. If property starts with ENC: then its considered as encrypted.
     * @param originalValue
     * @return decrypted value if encrypted
     */
    @Override
    protected String convertPropertyValue(String originalValue) {
        if(originalValue!=null && originalValue.trim().startsWith("ENC:")) {
            String enc = originalValue.split("ENC:")[1];
            System.out.println(originalValue.split("ENC:")[1]);
            originalValue = MySecurityUtilsClass.decryptPassword(enc);
            System.out.println(" Decrypted passwrod "+ originalValue);
        }
        return originalValue;
    }
}

和配置

@Configuration
public class AppBeanConfig {

    @Bean
    public static PropertyPlaceholderConfigurer placeHolderConfigurerName() throws IOException {
        PropertyPlaceholderConfigurer props = new EncryptablePropertyPlaceholderConfigurer();
        props.setSystemPropertiesMode( PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_NEVER );
        //add more properties as Required
        props.setLocation(new PathMatchingResourcePatternResolver().getResource("classpath:/application.properties"));
        return props;
    }

}

調用了方法convertPropertyValue,但屬性未解密,並且mysql連接失敗並輸入了錯誤的密碼。

幫助將適用於正確的配置方式,而無需使用jasypt lib

在為placeHolderConfigurerName()方法在@Bean之后添加注釋@Primary后,嘗試運行程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM