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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36619432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:41:18  来源:igfitidea点击:

SpringBoot + ActiveMQ - How to set trusted packages?

javaspring-bootactivemq

提问by AntoineB

I'm creating two springboot server & client applications communicating using JMS, and everything is working fine with the release 5.12.1for activemq, but as soon as I update to the 5.12.3version, I'm getting the following error :

我正在创建两个使用 JMS 进行通信的 springboot 服务器和客户端应用程序,并且5.12.1在 activemq版本中一切正常,但是一旦我更新到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.

I went on the URLthat is provided and I figured out that my issue is related to the new security implemented in the 5.12.2release of ActiveMQ, and I understand that I could fix it by defining the trusted packages, but I have no idea on where to put such a configuration in my SpringBoot project.

我访问提供的 URL,发现我的问题与5.12.2ActiveMQ 版本中实现的新安全性有关,我知道我可以通过定义受信任的包来修复它,但我不知道在哪里把这样的配置放在我的SpringBoot项目中。

The only reference I'm making to the JMS queue in my client and my server is setting up it's URI in application.properties and enabling JMS on my "main" class with @EnableJms, and here's my configuration on the separate broker :

我在客户端和服务器中对 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;
    }
}

So I'd like to know where I'm supposed to specify the trusted packages.

所以我想知道我应该在哪里指定受信任的包。

Thanks :)

谢谢 :)

回答by Mithun

You can just set one of the below spring boot properties in application.propertiesto set trusted packages.

您只需设置以下 spring boot 属性之一application.properties即可设置受信任的包。

spring.activemq.packages.trust-all=true

spring.activemq.packages.trust-all=true

or

或者

spring.activemq.packages.trusted=<package1>,<package2>,<package3>

spring.activemq.packages.trusted=<package1>,<package2>,<package3>

回答by Jim.R

Add the following bean:

添加以下bean:

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

The ability to do this via a configuration property has been added for the next release: https://github.com/spring-projects/spring-boot/issues/5631

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

回答by Sarvesh

Method: public void setTrustedPackages(List<String> trustedPackages)

方法: public void setTrustedPackages(List<String> trustedPackages)

Description: add all packages which is used in send and receive Messageobject.

说明:添加用于发送和接收Message对象的所有包。

Code : connectionFactory.setTrustedPackages(Arrays.asList("org.api","java.util"))

代码 : connectionFactory.setTrustedPackages(Arrays.asList("org.api","java.util"))

Implementated Code:

实施代码:

private static final String DEFAULT_BROKER_URL = "tcp://localhost:61616";

private static final String RESPONSE_QUEUE = "api-response";

@Bean
public ActiveMQConnectionFactory connectionFactory(){
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
    connectionFactory.setBrokerURL(DEFAULT_BROKER_URL);
    connectionFactory.setTrustedPackages(Arrays.asList("org.api","java.util"));
    return connectionFactory;
}

@Bean
public JmsTemplate jmsTemplate(){
    JmsTemplate template = new JmsTemplate();
    template.setConnectionFactory(connectionFactory());
    template.setDefaultDestinationName(RESPONSE_QUEUE);
    return template;
}

回答by Santhosh Hirekerur

I am setting Java_opts something like below and passing to java command and its working for me.

我正在设置 Java_opts 类似下面的内容并传递给 java 命令,它对我有用。

JAVA_OPTS=-Xmx256M -Xms16M -Dorg.apache.activemq.SERIALIZABLE_PACKAGES=*
java $JAVA_OPTS -Dapp.config.location=/data/config -jar <your_jar>.jar --spring.config.location=file:/data/config/<your config file path>.yml

回答by Srikanth Josyula

If any one still looking for an answer, below snippet worked for me

如果有人仍在寻找答案,下面的片段对我有用

@Bean
public ActiveMQConnectionFactory connectionFactory() {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
    connectionFactory.setBrokerURL(BROKER_URL);
    connectionFactory.setPassword(BROKER_USERNAME);
    connectionFactory.setUserName(BROKER_PASSWORD);
    connectionFactory.setTrustAllPackages(true); // all packages are considered as trusted 
    //connectionFactory.setTrustedPackages(Arrays.asList("com.my.package")); // selected packages
    return connectionFactory;
}

回答by rocyuan

Yes I found it's config in the new version

是的,我在新版本中找到了它的配置

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
</parent>

spring:
profiles:
    active: @profileActive@
cache:
  ehcache:
    config: ehcache.xml
activemq:
  packages:
    trusted: com.stylrplus.api.model