Add a secured connection factory for Hornetq.

See gh-6071
This commit is contained in:
Stéphane Lagraulet 2016-05-30 17:14:34 +02:00 committed by Stephane Nicoll
parent dcf0633394
commit 1aa8f49001
6 changed files with 145 additions and 10 deletions

View File

@ -39,7 +39,7 @@ class HornetQConnectionFactoryConfiguration {
public HornetQConnectionFactory jmsConnectionFactory(ListableBeanFactory beanFactory, public HornetQConnectionFactory jmsConnectionFactory(ListableBeanFactory beanFactory,
HornetQProperties properties) { HornetQProperties properties) {
return new HornetQConnectionFactoryFactory(beanFactory, properties) return new HornetQConnectionFactoryFactory(beanFactory, properties)
.createConnectionFactory(HornetQConnectionFactory.class); .createConnectionFactory(HornetQSecuredConnectionFactory.class);
} }
} }

View File

@ -111,8 +111,8 @@ class HornetQConnectionFactoryFactory {
this.properties.getEmbedded().generateTransportParameters()); this.properties.getEmbedded().generateTransportParameters());
ServerLocator serviceLocator = HornetQClient ServerLocator serviceLocator = HornetQClient
.createServerLocatorWithoutHA(transportConfiguration); .createServerLocatorWithoutHA(transportConfiguration);
return factoryClass.getConstructor(ServerLocator.class) return factoryClass.getConstructor(HornetQProperties.class, ServerLocator.class)
.newInstance(serviceLocator); .newInstance(this.properties, serviceLocator);
} }
catch (NoClassDefFoundError ex) { catch (NoClassDefFoundError ex) {
throw new IllegalStateException("Unable to create InVM " throw new IllegalStateException("Unable to create InVM "
@ -128,9 +128,9 @@ class HornetQConnectionFactoryFactory {
params.put(TransportConstants.PORT_PROP_NAME, this.properties.getPort()); params.put(TransportConstants.PORT_PROP_NAME, this.properties.getPort());
TransportConfiguration transportConfiguration = new TransportConfiguration( TransportConfiguration transportConfiguration = new TransportConfiguration(
NettyConnectorFactory.class.getName(), params); NettyConnectorFactory.class.getName(), params);
Constructor<T> constructor = factoryClass.getConstructor(boolean.class, Constructor<T> constructor = factoryClass.getConstructor(HornetQProperties.class,
TransportConfiguration[].class); boolean.class, TransportConfiguration[].class);
return constructor.newInstance(false, return constructor.newInstance(this.properties, false,
new TransportConfiguration[] { transportConfiguration }); new TransportConfiguration[] { transportConfiguration });
} }

View File

@ -50,6 +50,16 @@ public class HornetQProperties {
*/ */
private int port = 5445; private int port = 5445;
/**
* User for a secured connection.
*/
private String user;
/**
* Password for a secured connection.
*/
private String password;
private final Embedded embedded = new Embedded(); private final Embedded embedded = new Embedded();
public HornetQMode getMode() { public HornetQMode getMode() {
@ -80,6 +90,22 @@ public class HornetQProperties {
return this.embedded; return this.embedded;
} }
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;
}
/** /**
* Configuration for an embedded HornetQ server. * Configuration for an embedded HornetQ server.
*/ */
@ -188,6 +214,7 @@ public class HornetQProperties {
/** /**
* Creates the minimal transport parameters for an embedded transport * Creates the minimal transport parameters for an embedded transport
* configuration. * configuration.
*
* @return the transport parameters * @return the transport parameters
* @see TransportConstants#SERVER_ID_PROP_NAME * @see TransportConstants#SERVER_ID_PROP_NAME
*/ */

View File

@ -0,0 +1,54 @@
/*
* Copyright 2012-2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.jms.hornetq;
import javax.jms.Connection;
import javax.jms.JMSException;
import org.hornetq.api.core.TransportConfiguration;
import org.hornetq.api.core.client.ServerLocator;
import org.hornetq.jms.client.HornetQConnectionFactory;
/**
* Secured HornetQ implementation of a JMS ConnectionFactory.
*
* @author Stéphane Lagraulet
*
* @since 1.4.0
*
*/
public class HornetQSecuredConnectionFactory extends HornetQConnectionFactory {
private HornetQProperties properties;
public HornetQSecuredConnectionFactory(HornetQProperties properties,
final ServerLocator serverLocator) {
super(serverLocator);
this.properties = properties;
}
public HornetQSecuredConnectionFactory(HornetQProperties properties, final boolean ha,
final TransportConfiguration... initialConnectors) {
super(ha, initialConnectors);
this.properties = properties;
}
public Connection createConnection() throws JMSException {
return createConnection(this.properties.getUser(), this.properties.getPassword());
}
}

View File

@ -20,7 +20,6 @@ import javax.jms.ConnectionFactory;
import javax.transaction.TransactionManager; import javax.transaction.TransactionManager;
import org.hornetq.jms.client.HornetQConnectionFactory; import org.hornetq.jms.client.HornetQConnectionFactory;
import org.hornetq.jms.client.HornetQXAConnectionFactory;
import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@ -50,13 +49,14 @@ class HornetQXAConnectionFactoryConfiguration {
throws Exception { throws Exception {
return wrapper.wrapConnectionFactory( return wrapper.wrapConnectionFactory(
new HornetQConnectionFactoryFactory(beanFactory, properties) new HornetQConnectionFactoryFactory(beanFactory, properties)
.createConnectionFactory(HornetQXAConnectionFactory.class)); .createConnectionFactory(
HornetQXASecuredConnectionFactory.class));
} }
@Bean @Bean
public HornetQConnectionFactory nonXaJmsConnectionFactory( public HornetQConnectionFactory nonXaJmsConnectionFactory(
ListableBeanFactory beanFactory, HornetQProperties properties) { ListableBeanFactory beanFactory, HornetQProperties properties) {
return new HornetQConnectionFactoryFactory(beanFactory, properties) return new HornetQConnectionFactoryFactory(beanFactory, properties)
.createConnectionFactory(HornetQConnectionFactory.class); .createConnectionFactory(HornetQSecuredConnectionFactory.class);
} }
} }

View File

@ -0,0 +1,54 @@
/*
* Copyright 2012-2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.jms.hornetq;
import javax.jms.Connection;
import javax.jms.JMSException;
import org.hornetq.api.core.TransportConfiguration;
import org.hornetq.api.core.client.ServerLocator;
import org.hornetq.jms.client.HornetQXAConnectionFactory;
/**
* Secured HornetQ XA implementation of a JMS ConnectionFactory.
*
* @author Stéphane Lagraulet
*
* @since 1.4.0
*
*/
public class HornetQXASecuredConnectionFactory extends HornetQXAConnectionFactory {
private HornetQProperties properties;
public HornetQXASecuredConnectionFactory(HornetQProperties properties,
ServerLocator serverLocator) {
super(serverLocator);
this.properties = properties;
}
public HornetQXASecuredConnectionFactory(HornetQProperties properties, boolean ha,
TransportConfiguration... initialConnectors) {
super(ha, initialConnectors);
this.properties = properties;
}
public Connection createConnection() throws JMSException {
return createConnection(this.properties.getUser(), this.properties.getPassword());
}
}