Skip to content

Commit d331f28

Browse files
committed
Merge branch '2.1.x'
Closes gh-18885
2 parents 6759e8a + 9b3d625 commit d331f28

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
@@ -322,7 +322,7 @@ public Template getTemplate() {
322322
return this.template;
323323
}
324324

325-
public static class Ssl {
325+
public class Ssl {
326326

327327
/**
328328
* Whether to enable SSL support.
@@ -378,6 +378,21 @@ public boolean isEnabled() {
378378
return this.enabled;
379379
}
380380

381+
/**
382+
* Returns whether SSL is enabled from the first address, or the configured ssl
383+
* enabled flag if no addresses have been set.
384+
* @return whether ssl is enabled
385+
* @see #setAddresses(String)
386+
* @see #isEnabled()
387+
*/
388+
public boolean determineEnabled() {
389+
if (CollectionUtils.isEmpty(RabbitProperties.this.parsedAddresses)) {
390+
return isEnabled();
391+
}
392+
Address address = RabbitProperties.this.parsedAddresses.get(0);
393+
return address.secureConnection;
394+
}
395+
381396
public void setEnabled(boolean enabled) {
382397
this.enabled = enabled;
383398
}
@@ -960,6 +975,10 @@ private static final class Address {
960975

961976
private static final int DEFAULT_PORT = 5672;
962977

978+
private static final String PREFIX_AMQP_SECURE = "amqps://";
979+
980+
private static final int DEFAULT_PORT_SECURE = 5671;
981+
963982
private String host;
964983

965984
private int port;
@@ -970,6 +989,8 @@ private static final class Address {
970989

971990
private String virtualHost;
972991

992+
private boolean secureConnection;
993+
973994
private Address(String input) {
974995
input = input.trim();
975996
input = trimPrefix(input);
@@ -979,6 +1000,10 @@ private Address(String input) {
9791000
}
9801001

9811002
private String trimPrefix(String input) {
1003+
if (input.startsWith(PREFIX_AMQP_SECURE)) {
1004+
this.secureConnection = true;
1005+
return input.substring(PREFIX_AMQP_SECURE.length());
1006+
}
9821007
if (input.startsWith(PREFIX_AMQP)) {
9831008
input = input.substring(PREFIX_AMQP.length());
9841009
}
@@ -1015,7 +1040,7 @@ private void parseHostAndPort(String input) {
10151040
int portIndex = input.indexOf(':');
10161041
if (portIndex == -1) {
10171042
this.host = input;
1018-
this.port = DEFAULT_PORT;
1043+
this.port = (this.secureConnection) ? DEFAULT_PORT_SECURE : DEFAULT_PORT;
10191044
}
10201045
else {
10211046
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
@@ -90,6 +90,18 @@ void determinePortReturnsDefaultAmqpPortWhenFirstAddressHasNoExplicitPort() {
9090
assertThat(this.properties.determinePort()).isEqualTo(5672);
9191
}
9292

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

238+
@Test
239+
void determineSslUsingAmqpsReturnsStateOfFirstAddress() {
240+
this.properties.setAddresses("amqps://root:password@otherhost,amqp://root:password2@otherhost2");
241+
assertThat(this.properties.getSsl().determineEnabled()).isTrue();
242+
}
243+
244+
@Test
245+
void determineSslUsingAmqpReturnsStateOfFirstAddress() {
246+
this.properties.setAddresses("amqp://root:password@otherhost,amqps://root:password2@otherhost2");
247+
assertThat(this.properties.getSsl().determineEnabled()).isFalse();
248+
}
249+
250+
@Test
251+
void determineSslReturnFlagPropertyWhenNoAddresses() {
252+
this.properties.getSsl().setEnabled(true);
253+
assertThat(this.properties.getSsl().determineEnabled()).isTrue();
254+
}
255+
226256
@Test
227257
void simpleContainerUseConsistentDefaultValues() {
228258
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
@@ -5070,6 +5070,13 @@ For example, you might declare the following section in `application.properties`
50705070
spring.rabbitmq.password=secret
50715071
----
50725072

5073+
Alternatively, you could configure the same connection using the `addresses` attributes:
5074+
5075+
[source,properties,indent=0]
5076+
----
5077+
spring.rabbitmq.addresses=amqp://admin:secret@localhost
5078+
----
5079+
50735080
If a `ConnectionNameStrategy` bean exists in the context, it will be automatically used to name connections created by the auto-configured `ConnectionFactory`.
50745081
See {spring-boot-autoconfigure-module-code}/amqp/RabbitProperties.java[`RabbitProperties`] for more of the supported options.
50755082

0 commit comments

Comments
 (0)