繁体   English   中英

使用Apache Qpid加密的消息

[英]Encrypted messages using Apache Qpid

我正在尝试按照本指南/示例所示设置加密的消息传递

我已经完成了到达jndi属性的所有步骤。 当我去构建在此站点上给出的源代码(包括导入)时,出现以下异常。

源代码:

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class EncryptionExample {
    public EncryptionExample() {

    }

    public static void main(String[] args) throws Exception {
        EncryptionExample encryptionExampleApp = new EncryptionExample();
        encryptionExampleApp.runProducerExample();
        encryptionExampleApp.runReceiverExample();
    }

    private void runProducerExample() throws Exception {
        Connection connection = createConnection("producerConnectionFactory");

        try {
            Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
            Destination destination = createDesination("myTestQueue");

            MessageProducer messageProducer = session.createProducer(destination);
            TextMessage message = session.createTextMessage("Hello world!");

            // ============== Enable encryption for this message ==============
            message.setBooleanProperty("x-qpid-encrypt", true);
            // ============== Configure recipients for encryption ==============
            message.setStringProperty("x-qpid-encrypt-recipients", "CN=client1, OU=Qpid, O=Apache, C=US");

            messageProducer.send(message);
            session.commit();
        }
        finally {
            connection.close();
        }
    }

    private void runReceiverExample() throws Exception {
        Connection connection = createConnection("consumer1ConnectionFactory");

        try {
            connection.start();
            Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
            Destination destination = createDesination("myTestQueue");
            MessageConsumer messageConsumer = session.createConsumer(destination);
            Message message = messageConsumer.receive();

            if (message instanceof TextMessage) {
                // application logic
                System.out.println(((TextMessage) message).getText());
            } else if (message instanceof BytesMessage) {
                // handle potential decryption failure
                System.out.println("Potential decryption problem. Application not in list of intended recipients?");
            }

            session.commit();
        }
        finally {
            connection.close();
        }
    }

    ///////////////////////////////////////
    // The following is boilerplate code //
    ///////////////////////////////////////

    private Connection createConnection(final String connectionFactoryName) throws JMSException, IOException, NamingException {
        try (InputStream resourceAsStream = this.getClass().getResourceAsStream("example.properties")) {
            Properties properties = new Properties();
            properties.load(resourceAsStream);
            Context context = new InitialContext(properties);
            ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup(connectionFactoryName);
            final Connection connection = connectionFactory.createConnection();
            context.close();
            return connection;
        }
    }

    private Destination createDesination(String desinationJndiName) throws IOException, NamingException {
        try (InputStream resourceAsStream = this.getClass().getResourceAsStream("example.properties")) {
            Properties properties = new Properties();
            properties.load(resourceAsStream);
            Context context = new InitialContext(properties);
            Destination destination = (Destination) context.lookup(desinationJndiName);
            context.close();
            return destination;
        }
    }
}

我收到的错误消息如下:

Exception in thread "main" javax.naming.ConfigurationException: Failed
to parse entry: Virtual host found between indicies 7 and 12 
amqp://guest:guest@?brokerlist='tcp://localhost:5672?encryption_remote_trust_store='$certificates%255c/clientcerts''
       ^^^^^^^^^^^^ due to : Virtual host found at index 7: amqp://guest:guest@?brokerlist='tcp://localhost:5672?encryption_remote_trust_store='$certificates%255c/clientcerts''
[Root exception is Virtual host found between indicies 7 and 12 
amqp://guest:guest@?brokerlist='tcp://localhost:5672?encryption_remote_trust_store='$certificates%255c/clientcerts''
       ^^^^^^^^^^^^]    at org.apache.qpid.jndi.PropertiesFileInitialContextFactory.createFactory(PropertiesFileInitialContextFactory.java:247)
    at
org.apache.qpid.jndi.PropertiesFileInitialContextFactory.createConnectionFactories(PropertiesFileInitialContextFactory.java:160)
    at
org.apache.qpid.jndi.PropertiesFileInitialContextFactory.getInitialContext(PropertiesFileInitialContextFactory.java:118)
    at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)    at
javax.naming.InitialContext.init(Unknown Source)    at
javax.naming.InitialContext.<init>(Unknown Source)  at
EncryptionExample.createConnection(EncryptionExample.java:86)   at
EncryptionExample.runProducerExample(EncryptionExample.java:33)     at
EncryptionExample.main(EncryptionExample.java:27) Caused by: Virtual
host found between indicies 7 and 12 
amqp://guest:guest@?brokerlist='tcp://localhost:5672?encryption_remote_trust_store='$certificates%255c/clientcerts''
       ^^^^^^^^^^^^     at org.apache.qpid.url.URLHelper.parseError(URLHelper.java:143)     at
org.apache.qpid.client.url.URLParser.parseURL(URLParser.java:126)   at
org.apache.qpid.client.url.URLParser.<init>(URLParser.java:41)  at
org.apache.qpid.client.AMQConnectionURL.<init>(AMQConnectionURL.java:62)
    at
org.apache.qpid.client.AMQConnectionFactory.<init>(AMQConnectionFactory.java:83)
    at
org.apache.qpid.jndi.PropertiesFileInitialContextFactory.createFactory(PropertiesFileInitialContextFactory.java:241)
    ... 9 more

由于这些错误提及第86行,因此:

Context context = new InitialContext(properties);

在我看来,属性文件有问题,当前是以下文件(取自教程站点,稍作修改):

java.naming.factory.initial = org.apache.qpid.jndi.PropertiesFileInitialContextFactory

# connection factories. This is where end-to-end encryption is configured on the client.
# connectionfactory.[jndiname] = [ConnectionURL]
connectionfactory.producerConnectionFactory = amqp://guest:guest@?brokerlist='tcp://localhost:5672?encryption_remote_trust_store='$certificates%255c/clientcerts''
connectionfactory.consumer1ConnectionFactory = amqp://<username>:<password>@?brokerlist='tcp://localhost:5672?encryption_key_store='path/to/client_1.jks'&encryption_key_store_password='<keystore_password>''
connectionfactory.consumer2ConnectionFactory = amqp://<username>:<password>@?brokerlist='tcp://localhost:5672?encryption_key_store='path/to/client_2.jks'&encryption_key_store_password='<keystore_password>''

# Rest of JNDI configuration. For example
# destination.[jniName] = [Address Format]
queue.myTestQueue = testQueue

因此,我想我的真正问题是,如何正确设置属性文件以便可以运行此示例?

道歉。 文档中有错字。 连接网址在@?之间需要一个/ ? 这会将clientid与虚拟主机名分开。

我刚刚更新了源代码树中的文档。

https://git-wip-us.apache.org/repos/asf?p=qpid-jms-amqp-0-x.git;h=56bacf6

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM