簡體   English   中英

嘗試訂閱 ActiveMQ Artemis ManagementNotifications 時出現 JMSSecurity 異常

[英]JMSSecurity exception while trying to subscribe to ActiveMQ Artemis ManagementNotifications

我正在使用 Spring Boot 來執行JNDI lookup ,因此我可以訂閱 Artemis 的管理通知並確定客戶端何時不再訂閱特定主題。 我對 JNDI 還很陌生,我的理解是,我提供的代碼片段會導致建立與現有 Artemis 代理的連接,從而捕獲代理發送的通知。

當控制點擊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 代碼沒有任何問題。 您只需要通過createConnection(String, String)傳遞用戶名和密碼。 這是 JMS 中的標准做法。

值得注意的是,您收到身份驗證錯誤意味着您的 SSL 配置正在運行。 如果您的 SSL 配置不正確,那么在您成功連接到代理並嘗試進行身份驗證之前,您會收到 SSL 錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM