小编典典

SpringBoot + ActiveMQ-如何设置可信包?

spring-boot

我正在创建两个使用JMS进行通信的springboot服务器和客户端应用程序,并且与5.12.1activemq
的发行版都可以正常工作,但是一旦更新到该5.12.3版本,就会出现以下错误:

org.springframework.jms.support.converter.MessageConversionException: Could not convert JMS message; nested exception is javax.jms.JMSException: Failed to build body from content. Serializable class not available to broker. Reason: java.lang.ClassNotFoundException: Forbidden class MyClass! This class is not trusted to be serialized as ObjectMessage payload. Please take a look at http://activemq.apache.org/objectmessage.html for more information on how to configure trusted classes.

我继续使用提供的URL,发现我的问题与5.12.2ActiveMQ发行版中实现的新安全性有关,并且我知道可以通过定义受信任的程序包来解决此问题,但是我不知道在哪里将这样的配置放在我的SpringBoot项目中。

我对客户端和服务器中的JMS队列的唯一参考是在application.properties中设置它的URI,并使用启用“主要”类上的JMS
@EnableJms,这是我在单独代理上的配置:

@Configuration
@ConfigurationProperties(prefix = "activemq")
public class BrokerConfiguration {

    /**
     * Defaults to TCP 10000
     */
    private String connectorURI = "tcp://0.0.0.0:10000";
    private String kahaDBDataDir = "../../data/activemq";

    public String getConnectorURI() {
        return connectorURI;
    }

    public void setConnectorURI(String connectorURI) {
        this.connectorURI = connectorURI;
    }

    public String getKahaDBDataDir() {
        return kahaDBDataDir;
    }

    public void setKahaDBDataDir(String kahaDBDataDir) {
        this.kahaDBDataDir = kahaDBDataDir;
    }

    @Bean(initMethod = "start", destroyMethod = "stop")
    public BrokerService broker() throws Exception {
        KahaDBPersistenceAdapter persistenceAdapter = new KahaDBPersistenceAdapter();
        persistenceAdapter.setDirectory(new File(kahaDBDataDir));

        final BrokerService broker = new BrokerService();
        broker.addConnector(getConnectorURI());
        broker.setPersistent(true);
        broker.setPersistenceAdapter(persistenceAdapter);
        broker.setShutdownHooks(Collections.<Runnable> singletonList(new SpringContextHook()));
        broker.setUseJmx(false);

        final ManagementContext managementContext = new ManagementContext();
        managementContext.setCreateConnector(true);
        broker.setManagementContext(managementContext);

        return broker;
    }
}

因此,我想知道应该在哪里指定受信任的程序包。

谢谢 :)


阅读 500

收藏
2020-05-30

共1个答案

小编典典

添加以下bean:

@Bean
public ActiveMQConnectionFactory activeMQConnectionFactory() {
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("your broker URL");
    factory.setTrustedPackages(Arrays.asList("com.my.package"));
    return factory;
}

下一个版本已添加了通过配置属性执行此操作的功能:https : //github.com/spring-projects/spring-
boot/issues/5631

2020-05-30