diff --git a/activemq-mqtt/src/main/java/org/apache/activemq/transport/mqtt/MQTTProtocolConverter.java b/activemq-mqtt/src/main/java/org/apache/activemq/transport/mqtt/MQTTProtocolConverter.java index 2abc741ff4..ac598e7c38 100644 --- a/activemq-mqtt/src/main/java/org/apache/activemq/transport/mqtt/MQTTProtocolConverter.java +++ b/activemq-mqtt/src/main/java/org/apache/activemq/transport/mqtt/MQTTProtocolConverter.java @@ -209,11 +209,11 @@ public class MQTTProtocolConverter { clientId = connect.clientId().toString(); } - String userName = ""; + String userName = null; if (connect.userName() != null) { userName = connect.userName().toString(); } - String passswd = ""; + String passswd = null; if (connect.password() != null) { passswd = connect.password().toString(); } diff --git a/activemq-mqtt/src/test/java/org/apache/activemq/transport/mqtt/MQTTNioTest.java b/activemq-mqtt/src/test/java/org/apache/activemq/transport/mqtt/MQTTNioTest.java index b0d59142ce..b64a84f450 100644 --- a/activemq-mqtt/src/test/java/org/apache/activemq/transport/mqtt/MQTTNioTest.java +++ b/activemq-mqtt/src/test/java/org/apache/activemq/transport/mqtt/MQTTNioTest.java @@ -16,11 +16,18 @@ */ package org.apache.activemq.transport.mqtt; +import org.apache.activemq.broker.BrokerPlugin; +import org.apache.activemq.broker.BrokerService; +import org.apache.activemq.command.ActiveMQTopic; +import org.apache.activemq.filter.DestinationMapEntry; +import org.apache.activemq.security.*; import org.apache.activemq.util.Wait; import org.fusesource.mqtt.client.BlockingConnection; import org.fusesource.mqtt.client.MQTT; import org.junit.Test; +import java.util.LinkedList; + import static org.junit.Assert.assertTrue; public class MQTTNioTest extends MQTTTest { @@ -35,6 +42,7 @@ public class MQTTNioTest extends MQTTTest { addMQTTConnector("maxInactivityDuration=-1"); brokerService.start(); MQTT mqtt = createMQTTConnection(); + mqtt.setClientId("test-mqtt"); mqtt.setKeepAlive((short)2); final BlockingConnection connection = mqtt.blockingConnection(); connection.connect(); @@ -49,4 +57,45 @@ public class MQTTNioTest extends MQTTTest { connection.disconnect(); } + @Test + public void testAnonymousUserConnect() throws Exception { + addMQTTConnector(); + configureAuthentication(brokerService); + brokerService.start(); + brokerService.waitUntilStarted(); + MQTT mqtt = createMQTTConnection(); + mqtt.setCleanSession(true); + mqtt.setUserName((String)null); + mqtt.setPassword((String)null); + final BlockingConnection connection = mqtt.blockingConnection(); + connection.connect(); + + System.out.println("Connected!"); + + connection.disconnect(); + + } + + private void configureAuthentication(BrokerService brokerService) throws Exception { + LinkedList users = new LinkedList(); + users.add(new AuthenticationUser("user1", "user1", "anonymous,user1group")); + final SimpleAuthenticationPlugin authenticationPlugin = new SimpleAuthenticationPlugin(users); + + DefaultAuthorizationMap map = new DefaultAuthorizationMap(); + LinkedList authz = new LinkedList(); + AuthorizationEntry entry = new AuthorizationEntry(); + entry.setDestination(new ActiveMQTopic(">")); + entry.setAdmin("admins"); + entry.setRead("admins,anonymous"); + entry.setWrite("admins"); + authz.add(entry); + map.setAuthorizationEntries(authz); + AuthorizationPlugin authorizationPlugin = new AuthorizationPlugin(map); + authenticationPlugin.setAnonymousAccessAllowed(true); + + brokerService.setPlugins(new BrokerPlugin[]{ + authenticationPlugin, authorizationPlugin + }); + } + }