# ActiveMQ Authentication and Setup

## Authentication

The particular [Docker image](https://hub.docker.com/r/symptoma/activemq) of ActiveMQ that I'm currently using doesn't support authentication by default, you will need to enable the authentication plugin in order to enforce some kind of authentication.

To do so you will need to add the following plugin into the `activemq.xml` file and under the `broker` section tag.

<details id="bkmrk-activemq.xml-%C2%A0-%C2%A0-%C2%A0-%3C"><summary>activemq.xml</summary>

```
    <plugins>
       <simpleAuthenticationPlugin anonymousAccessAllowed="false">
         <users>
             <authenticationUser username="system" password="system" groups="users,admins"/>
             <authenticationUser username="test01" password="test01" groups="users,admins"/>
         </users>
       </simpleAuthenticationPlugin>
    </plugins>
```

</details>You can create your own Docker image and copy this particular file overriding the default one that's in the container.

An example of Docker image and Docker compose file:

<details id="bkmrk-docker-image-from-sy"><summary>Docker image</summary>

```yaml
FROM symptoma/activemq:5.15.13

COPY ./activemq.xml /opt/apache-activemq-5.15.13/conf/activemq.xml
```

</details><details id="bkmrk-docker-compose-servi"><summary>Docker compose</summary>

```
services:
  amq:
    build: .
    ports:
      - 61616:61616
      - 8161:8161
```

</details>Now you will actually have to provide a valid username and password in order to access to the broker, without this you can access the broker without any authentication.

To set the username and password with Spring Boot JMS you can create a ConnectionFactory bean like below

```java
@Bean
public ConnectionFactory connetionFactory() {
    var factory = new ActiveMQConnectionFactory();
    factory.setUserName("system");
    factory.setPassword("system");
    factory.setBrokerURL("tcp://localhost:61616");
    return factory;
}
```

Then you can do a constructor injection of `JmsTemplate` object which gives you access to send and receive messages from the ActiveMQ.

<p class="callout info">For whatever reason if you don't configure your own ConnectionFactory and instead use the default auto-configured ConnectionFactory the Spring boot application will actually not terminate immediately after your Spring Boot application finishes. Perhaps this isn't a problem if your application is web application, but for those application that just runs and finishes you will want to consider the option of configuring your own ConnectionFactory.</p>