Skip to content

Commit 9b3d625

Browse files
committed
Merge pull request #18808 from markpollack
* pr/18808: Polish "Support amqps:// URIs in spring.rabbitmq.addresses" Support amqps:// URIs in spring.rabbitmq.addresses Closes gh-18808
2 parents ed50bf2 + 4d1373c commit 9b3d625

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private RabbitConnectionFactoryBean getRabbitConnectionFactoryBean(RabbitPropert
125125
map.from(properties::getRequestedHeartbeat).whenNonNull().asInt(Duration::getSeconds)
126126
.to(factory::setRequestedHeartbeat);
127127
RabbitProperties.Ssl ssl = properties.getSsl();
128-
if (ssl.isEnabled()) {
128+
if (ssl.determineEnabled()) {
129129
factory.setUseSSL(true);
130130
map.from(ssl::getAlgorithm).whenNonNull().to(factory::setSslAlgorithm);
131131
map.from(ssl::getKeyStoreType).to(factory::setKeyStoreType);

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ public Template getTemplate() {
310310
return this.template;
311311
}
312312

313-
public static class Ssl {
313+
public class Ssl {
314314

315315
/**
316316
* Whether to enable SSL support.
@@ -366,6 +366,21 @@ public boolean isEnabled() {
366366
return this.enabled;
367367
}
368368

369+
/**
370+
* Returns whether SSL is enabled from the first address, or the configured ssl
371+
* enabled flag if no addresses have been set.
372+
* @return whether ssl is enabled
373+
* @see #setAddresses(String)
374+
* @see #isEnabled()
375+
*/
376+
public boolean determineEnabled() {
377+
if (CollectionUtils.isEmpty(RabbitProperties.this.parsedAddresses)) {
378+
return isEnabled();
379+
}
380+
Address address = RabbitProperties.this.parsedAddresses.get(0);
381+
return address.secureConnection;
382+
}
383+
369384
public void setEnabled(boolean enabled) {
370385
this.enabled = enabled;
371386
}
@@ -937,6 +952,10 @@ private static final class Address {
937952

938953
private static final int DEFAULT_PORT = 5672;
939954

955+
private static final String PREFIX_AMQP_SECURE = "amqps://";
956+
957+
private static final int DEFAULT_PORT_SECURE = 5671;
958+
940959
private String host;
941960

942961
private int port;
@@ -947,6 +966,8 @@ private static final class Address {
947966

948967
private String virtualHost;
949968

969+
private boolean secureConnection;
970+
950971
private Address(String input) {
951972
input = input.trim();
952973
input = trimPrefix(input);
@@ -956,6 +977,10 @@ private Address(String input) {
956977
}
957978

958979
private String trimPrefix(String input) {
980+
if (input.startsWith(PREFIX_AMQP_SECURE)) {
981+
this.secureConnection = true;
982+
return input.substring(PREFIX_AMQP_SECURE.length());
983+
}
959984
if (input.startsWith(PREFIX_AMQP)) {
960985
input = input.substring(PREFIX_AMQP.length());
961986
}
@@ -992,7 +1017,7 @@ private void parseHostAndPort(String input) {
9921017
int portIndex = input.indexOf(':');
9931018
if (portIndex == -1) {
9941019
this.host = input;
995-
this.port = DEFAULT_PORT;
1020+
this.port = (this.secureConnection) ? DEFAULT_PORT_SECURE : DEFAULT_PORT;
9961021
}
9971022
else {
9981023
this.host = input.substring(0, portIndex);

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ public void determinePortReturnsDefaultAmqpPortWhenFirstAddressHasNoExplicitPort
8989
assertThat(this.properties.determinePort()).isEqualTo(5672);
9090
}
9191

92+
@Test
93+
public void determinePortUsingAmqpReturnsPortOfFirstAddress() {
94+
this.properties.setAddresses("amqp://root:password@otherhost,amqps://root:password2@otherhost2");
95+
assertThat(this.properties.determinePort()).isEqualTo(5672);
96+
}
97+
98+
@Test
99+
public void determinePortUsingAmqpsReturnsPortOfFirstAddress() {
100+
this.properties.setAddresses("amqps://root:password@otherhost,amqp://root:password2@otherhost2");
101+
assertThat(this.properties.determinePort()).isEqualTo(5671);
102+
}
103+
92104
@Test
93105
public void virtualHostDefaultsToNull() {
94106
assertThat(this.properties.getVirtualHost()).isNull();
@@ -222,6 +234,24 @@ public void determineAddressesUsesHostAndPortPropertiesWhenNoAddressesSet() {
222234
assertThat(this.properties.determineAddresses()).isEqualTo("rabbit.example.com:1234");
223235
}
224236

237+
@Test
238+
public void determineSslUsingAmqpsReturnsStateOfFirstAddress() {
239+
this.properties.setAddresses("amqps://root:password@otherhost,amqp://root:password2@otherhost2");
240+
assertThat(this.properties.getSsl().determineEnabled()).isTrue();
241+
}
242+
243+
@Test
244+
public void determineSslUsingAmqpReturnsStateOfFirstAddress() {
245+
this.properties.setAddresses("amqp://root:password@otherhost,amqps://root:password2@otherhost2");
246+
assertThat(this.properties.getSsl().determineEnabled()).isFalse();
247+
}
248+
249+
@Test
250+
public void determineSslReturnFlagPropertyWhenNoAddresses() {
251+
this.properties.getSsl().setEnabled(true);
252+
assertThat(this.properties.getSsl().determineEnabled()).isTrue();
253+
}
254+
225255
@Test
226256
public void simpleContainerUseConsistentDefaultValues() {
227257
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4762,6 +4762,13 @@ For example, you might declare the following section in `application.properties`
47624762
spring.rabbitmq.password=secret
47634763
----
47644764

4765+
Alternatively, you could configure the same connection using the `addresses` attributes:
4766+
4767+
[source,properties,indent=0]
4768+
----
4769+
spring.rabbitmq.addresses=amqp://admin:secret@localhost
4770+
----
4771+
47654772
If a `ConnectionNameStrategy` bean exists in the context, it will be automatically used to name connections created by the auto-configured `ConnectionFactory`.
47664773
See {spring-boot-autoconfigure-module-code}/amqp/RabbitProperties.java[`RabbitProperties`] for more of the supported options.
47674774

0 commit comments

Comments
 (0)