Home > database >  Unauthorized access to the RabbitMQ API
Unauthorized access to the RabbitMQ API

Time:11-30

I am trying to accomplish each time when I start my app, to get from the RabbitMQ API, all the queues and exchanges that exist. But unfortunately I get the following error:

org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 Unauthorized: [no body]

My code:

application properties file :

spring.rabbitmq.host=localhost

spring.rabbitmq.port=5672

spring.rabbitmq.username=guest

spring.rabbitmq.password=guest

spring.rabbitmq.addresses=localhost:8080/api/

rabbitmq-api-url = http://localhost:8080/api/

Component that executes each time on startup:

@Component
public class StartupCLass implements ApplicationListener<ApplicationReadyEvent> {

    @Value("${rabbitmq-api-url}")
    private String endpoint;


    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.getForEntity(endpoint "queues",String.class);
    }
}

What I am trying to accomplish is the following:

I have some queues and exchanges defined on my RabbitMQ server. I have also exported the definitions json file and modified it with renaming some of the queues/exchanges. I want to check when starting the app, which queue or exchange has been renamed regarding the json file and the current state of the rabbit mq server (that's why I want to get from the API the queues)

CodePudding user response:

401 means unauthorized. So you have to pass username and password in the request.

restTemplate.getInterceptors().add(
  new BasicAuthorizationInterceptor("username", "password"));

Read more here: https://www.baeldung.com/how-to-use-resttemplate-with-basic-authentication-in-spring

  • Related