Skip to content

Commit aa38d33

Browse files
author
Dave Syer
committed
Support for setting credentials and vhost in rabbit addresses
User can now add credentials, vhost and protocol prefix (amqp://) to any or all of the addresses, extending the format beyond that accepted bu the rabbitmq client, but making it cloud friendly. Only one of the addresses needs those properties and all are optional. Port also defaults to 5672 in an address.
1 parent 148e32d commit aa38d33

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,46 @@ public int getPort() {
7070
}
7171

7272
public void setAddresses(String addresses) {
73-
this.addresses = addresses;
73+
this.addresses = parseAddresses(addresses);
7474
}
7575

7676
public String getAddresses() {
7777
return (this.addresses == null ? this.host + ":" + this.port : this.addresses);
7878
}
7979

80+
private String parseAddresses(String addresses) {
81+
StringBuilder result = new StringBuilder();
82+
for (String address : StringUtils.commaDelimitedListToSet(addresses)) {
83+
address = address.trim();
84+
if (address.startsWith("amqp://")) {
85+
address = address.substring("amqp://".length());
86+
}
87+
if (address.contains("@")) {
88+
String[] split = StringUtils.split(address, "@");
89+
String creds = split[0];
90+
address = split[1];
91+
split = StringUtils.split(creds, ":");
92+
this.username = split[0];
93+
if (split.length > 0) {
94+
this.password = split[1];
95+
}
96+
}
97+
int index = address.indexOf("/");
98+
if (index >= 0 && index < address.length()) {
99+
this.virtualHost = address.substring(index + 1);
100+
address = address.substring(0, index);
101+
}
102+
if (result.length() > 0) {
103+
result.append(",");
104+
}
105+
if (!address.contains(":")) {
106+
address = address + ":" + this.port;
107+
}
108+
result.append(address);
109+
}
110+
return result.length() > 0 ? result.toString() : null;
111+
}
112+
80113
public void setPort(int port) {
81114
this.port = port;
82115
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,34 @@ public void addressesDoubleValued() {
5050
assertEquals(9999, this.properties.getPort());
5151
}
5252

53+
@Test
54+
public void addressesDoubleValuedWithCredentials() {
55+
this.properties.setAddresses("myhost:9999,root:password@otherhost:1111/host");
56+
assertNull(this.properties.getHost());
57+
assertEquals(9999, this.properties.getPort());
58+
assertEquals("root", this.properties.getUsername());
59+
assertEquals("host", this.properties.getVirtualHost());
60+
}
61+
62+
@Test
63+
public void addressesSingleValuedWithCredentials() {
64+
this.properties.setAddresses("amqp://root:password@otherhost:1111/host");
65+
assertEquals("otherhost", this.properties.getHost());
66+
assertEquals(1111, this.properties.getPort());
67+
assertEquals("root", this.properties.getUsername());
68+
assertEquals("host", this.properties.getVirtualHost());
69+
}
70+
71+
@Test
72+
public void addressesSingleValuedWithCredentialsDefaultPort() {
73+
this.properties.setAddresses("amqp://root:[email protected]/host");
74+
assertEquals("lemur.cloudamqp.com", this.properties.getHost());
75+
assertEquals(5672, this.properties.getPort());
76+
assertEquals("root", this.properties.getUsername());
77+
assertEquals("host", this.properties.getVirtualHost());
78+
assertEquals("lemur.cloudamqp.com:5672", this.properties.getAddresses());
79+
}
80+
5381
@Test
5482
public void testDefaultVirtualHost() {
5583
this.properties.setVirtualHost("/");

0 commit comments

Comments
 (0)