Spring Boot(XML配置)和Jasypt集成

问题描述 投票:0回答:1

我的应用程序仅仅是启动一个ActiveMQ代理。

我想在Spring Boot中使用基于XML的配置,以利用ActiveMQ代理的XML配置(引用here)。

我正在使用jasypt-spring-boot-starter来满足我的加密需求,但似乎在初始化XML配置时我的密码的加密值没有被解密。

启动时没有错误。只是当我尝试使用admin / user访问代理时,它将失败并显示错误“用户名[用户]或密码无效”。

主要的Spring Boot应用程序类

@Configuration
@ComponentScan
@EnableAutoConfiguration
@SpringBootApplication
@RestController
@ImportResource({"classpath:activemq.xml"})
public class Application {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

摘自Broker Config(activemq.xml)

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:amq="http://activemq.apache.org/schema/core"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://activemq.apache.org/schema/core 
    http://activemq.apache.org/schema/core/activemq-core.xsd">

    <broker xmlns="http://activemq.apache.org/schema/core" brokerName="${activemq.broker.name}" dataDirectory="${activemq.broker.data}">

    <plugins>
        <runtimeConfigurationPlugin checkPeriod="1000" />

        <simpleAuthenticationPlugin>
                <users>
                <authenticationUser username="admin" password="${activemq.broker.admin.password}" groups="users,admins" />
                <authenticationUser username="user" password="${activemq.broker.user.password}" groups="users" />
                <authenticationUser username="guest" password="${activemq.broker.guest.password}" groups="guests" />
            </users>
        </simpleAuthenticationPlugin>
    </plugins>

...more

application.properties

jasypt.encryptor.password=thisisnotapassword
jasypt.encryptor.algorithm=PBEWITHMD5ANDTRIPLEDES
activemq.broker.admin.password=ENC(OZRghRNXYpRiiw18KD7P6Uf2Y7fOieI7)
activemq.broker.user.password=ENC(yOiHeJlh6Z+VRVmSZe//Yw==)
activemq.broker.guest.password=guest

我从启动日志中注意到的一件事是,在jasypt相关日志出现之前,activemq.xml被加载

Loading XML bean definitions from class path resource [activemq.xml]
...some logs
String Encryptor custom Bean not found with name 'jasyptStringEncryptor'. Initializing Default String Encryptor
java xml spring encryption jasypt
1个回答
0
投票

这可以通过使用自定义环境来解决,如https://github.com/ulisesbocchio/jasypt-spring-boot中所述:

    new SpringApplicationBuilder()
            .environment(new StandardEncryptableEnvironment())
            .sources(Application.class).run(args);

从README.md:

此方法对于在引导程序上早期访问加密属性很有用。虽然在大多数情况下不需要,但在定制Spring Boot的init行为或与早期配置的某些功能(如Logging配置)集成时可能非常有用。

© www.soinside.com 2019 - 2024. All rights reserved.