Spring Boot RabbitMQ 尝试反序列化未经授权的类异常

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

我在 Spring Boot 项目中使用 RabbitMQ:

sender

@Component
@AllArgsConstructor
public class UserSender {

    private final RabbitTemplate rabbitTemplate;

    public String send() {
        User user = new User(1L, "Tom", "123");
        rabbitTemplate.convertAndSend("userQueue", user);
        return "user sender sent: " + user;
    }
}

Receiver

@Component
public class UserReceiver {

    @RabbitListener(queues = "userQueue")
    @RabbitHandler
    private void process(User user) {
        System.out.println("received user: " + user);
    }
}

启动时出现异常:

Caused by: java.lang.SecurityException: Attempt to deserialize unauthorized class com.example.lab06.entity.User; add allowed class name patterns to the message converter or, if you trust the message orginiator, set environment variable 'SPRING_AMQP_DESERIALIZATION_TRUST_ALL' or system property 'spring.amqp.deserialization.trust.all' to true

我检查了Spring AMPQ 文档

您可以使用这些转换器上的

allowedListPatterns
属性来设置模式。或者,如果您信任所有消息发起者,则可以将环境变量
SPRING_AMQP_DESERIALIZATION_TRUST_ALL
或系统属性
spring.amqp.deserialization.trust.all
设置为
true

但是,我无法在

spring.amqp.deserialization.trust.all
中设置
application.properties
。我收到此错误:

Cannot resolve configuration property 'spring.amqp.deserialization.trust.all'

如何解决?

rabbitmq amqp
1个回答
0
投票

文档指出(强调我的):

...将 环境变量

SPRING_AMQP_DESERIALIZATION_TRUST_ALL
系统属性
spring.amqp.deserialization.trust.all
设置为
true

application.properties
中配置的值不是环境变量或系统属性。环境变量在您的环境中设置,系统属性通常作为
-D
参数传递到 JVM。

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