From a73293350aaecf50ef54f7994f94212543fd4102 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 1 Oct 2021 11:30:11 +0100 Subject: [PATCH] Fix parsing of portless IPv6 spring.rabbitmq.addresses Closes gh-28133 --- .../boot/autoconfigure/amqp/RabbitProperties.java | 9 +++++---- .../boot/autoconfigure/amqp/RabbitPropertiesTests.java | 9 ++++++++- 2 files changed, 13 insertions(+), 5 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 81e3a76efc..f9e43f4290 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 @@ -1083,14 +1083,15 @@ public class RabbitProperties { } private void parseHostAndPort(String input, boolean sslEnabled) { - int portIndex = input.lastIndexOf(':'); - if (portIndex == -1) { + int bracketIndex = input.lastIndexOf(']'); + int colonIndex = input.lastIndexOf(':'); + if (colonIndex == -1 || colonIndex < bracketIndex) { this.host = input; this.port = (determineSslEnabled(sslEnabled)) ? DEFAULT_PORT_SECURE : DEFAULT_PORT; } else { - this.host = input.substring(0, portIndex); - this.port = Integer.parseInt(input.substring(portIndex + 1)); + this.host = input.substring(0, colonIndex); + this.port = Integer.parseInt(input.substring(colonIndex + 1)); } } 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 331c5bbc9e..9dd47e9fce 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 @@ -242,7 +242,14 @@ class RabbitPropertiesTests { @Test void ipv6Address() { - this.properties.setAddresses("amqp://foo:bar@[aaaa:bbbb:cccc::d]:5672"); + this.properties.setAddresses("amqp://foo:bar@[aaaa:bbbb:cccc::d]:1234"); + assertThat(this.properties.determineHost()).isEqualTo("[aaaa:bbbb:cccc::d]"); + assertThat(this.properties.determinePort()).isEqualTo(1234); + } + + @Test + void ipv6AddressDefaultPort() { + this.properties.setAddresses("amqp://foo:bar@[aaaa:bbbb:cccc::d]"); assertThat(this.properties.determineHost()).isEqualTo("[aaaa:bbbb:cccc::d]"); assertThat(this.properties.determinePort()).isEqualTo(5672); }