【问题标题】:JMSSecurity exception while trying to subscribe to ActiveMQ Artemis ManagementNotifications尝试订阅 ActiveMQ Artemis ManagementNotifications 时出现 JMSSecurity 异常
【发布时间】:2022-01-19 10:08:36
【问题描述】:

我正在使用 Spring Boot 来执行JNDI lookup,因此我可以订阅 Artemis 的管理通知并确定客户端何时不再订阅特定主题。我对 JNDI 还很陌生,我的理解是我提供的代码 sn-p 会导致建立与现有 Artemis 代理的连接,从而捕获代理发送的通知。

当控件点击提供的代码 sn-p 中的connection = cf.createConnection(); 行时,我收到错误。

错误:

Caused by: javax.jms.JMSSecurityException: AMQ229031: Unable to validate user from /ip:port. Username: null; SSL certificate subject DN: unavailable
...
Caused by: org.apache.activemq.artemis.api.core.ActiveMQSecurityException: AMQ229031: Unable to validate user from /ip:port. Username: null; SSL certificate subject DN: unavailable

如何在InitialContext 中包含用户名、密码、密钥库和信任库,以使该错误不会出现?

我正在使用的代码:

@ComponentScan({"com.management.notifications"})
@SpringBootApplication
public class ManagementNotificationTestApplication {

    @Value("${JMS_BROKER_TRUSTSTORE}")
    private String pathToTrustStore;

    public static void main(String[] args) {
        SpringApplication.run(ManagementNotificationTestApplication.class);
    }


    @EventListener(ApplicationReadyEvent.class)
    public void doSomething() throws NamingException, JMSException {
        Connection connection = null;
        InitialContext initialContext = null;
        try {
            Properties properties = new Properties();
            properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory");
            properties.setProperty(Context.PROVIDER_URL, "tcp://ipAddress:portNumber?&" + "sslEnabled=true&" +
                    "trustStorePath=" + pathToTrustStore + "&trustStorePassword=" + "abc");

            // Step 1. Create an initial context to perform the JNDI lookup.
            initialContext = new InitialContext(properties);

            // Step 3. Perform a lookup on the Connection Factory
            ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");

            // Step 4.Create a JMS connection, a session and a producer for the queue
            connection = cf.createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            // Step 5. Perform a lookup on the notifications topic
            Topic notificationsTopic = (Topic) initialContext.lookup("topic/notificationsTopic");

            // Step 6. Create a JMS message consumer for the notification queue and set its message listener
            // It will display all the properties of the JMS Message
            MessageConsumer notificationConsumer = session.createConsumer(notificationsTopic);
            notificationConsumer.setMessageListener(new MessageListener() {
                @Override
                public void onMessage(final Message notif) {
                    System.out.println("------------------------");
                    System.out.println("Received notification:");
                    try {
                        Enumeration propertyNames = notif.getPropertyNames();
                        while (propertyNames.hasMoreElements()) {
                            String propertyName = (String) propertyNames.nextElement();
                            System.out.format("  %s: %s%n", propertyName, notif.getObjectProperty(propertyName));
                        }
                    } catch (JMSException e) {
                    }
                    System.out.println("------------------------");
                }
            });

            // Step 7. Start the Connection to allow the consumers to receive messages
            connection.start();

            // Step 10. Try to create a connection with unknown user
            try {
                cf.createConnection("not.a.valid.user", "not.a.valid.password");
            } catch (JMSException e) {
            }

            // sleep a little bit to be sure to receive the notification for the security
            // authentication violation before leaving the example
            /*Thread.sleep(2000);*/
        } finally {
            // Step 11. Be sure to close the resources!
            if (initialContext != null) {
                initialContext.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
    }
}

【问题讨论】:

  • 您应该在通过 JNDI 获取它的服务器上的 ConnectionFactory 上配置所有这些
  • 您的意思是 Artemis 代理必须配置所有这些吗?如果是,那么是的,用户名、密码和相关证书已经配置好,其他客户端可以连接到代理。问题出在这个 JNDI 东西上。
  • 可以分享ActiveMQ Artemis的login.config文件吗?
  • @ehsavoie,他正在使用org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory,这是一个仅限客户端的 JNDI 实现。服务器上没有什么可以配置的。有关详细信息,请参阅the documentation

标签: spring-boot jndi activemq-artemis


【解决方案1】:

您的 JNDI 代码没有任何问题。您只需要通过createConnection(String, String) 传递用户名和密码。这是 JMS 中的标准做法。

值得注意的是,您遇到身份验证错误意味着您的 SSL 配置正在运行。如果您的 SSL 配置不正确,那么在您成功连接到代理并尝试进行身份验证之前,您就会收到 SSL 错误。

【讨论】:

    猜你喜欢
    • 2021-06-09
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    相关资源
    最近更新 更多