【问题标题】:Spring Boot (XML Configuration) & Jasypt IntegrationSpring Boot(XML 配置)和 Jasypt 集成
【发布时间】:2017-07-31 11:23:31
【问题描述】:

我的应用程序只是启动一个 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

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

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

【问题讨论】:

  • 为什么要在 Spring Boot 中使用 xml 配置?
  • 如前所述,这是为了利用 ActiveMQ 代理的 XML 配置。除了配置(xml/properties)外,无需修改应用程序。
  • 问题在于您使用 XML 支持命名空间。占位符的实际解析现在是通过命名空间完成的,而不是常规的 Spring 机制。因此,它将在 Spring Boot 更改解码属性之前被调用。
  • 有没有办法不使用命名空间支持?
  • @acys 你有解决办法吗?

标签: java xml spring encryption jasypt


【解决方案1】:

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

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

来自 README.md:

此方法对于早期访问上的加密属性很有用 引导程序。虽然在大多数情况下不需要,但在以下情况下可能很有用 自定义 Spring Boot 的初始化行为或与某些集成 很早就配置的功能,例如日志记录 配置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 2016-12-22
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    相关资源
    最近更新 更多