Skip to content

Commit c5935ea

Browse files
committed
Merge pull request #40421 from quaff
* pr/40421: Polish 'Add `spring.rabbitmq.template.allowed-list-patterns` property' Add `spring.rabbitmq.template.allowed-list-patterns` property Closes gh-40421
2 parents 4ce7366 + d243d7e commit c5935ea

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
* @author Rafael Carvalho
4949
* @author Scott Frederick
5050
* @author Lasse Wulff
51+
* @author Yanming Zhou
5152
* @since 1.0.0
5253
*/
5354
@ConfigurationProperties(prefix = "spring.rabbitmq")
@@ -1015,6 +1016,11 @@ public static class Template {
10151016
*/
10161017
private boolean observationEnabled;
10171018

1019+
/**
1020+
* Simple patterns for allowable packages/classes for deserialization.
1021+
*/
1022+
private List<String> allowedListPatterns;
1023+
10181024
public Retry getRetry() {
10191025
return this.retry;
10201026
}
@@ -1075,6 +1081,14 @@ public void setObservationEnabled(boolean observationEnabled) {
10751081
this.observationEnabled = observationEnabled;
10761082
}
10771083

1084+
public List<String> getAllowedListPatterns() {
1085+
return this.allowedListPatterns;
1086+
}
1087+
1088+
public void setAllowedListPatterns(List<String> allowedListPatterns) {
1089+
this.allowedListPatterns = allowedListPatterns;
1090+
}
1091+
10781092
}
10791093

10801094
public static class Retry {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,18 @@
2121

2222
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
2323
import org.springframework.amqp.rabbit.core.RabbitTemplate;
24+
import org.springframework.amqp.support.converter.AllowedListDeserializingMessageConverter;
2425
import org.springframework.amqp.support.converter.MessageConverter;
2526
import org.springframework.boot.context.properties.PropertyMapper;
27+
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
2628
import org.springframework.util.Assert;
29+
import org.springframework.util.CollectionUtils;
2730

2831
/**
2932
* Configure {@link RabbitTemplate} with sensible defaults.
3033
*
3134
* @author Stephane Nicoll
35+
* @author Yanming Zhou
3236
* @since 2.3.0
3337
*/
3438
public class RabbitTemplateConfigurer {
@@ -102,6 +106,19 @@ public void configure(RabbitTemplate template, ConnectionFactory connectionFacto
102106
map.from(templateProperties::getRoutingKey).to(template::setRoutingKey);
103107
map.from(templateProperties::getDefaultReceiveQueue).whenNonNull().to(template::setDefaultReceiveQueue);
104108
map.from(templateProperties::isObservationEnabled).to(template::setObservationEnabled);
109+
map.from(templateProperties::getAllowedListPatterns)
110+
.whenNot(CollectionUtils::isEmpty)
111+
.to((allowListPatterns) -> setAllowedListPatterns(template.getMessageConverter(), allowListPatterns));
112+
}
113+
114+
private void setAllowedListPatterns(MessageConverter messageConverter, List<String> allowListPatterns) {
115+
if (messageConverter instanceof AllowedListDeserializingMessageConverter allowedListDeserializingMessageConverter) {
116+
allowedListDeserializingMessageConverter.setAllowedListPatterns(allowListPatterns);
117+
return;
118+
}
119+
throw new InvalidConfigurationPropertyValueException("spring.rabbitmq.template.allow-list-patterns",
120+
allowListPatterns,
121+
"Allow list patterns can only be applied to a AllowedListDeserializingMessageConverter");
105122
}
106123

107124
private boolean determineMandatoryFlag() {

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.autoconfigure.amqp;
1818

1919
import java.security.NoSuchAlgorithmException;
20+
import java.util.Collection;
2021
import java.util.List;
2122
import java.util.concurrent.ThreadFactory;
2223
import java.util.concurrent.atomic.AtomicInteger;
@@ -35,11 +36,14 @@
3536
import org.junit.jupiter.api.condition.EnabledForJreRange;
3637
import org.junit.jupiter.api.condition.JRE;
3738
import org.junit.jupiter.api.extension.ExtendWith;
39+
import org.junit.jupiter.params.ParameterizedTest;
40+
import org.junit.jupiter.params.provider.ValueSource;
3841
import org.mockito.InOrder;
3942

4043
import org.springframework.amqp.core.AcknowledgeMode;
4144
import org.springframework.amqp.core.AmqpAdmin;
4245
import org.springframework.amqp.core.Message;
46+
import org.springframework.amqp.core.MessageProperties;
4347
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
4448
import org.springframework.amqp.rabbit.annotation.RabbitListener;
4549
import org.springframework.amqp.rabbit.config.AbstractRabbitListenerContainerFactory;
@@ -59,10 +63,13 @@
5963
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
6064
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
6165
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
66+
import org.springframework.amqp.support.converter.MessageConversionException;
6267
import org.springframework.amqp.support.converter.MessageConverter;
68+
import org.springframework.amqp.support.converter.SerializerMessageConverter;
6369
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
6470
import org.springframework.boot.autoconfigure.AutoConfigurations;
6571
import org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration;
72+
import org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException;
6673
import org.springframework.boot.test.context.FilteredClassLoader;
6774
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
6875
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
@@ -107,6 +114,7 @@
107114
* @author Andy Wilkinson
108115
* @author Phillip Webb
109116
* @author Scott Frederick
117+
* @author Yanming Zhou
110118
*/
111119
@ExtendWith(OutputCaptureExtension.class)
112120
class RabbitAutoConfigurationTests {
@@ -796,6 +804,27 @@ void customizeRequestedChannelMax() {
796804
});
797805
}
798806

807+
@ParameterizedTest
808+
@ValueSource(classes = { TestConfiguration.class, TestConfiguration6.class })
809+
@SuppressWarnings("unchecked")
810+
void customizeAllowedListPatterns(Class<?> configuration) {
811+
this.contextRunner.withUserConfiguration(configuration)
812+
.withPropertyValues("spring.rabbitmq.template.allowed-list-patterns:*")
813+
.run((context) -> {
814+
MessageConverter messageConverter = context.getBean(RabbitTemplate.class).getMessageConverter();
815+
assertThat(messageConverter).extracting("allowedListPatterns")
816+
.isInstanceOfSatisfying(Collection.class, (set) -> assertThat(set).contains("*"));
817+
});
818+
}
819+
820+
@Test
821+
void customizeAllowedListPatternsWhenHasNoAllowedListDeserializingMessageConverter() {
822+
this.contextRunner.withUserConfiguration(CustomMessageConverterConfiguration.class)
823+
.withPropertyValues("spring.rabbitmq.template.allowed-list-patterns:*")
824+
.run((context) -> assertThat(context).getFailure()
825+
.hasRootCauseInstanceOf(InvalidConfigurationPropertyValueException.class));
826+
}
827+
799828
@Test
800829
void noSslByDefault() {
801830
this.contextRunner.withUserConfiguration(TestConfiguration.class).run((context) -> {
@@ -1113,6 +1142,16 @@ RabbitListenerContainerFactory<?> rabbitListenerContainerFactory() {
11131142

11141143
}
11151144

1145+
@Configuration(proxyBeanMethods = false)
1146+
static class TestConfiguration6 {
1147+
1148+
@Bean
1149+
MessageConverter messageConverter() {
1150+
return new SerializerMessageConverter();
1151+
}
1152+
1153+
}
1154+
11161155
@Configuration(proxyBeanMethods = false)
11171156
static class MessageConvertersConfiguration {
11181157

@@ -1387,6 +1426,29 @@ public List<Address> getAddresses() {
13871426

13881427
}
13891428

1429+
@Configuration
1430+
static class CustomMessageConverterConfiguration {
1431+
1432+
@Bean
1433+
MessageConverter messageConverter() {
1434+
return new MessageConverter() {
1435+
1436+
@Override
1437+
public Message toMessage(Object object, MessageProperties messageProperties)
1438+
throws MessageConversionException {
1439+
return new Message(object.toString().getBytes());
1440+
}
1441+
1442+
@Override
1443+
public Object fromMessage(Message message) throws MessageConversionException {
1444+
return new String(message.getBody());
1445+
}
1446+
1447+
};
1448+
}
1449+
1450+
}
1451+
13901452
static class TestListener {
13911453

13921454
@RabbitListener(queues = "test", autoStartup = "false")

0 commit comments

Comments
 (0)