From 01a39aaf6aa14655c991392a8b60cf4fec7d2633 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sun, 26 Sep 2021 13:04:13 -0700 Subject: [PATCH] Support IPv6 addresses in spring.rabbitmq.addresses Refine parsing logic in `RabbitProperties` so that IPv6 addresses can be used. Fixes gh-28133 --- .../boot/autoconfigure/amqp/RabbitProperties.java | 4 ++-- .../boot/autoconfigure/amqp/RabbitPropertiesTests.java | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java index 7511b372401..81e3a76efc6 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -1083,7 +1083,7 @@ public class RabbitProperties { } private void parseHostAndPort(String input, boolean sslEnabled) { - int portIndex = input.indexOf(':'); + int portIndex = input.lastIndexOf(':'); if (portIndex == -1) { this.host = input; this.port = (determineSslEnabled(sslEnabled)) ? DEFAULT_PORT_SECURE : DEFAULT_PORT; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java index 7eded032ec4..331c5bbc9e3 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -240,6 +240,13 @@ class RabbitPropertiesTests { .isEqualTo("user:secret@rabbit1.example.com:1234/alpha,rabbit2.example.com"); } + @Test + void ipv6Address() { + this.properties.setAddresses("amqp://foo:bar@[aaaa:bbbb:cccc::d]:5672"); + assertThat(this.properties.determineHost()).isEqualTo("[aaaa:bbbb:cccc::d]"); + assertThat(this.properties.determinePort()).isEqualTo(5672); + } + @Test void determineAddressesReturnsAddressesWithJustHostAndPort() { this.properties.setAddresses("user:secret@rabbit1.example.com:1234/alpha,rabbit2.example.com");