Skip to content

Commit 0fedb24

Browse files
markpollackxyloman
authored andcommitted
Support amqps:// URIs in spring.rabbitmq.addresses
See gh-18808 Co-Authored-By: Bryan Kelly <[email protected]>
1 parent ed50bf2 commit 0fedb24

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,10 @@ private static final class Address {
937937

938938
private static final int DEFAULT_PORT = 5672;
939939

940+
private static final String PREFIX_AMQP_SECURE = "amqps://";
941+
942+
private static final int DEFAULT_PORT_SECURE = 5671;
943+
940944
private String host;
941945

942946
private int port;
@@ -947,6 +951,8 @@ private static final class Address {
947951

948952
private String virtualHost;
949953

954+
private boolean isSecureConnection;
955+
950956
private Address(String input) {
951957
input = input.trim();
952958
input = trimPrefix(input);
@@ -956,6 +962,10 @@ private Address(String input) {
956962
}
957963

958964
private String trimPrefix(String input) {
965+
if (input.startsWith(PREFIX_AMQP_SECURE)) {
966+
this.isSecureConnection = true;
967+
return input.substring(PREFIX_AMQP_SECURE.length());
968+
}
959969
if (input.startsWith(PREFIX_AMQP)) {
960970
input = input.substring(PREFIX_AMQP.length());
961971
}
@@ -992,7 +1002,12 @@ private void parseHostAndPort(String input) {
9921002
int portIndex = input.indexOf(':');
9931003
if (portIndex == -1) {
9941004
this.host = input;
995-
this.port = DEFAULT_PORT;
1005+
if (this.isSecureConnection) {
1006+
this.port = DEFAULT_PORT_SECURE;
1007+
}
1008+
else {
1009+
this.port = DEFAULT_PORT;
1010+
}
9961011
}
9971012
else {
9981013
this.host = input.substring(0, portIndex);

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitPropertiesTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ public void customPort() {
7070
assertThat(this.properties.getPort()).isEqualTo(1234);
7171
}
7272

73+
@Test
74+
void usingSecuredConnections() {
75+
this.properties.setAddresses("amqps://root:password@otherhost,amqps://root:password2@otherhost2");
76+
assertThat(this.properties.determinePort()).isEqualTo(5671);
77+
assertThat(this.properties.determineAddresses()).isEqualTo("otherhost:5671,otherhost2:5671");
78+
}
79+
7380
@Test
7481
public void determinePortReturnsPortOfFirstAddress() {
7582
this.properties.setAddresses("rabbit1.example.com:1234,rabbit2.example.com:2345");

0 commit comments

Comments
 (0)