代码之家  ›  专栏  ›  技术社区  ›  Kris Swat

EncryptablePropertyPlaceholderConfigurer的工作原理

  •  0
  • Kris Swat  · 技术社区  · 6 年前

    我在数据库中有一个加密的密码,在application.properties中有一个占位符 分贝

    KPW=ENC(somebigencryptedpwd=)
    

    应用程序.属性

    KPW=${KPW}
    

    豆: @豆子

    public static PlaceholderManager getPropertyConfigurer() throws IOException{
        PlaceholderManager propUtils = new PlaceholderManager(); //this class extends PropertyPlaceholderConfigurer and override postProcessBeanFactory
        propUtils.setDataSourceName("DSXA");
        propUtils.setLocalOverride(true);
        return propUtils;
    }
    
    @Bean
    public static EnvironmentStringPBEConfig environmentVariablesConfiguration() {
        EnvironmentStringPBEConfig environmentVariablesConfiguration = new EnvironmentStringPBEConfig();
        environmentVariablesConfiguration.setAlgorithm("PBEWithMD5AndDES");
        environmentVariablesConfiguration.setPasswordEnvName("APP_ENCRYPTION_PASSWORD");
        return environmentVariablesConfiguration;
    }
    
    @Bean
    public static PBEStringEncryptor configurationEncryptor() {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        encryptor.setConfig(environmentVariablesConfiguration());  
        return encryptor;
    }
    
     @Bean
     public static PropertyPlaceholderConfigurer propertyConfigurer() {
         EncryptablePropertyPlaceholderConfigurer propertyConfigurer = new EncryptablePropertyPlaceholderConfigurer(configurationEncryptor());
         propertyConfigurer.setLocation(new ClassPathResource("application.properties"));
         // propertyConfigurer.setLocation(resource);
         return propertyConfigurer;
     }
    

    用法:

    <bean id="keSender" class="org.springframework.ws.transport.http.HttpComponentsMessageSender">
            <property name="credentials">
                <bean class="org.apache.http.auth.UsernamePasswordCredentials">
                    <constructor-arg value="${KID}"/>
                    <constructor-arg value="${KPW}"/>
                </bean>
            </property>
        </bean>
    

    如果我使用@Value注入KPW并使用端点返回它,我将从DB获取该值。但是我如何在加密的占位符后打印密码,看它是否被解密并且是正确的

    0 回复  |  直到 6 年前