[MQTT] Autenticazione con crittografia

Rispondi
Roberto
Amministratore
Messaggi: 160
Iscritto il: 19/12/2009, 19:30

[MQTT] Autenticazione con crittografia

Messaggio da Roberto »

Mi sono scontrato di recente con la creazione di un sistema di autenticazione su MQTT che utiliizza il sistema di crittografia. Per fare questo e' necessario creare un nuovo file di configurazione nella directory conf e lo chiamiamo activemq-security.xml di cui riporto integralmente il codice perfettamente funzionante:

Codice: Seleziona tutto

<?xml version="1.0" encoding="UTF-8"?>

<beans
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:amq="http://activemq.apache.org/schema/core"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
  
  <bean id="environmentVariablesConfiguration" class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
    <property name="algorithm" value="PBEWithMD5AndDES" />
    <property name="password" value="CHIAVE_CRITTOGRAFIA" />
  </bean>
                                                                     
  <bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
    <property name="config" ref="environmentVariablesConfiguration" />
  </bean>  
    
  <bean id="propertyConfigurer" class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer">
      <constructor-arg ref="configurationEncryptor" /> 
      <property name="location" value="file:${activemq.conf}/credentials-enc.properties"/> 
  </bean> 

  <broker useJmx="true" persistent="false" xmlns="http://activemq.apache.org/schema/core" >

    <managementContext>
        <managementContext createConnector="true">
            <property xmlns="http://www.springframework.org/schema/beans" name="environment">
                <map xmlns="http://www.springframework.org/schema/beans">
                    <entry xmlns="http://www.springframework.org/schema/beans" key="jmx.remote.x.password.file"
                           value="${activemq.conf}/jmx.password"/>
                    <entry xmlns="http://www.springframework.org/schema/beans" key="jmx.remote.x.access.file"
                           value="${activemq.conf}/jmx.access"/>
                </map>
            </property>
        </managementContext>
    </managementContext>

    <plugins>

        <simpleAuthenticationPlugin>
            <users>
                <authenticationUser username="ROSSI" password="${ROSSI.password}"
                    groups="users,admins"/>
            </users>
        </simpleAuthenticationPlugin>
        
    </plugins>
    
        <transportConnectors>
            <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
        </transportConnectors>

    <shutdownHooks>
        <bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
    </shutdownHooks>

  </broker>

  <import resource="jetty.xml"/>

</beans>
Le uniche modifiche che dovrete fare a questo file sono di inserire la vostra chiave di crittografia al posto di CHIAVE_CRITTOGRAFIA e creare gli utenti inserendo il seguente codice nel tag <users>:

Codice: Seleziona tutto

<authenticationUser username="ROSSI" password="${ROSSI.password}" groups="users,admins"/>
assegnando all'occorrenza username, password e gruppi di appartenenza per ogni utente, facendo attenzione al fatto che il valore della password si trova all'interno del file credentials-enc.properties con la seguente dicitura:

Codice: Seleziona tutto

rossi.password=ENC(hcCapWau4wZAUopcLvuMZg==)
Roberto Basile
Rispondi