【发布时间】:2021-10-30 17:04:30
【问题描述】:
我在 Spring Boot 应用程序中有一个活动的 MQ 工厂,输入 ActiveMQConnectionFactory。
在实例化它之后,我将受信任的包设置为 java.lang,因为我希望能够在我的应用程序中通过 JMS 发送/接收字符串。
参见此处:
SpringBoot + ActiveMQ - How to set trusted packages?
https://stackoverflow.com/a/36622567/2300597
@Bean(name="jmsConnectionFactory")
public ConnectionFactory getJMSConnectionFactory(){
_factory = new ActiveMQConnectionFactory(active_MQ_BrokerURL);
ActiveMQConnectionFactory fct = ((ActiveMQConnectionFactory)_factory);
fct.setTrustAllPackages(false);
fct.setTrustedPackages(Arrays.asList("java.lang"));
return _factory;
}
但我可以发送其他对象(不是来自包 java.lang)?!
发送它们时,我没有收到某种Exception。
这是为什么呢?我做错了什么?
注意:我正在使用org.springframework.jms.core.JmsTemplate 发送消息
(以防万一)。该模板已链接到该工厂。
public void sendObjectMessage(final Serializable msg) {
try {
this.jmsTemplate.send(this.queue, new MessageCreator() {
public Message createMessage(final Session session) throws JMSException {
return session.createObjectMessage(msg);
}
});
logger.info("Success sending Object MQ message [{}]", msg);
} catch (Exception e) {
logger.error("Error sending Object MQ message [{}]", msg, e);
}
}
【问题讨论】:
-
FWIW,
ObjectMessage对象依赖于 Java 序列化来编组和解组其对象有效负载。这个过程通常被认为是不安全的(因此需要明确地“信任”某些包)。还有一些other issues 使用与安全性无关的JMSObjectMessage,这使其成为大多数用例的糟糕选择。如果可能的话,我会鼓励你避免使用ObjectMessage。鉴于您只想使用String,我建议使用java.jms.TextMessage。
标签: java spring-boot jms activemq