0

我正在尝试从 springboot 应用程序向 RabbitMQ 发送消息。使用的依赖项(springboot & amqp):

        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>2.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>

我可以使用具有以下访问权限(读取、写入和配置)的用户凭据将消息发布到交换:

具有配置访问权限的用户

但是当我使用没有配置权限的用户时它会失败,如下所示:

没有配置访问权限的用户

调用时收到错误rabbitTemplate.convertAndSend(exchange, null, message);

CachingConnectionFactory       : Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=403, reply-text=ACCESS_REFUSED - access to exchange 'MyExchangeName' in vhost 'my_vHost' refused for user 'my_user', class-id=40, method-id=10

我认为这是因为 Spring 它试图创建交换或检查交换是否存在(使用管理 API)。如果是这种情况,我们可以使用某些属性禁用它吗?

4

1 回答 1

0

删除任何Exchange/Queue/Binding @Beans 或将shouldDeclare属性设置为false

@SpringBootApplication
public class So62316257Application {

    public static void main(String[] args) {
        SpringApplication.run(So62316257Application.class, args);
    }

    @Bean
    public DirectExchange exchange() {
        DirectExchange exchange = new DirectExchange("foo");
        exchange.setShouldDeclare(false);
        return exchange;
    }

    @Bean
    public ApplicationRunner runner(RabbitTemplate template) {
        return args -> {
            template.convertAndSend("foo", "", "bar");
        };
    }

}
于 2020-06-11T12:48:54.187 回答