Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authentication for @JmsListener using Spring 4.2 annotations and ActiveMQ

Trying to implement a non-XML JMS listener using Spring 4 and ActiveMQ. My issue is that I keep getting the following error with my client:

Setup of JMS message listener invoker failed for destination 'topic.FromJndiProperties' [...] 
Cause: The JMS connection has failed: Force close due to SecurityException on connect.
Cause: User name [null] or password is invalid.

So the connection to the destination is being made with username and password null. I think I may have not setup the destinationResolver correctly but I am stuck working out how to resolve this. Can anyone help me fix this?

My AppConfig:

@Autowired
private Environment env;

@Autowired
private BeanFactory springContextBeanFactory;  

@Bean
public DefaultJmsListenerContainerFactory myListenerContainerFactory() throws NamingException {

    Properties props = new Properties();
    props.setProperty(Context.INITIAL_CONTEXT_FACTORY, env.getProperty("java.naming.factory.initial"));
    props.setProperty(Context.PROVIDER_URL, env.getProperty("java.naming.provider.url"));
    props.setProperty(Context.SECURITY_PRINCIPAL, env.getProperty("java.naming.security.principal"));
    props.setProperty(Context.SECURITY_CREDENTIALS, env.getProperty("java.naming.security.credentials"));

    Context jndiContext = new InitialContext(props);;
    ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup("ConnectionFactory");

    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setDestinationResolver(new BeanFactoryDestinationResolver(springContextBeanFactory));
    factory.setPubSubDomain(true);
    factory.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);

    return factory;
}

The listener:

@JmsListener(containerFactory = "myListenerContainerFactory", destination = "topic.FromJndiProperties")
public void receiveMessage(String message) {
    try {
        System.out.println("Received <" + message + ">");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
like image 312
Dan Avatar asked Feb 25 '16 15:02

Dan


2 Answers

If you have not changed any features on the broker end , try the below

props.setProperty(Context.SECURITY_PRINCIPAL,"admin"
props.setProperty(Context.SECURITY_CREDENTIALS, "admin");

also in your jndi.properties you can add the properties userName=admin and password=admin instead

like image 126
Sundar Avatar answered Nov 17 '22 17:11

Sundar


ConnectionFactory, TopicConnectionFactory and QueueConnectionFactory all have two ways of creating a connection (eg ConnectionFactory.createConnection() and ConnectionFactory.createConnection(username, password).

It seems that the @JmsListener annotation always creates a connection without passing the credentials via the zero args methods.

I used spring's UserCredentialsConnectionFactoryAdapter to wrap my ConnectionFactory for use with @JmsListener so that it passes the credentials when creating connections

like image 30
lance-java Avatar answered Nov 17 '22 17:11

lance-java