在Springboot中创建自定义Jasypt PropertySource
我正在使用Spring
Boot创建一个访问数据库的简单Web应用程序。我通过在中设置spring.datasource.*属性来利用DataSource的自动配置功能application.properties。一切都很棒,而且非常快-
伟大的工作人员@ Spring!
我公司的政策是不应使用明文密码。因此,我需要进行sping.datasource.password加密。经过一番挖掘之后,我决定创建一个org.springframework.boot.env.PropertySourceLoader实现,该实现将创建一个jasypt
org.jasypt.spring31.properties.EncryptablePropertiesPropertySource,如下所示:
public class EncryptedPropertySourceLoader implements PropertySourceLoader{
    private final StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
    public EncryptedPropertySourceLoader()
    {
        //TODO: this could be taken from an environment variable
        this.encryptor.setPassword("password"); 
    }
    @Override
    public String[] getFileExtensions()
    {
        return new String[]{"properties"};
    }
    @Override
    public PropertySource<?> load(final String name, final Resource resource, final String profile) throws IOException
    {
        if (profile == null)
        {
            final Properties props = PropertiesLoaderUtils.loadProperties(resource);
            if (!props.isEmpty())
            {
                return new EncryptablePropertiesPropertySource(name, props, this.encryptor);
            }
        }
        return null;
    }
}
然后,我将其包装在它自己的jar中,并带有一个META-INF/spring.factories文件,如下所示:
org.springframework.boot.env.PropertySourceLoader=com.mycompany.spring.boot.env.EncryptedPropertySourceLoader当从Maven使用运行时,此方法非常有效mvn spring-boot:run。当我使用进行独立战争时会发生问题java -jar my-
app.war。该应用程序仍然加载,但是当我尝试连接数据库时失败,因为密码值仍然被加密。添加日志记录将显示EncryptedPropertySourceLoader从未加载。
对我来说,这听起来像是类路径问题。在maven下运行时,jar的加载顺序很严格,但是一旦进入了tomcat的tomcat,就无话可说,我的自定义jar应该在Spring
Boot之前加载。
我尝试将以下内容添加到我的pom.xml中,以确保保留classpth,但它似乎没有任何效果。
<build>    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <archive>
                        <manifest>
                            <mainClass>${start-class}</mainClass>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
有人有什么想法吗?提前致谢。
向前迈进了一步:我已经设法通过使EncryptedPropertySourceLoader类实现org.springframework.core.PriorityOrdered接口并HIGHEST_PRECEDENCE从中返回来解决此问题getOrder()。现在,这已解决了不使用PropertySourceLoader的问题。但是,现在尝试解密属性时会引发以下错误:
org.jasypt.exceptions.EncryptionInitializationException: java.security.NoSuchAlgorithmException: PBEWithMD5AndDES SecretKeyFactory not available    at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.initialize(StandardPBEByteEncryptor.java:716)
    at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.initialize(StandardPBEStringEncryptor.java:553)
    at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.decrypt(StandardPBEStringEncryptor.java:705)
    at org.jasypt.properties.PropertyValueEncryptionUtils.decrypt(PropertyValueEncryptionUtils.java:72)
    at org.jasypt.properties.EncryptableProperties.decode(EncryptableProperties.java:230)
    at org.jasypt.properties.EncryptableProperties.get(EncryptableProperties.java:209)
    at org.springframework.core.env.MapPropertySource.getProperty(MapPropertySource.java:36)
    at org.springframework.boot.env.EnumerableCompositePropertySource.getProperty(EnumerableCompositePropertySource.java:49)
    at org.springframework.boot.context.config.ConfigFileApplicationListener$ConfigurationPropertySources.getProperty(ConfigFileApplicationListener.java:490)
同样,从运行时不会发生这种情况,mvn spring-
boot:run但是从可执行war文件运行时会发生这种情况。两种方案都使用相同的JVM(jdk1.6.0_35)。Google /
Stackoverflow上的结果表明这是Java安全策略的一个问题,但是从maven运行时它确实可以工作,我想我可以对此予以保留。可能是包装问题…
回答:
这里有两个问题。
1)EncryptedPropertySourceLoader的加载需要高于标准PropertiesPropertySourceLoader的加载。这可以通过实现PriorityOrder接口来实现,如下所示:
public class EncryptedPropertySourceLoader implements PropertySourceLoader, PriorityOrdered{
    private final StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
    public EncryptedPropertySourceLoader()
    {
        this.encryptor.setPassword("password"); //TODO: this could be taken from an environment variable
    }
    @Override
    public String[] getFileExtensions()
    {
        return new String[]{"properties"};
    }
    @Override
    public PropertySource<?> load(final String name, final Resource resource, final String profile) throws IOException
    {
        if (profile == null)
        {
            //load the properties
            final Properties props = PropertiesLoaderUtils.loadProperties(resource);
            if (!props.isEmpty())
            {
                //create the encryptable properties property source
                return new EncryptablePropertiesPropertySource(name, props, this.encryptor);
            }
        }
        return null;
    }
    @Override
    public int getOrder()
    {
        return HIGHEST_PRECEDENCE;
    }
}
使用将从订单中org.springframework.core.io.support.SpringFactoriesLoader加载结果的类。意味着应首先返回此类,并将负责为*
.proerpties文件提供PropertySourceLoader实现。org.springframework.boot.env.PropertySourceLoader``META-
INF/spring.factories``org.springframework.core.OrderComparator
2)第二个问题是可执行JAR / WAR的类加载问题,这似乎是由Windows上的Spring
Boot版本1.1.2.RELEASE中的错误引起的。降至版本1.1.1.RELEASE或版本1.1.3RELEASE解决了在maven外部运行时类和属性文件未加载的各种问题。
以上是 在Springboot中创建自定义Jasypt PropertySource 的全部内容, 来源链接: utcz.com/qa/405293.html






