SpringBoot RabbitMQ 尝试反序列化未授权类异常

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

我在Springboot项目中使用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 文档

You can set the patterns using the allowedListPatterns property on these converters. Alternatively, if you trust all message originators, you can set the environment variable SPRING_AMQP_DESERIALIZATION_TRUST_ALL or system property spring.amqp.deserialization.trust.all to 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.