使用Apache Qpid加密的消息

时间:2017-08-09 06:28:44

标签: java apache encryption mq qpid

我正在尝试设置加密消息,如guide/example

所示

我已经完成了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

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

0 个答案:

没有答案