From a273d8d0c8491eb55373ce8d97f8cc42c43c6cd5 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 17 Jun 2016 10:30:43 +0200 Subject: [PATCH] Add secured connection support to Artemis This commit aligns the feature introduced in gh-6071 to Artemis. Closes gh-6179 --- .../ArtemisConnectionFactoryFactory.java | 11 ++++++-- .../jms/artemis/ArtemisProperties.java | 28 ++++++++++++++++++- .../ArtemisAutoConfigurationTests.java | 15 ++++++++++ .../appendix-application-properties.adoc | 2 ++ 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryFactory.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryFactory.java index 2a59eca0713..733f91a7f76 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryFactory.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisConnectionFactoryFactory.java @@ -31,6 +31,7 @@ import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.StringUtils; /** * Factory to create an Artemis {@link ActiveMQConnectionFactory} instance from properties @@ -130,8 +131,14 @@ class ArtemisConnectionFactoryFactory { NettyConnectorFactory.class.getName(), params); Constructor constructor = factoryClass.getConstructor(boolean.class, TransportConfiguration[].class); - return constructor.newInstance(false, - new TransportConfiguration[] { transportConfiguration }); + T connectionFactory = constructor.newInstance(false, + new TransportConfiguration[] {transportConfiguration}); + String user = this.properties.getUser(); + if (StringUtils.hasText(user)) { + connectionFactory.setUser(user); + connectionFactory.setPassword(this.properties.getPassword()); + } + return connectionFactory; } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisProperties.java index 7af35dfa239..2c59e06d423 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -51,6 +51,16 @@ public class ArtemisProperties { */ private int port = 61616; + /** + * Login user of the broker. + */ + private String user; + + /** + * Login password of the broker. + */ + private String password; + private final Embedded embedded = new Embedded(); public ArtemisMode getMode() { @@ -77,6 +87,22 @@ public class ArtemisProperties { this.port = port; } + public String getUser() { + return this.user; + } + + public void setUser(String user) { + this.user = user; + } + + public String getPassword() { + return this.password; + } + + public void setPassword(String password) { + this.password = password; + } + public Embedded getEmbedded() { return this.embedded; } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java index 99dccd0e381..629005d9256 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/artemis/ArtemisAutoConfigurationTests.java @@ -84,6 +84,8 @@ public class ArtemisAutoConfigurationTests { .getBean(ActiveMQConnectionFactory.class); assertThat(connectionFactory).isEqualTo(jmsTemplate.getConnectionFactory()); assertNettyConnectionFactory(connectionFactory, "localhost", 61616); + assertThat(connectionFactory.getUser()).isNull(); + assertThat(connectionFactory.getPassword()).isNull(); } @Test @@ -95,6 +97,19 @@ public class ArtemisAutoConfigurationTests { assertNettyConnectionFactory(connectionFactory, "192.168.1.144", 9876); } + @Test + public void nativeConnectionFactoryCredentials() { + load(EmptyConfiguration.class, "spring.artemis.mode:native", + "spring.artemis.user:user", "spring.artemis.password:secret"); + JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class); + ActiveMQConnectionFactory connectionFactory = this.context + .getBean(ActiveMQConnectionFactory.class); + assertThat(connectionFactory).isEqualTo(jmsTemplate.getConnectionFactory()); + assertNettyConnectionFactory(connectionFactory, "localhost", 61616); + assertThat(connectionFactory.getUser()).isEqualTo("user"); + assertThat(connectionFactory.getPassword()).isEqualTo("secret"); + } + @Test public void embeddedConnectionFactory() { load(EmptyConfiguration.class, "spring.artemis.mode:embedded"); diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index ecab02671a3..d87e68b1167 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -829,7 +829,9 @@ content into your application; rather pick only the properties that you need. spring.artemis.embedded.topics= # Comma-separated list of topics to create on startup. spring.artemis.host=localhost # Artemis broker host. spring.artemis.mode= # Artemis deployment mode, auto-detected by default. Can be explicitly set to "native" or "embedded". + spring.artemis.password= # Login password of the broker. spring.artemis.port=61616 # Artemis broker port. + spring.artemis.user= # Login user of the broker. # SPRING BATCH ({sc-spring-boot-autoconfigure}/batch/BatchProperties.{sc-ext}[BatchProperties]) spring.batch.initializer.enabled=true # Create the required batch tables on startup if necessary.