Spring Boot ActiveMQ如何设置访问密码

 更新时间:2020年07月02日 14:52:14   作者:楼兰的胡杨  
这篇文章主要介绍了Spring Boot ActiveMQ如何设置访问密码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

Apache ActiveMQ是Apache出品,是最流行的,能力很强的开源消息总线。默认情况下,程序连接ActiveMQ是不需要密码的,为了安装起见,需要设置密码,提高安全性。本文分享如何设置访问ActiveMQ的账号密码。

小编使用的ActiveMQ版本是apache-activemq-5.15.13。

一、设置控制台管理密码

ActiveMQ使用的是jetty服务器,找到 ActiveMQ安装目录下的\conf\jetty.xml文件:

<bean id="adminSecurityConstraint" class="org.eclipse.jetty.util.security.Constraint">
  <property name="name" value="BASIC" />
  <property name="roles" value="admin" />
  <!-- set authenticate=false to disable login -->
  <property name="authenticate" value="true" />
</bean>

注意:authenticate的属性默认为"true",登录管理界面时需要输入账户和密码;如果是“false”,需要改为"true"。

修改管理界面登录时的用户名和密码,在conf/jetty-realm.properties文件中添加用户

## ---------------------------------------------------------------------------
## Licensed to the Apache Software Foundation (ASF) under one or more
## contributor license agreements. See the NOTICE file distributed with
## this work for additional information regarding copyright ownership.
## The ASF licenses this file to You under the Apache License, Version 2.0
## (the "License"); you may not use this file except in compliance with
## the License. You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
## ---------------------------------------------------------------------------

# Defines users that can access the web (console, demo, etc.)
# username: password [,rolename ...]
# admin: admin, admin
# user: user, user
wiener: wiener1237, admin

配置信息按顺序解释,分别是:用户名、密码、角色名

二、消息生产者和消费者密码认证

在\conf\activemq.xml中broker 标签最后添加生产者和消费者密码认证信息:

<!-- destroy the spring context on shutdown to stop jetty -->
    <shutdownHooks>
      <bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
    </shutdownHooks>
    <!-- add plugins -->
    <plugins>
      <simpleAuthenticationPlugin>
        <users>
          <authenticationUser username="${activemq.username}" password="${activemq.password}" groups="users,admins"/>
        </users>
      </simpleAuthenticationPlugin>
    </plugins>
  </broker>

activemq.username和activemq.password的值在文件credentials.properties中配置,见如下步骤。

设置用户名密码,文件在\conf\credentials.properties

## ---------------------------------------------------------------------------
## Licensed to the Apache Software Foundation (ASF) under one or more
## contributor license agreements. See the NOTICE file distributed with
## this work for additional information regarding copyright ownership.
## The ASF licenses this file to You under the Apache License, Version 2.0
## (the "License"); you may not use this file except in compliance with
## the License. You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
## ---------------------------------------------------------------------------

# Defines credentials that will be used by components (like web console) to access the broker

# activemq.username=system
# activemq.password=manager
# guest.password=password

activemq.username=wiener
activemq.password=wiener1237
guest.password=password

三、Java端配置用户名密码

验证代码是在《【Spring Boot】ActiveMQ 发布/订阅消息模式介绍》的基础上做重构,除了新增类ActiveMQConfig之外,修改部分均用红色字体标注。配置application.properties连接信息:

## URL of the ActiveMQ broker. Auto-generated by default. For instance `tcp://localhost:61616`
# failover:(tcp://localhost:61616,tcp://localhost:61617)
# tcp://localhost:61616
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
#默认值false,表示point to point(点到点)模式,true时代表发布订阅模式,需要手动开启
spring.jms.pub-sub-domain=true
spring.activemq.user=wiener
spring.activemq.password=wiener1237

在项目中配置 ActiveMQ连接属性,新增ActiveMQConfig类:

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

/**
* 配置 ActiveMQ
*
* @author east7
* @date 2020/6/23 11:27
*/
@Configuration
public class ActiveMQConfig {

  @Value("${spring.activemq.user}")
  private String usrName;

  @Value("${spring.activemq.password}")
  private String password;

  @Value("${spring.activemq.broker-url}")
  private String brokerUrl;

  @Bean
  public ActiveMQConnectionFactory connectionFactory() {
    System.out.println("password =========== " + password);
    return new ActiveMQConnectionFactory(usrName, password, brokerUrl);
  }

  /**
   * 设置点对点模式,和下面的发布订阅模式二选一即可
   * @param connectionFactory
   * @return
   */
  @Bean
  public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ActiveMQConnectionFactory connectionFactory){
    DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
    bean.setConnectionFactory(connectionFactory);
    return bean;
  }

  @Bean
  public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory){
    //设置为发布订阅模式, 默认情况下使用生产消费者方式
    DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
    bean.setPubSubDomain(true);
    bean.setConnectionFactory(connectionFactory);
    return bean;
  }
}

bean.setPubSubDomain(true)配置会覆盖properties文件中spring.jms.pub-sub-domain的属性值,故可以在properties不设置spring.jms.pub-sub-domain属性。另外,这种配置方式可以在系统中同时使用点对点和发布/订阅两种消息模式。修改订阅者:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;

/**
* 消费者
*/
@Component
public class Subscriber1 {
  private static Logger logger = LoggerFactory.getLogger(Subscriber1.class);
  /**
   * 订阅 topicListener1,仅仅加入containerFactory即可
   *
   * @param text
   * @throws JMSException
   */
  @JmsListener(destination = "topicListener1", containerFactory = "jmsListenerContainerTopic")
  public void subscriber(String text) {
    logger.info("Subscriber1 收到的报文:{}", text);
  }
}

containerFactory 的值 "jmsListenerContainerTopic" 会自动匹配到ActiveMQConfig中的函数JmsListenerContainerFactory<?> jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory)。 Subscriber2同样修改即可,代码省略。如果containerFactory 的值设置为jmsListenerContainerQueue,则开启了点到点消息模式。

测试函数还可以使用topicTest()。下面提供一个新的测试途径——在controller中测试。新增方法

@Autowired
private Publisher publisher;

@GetMapping("/sendTopicMsg")
public String sendTopicMsg(String msg) {
  // 指定消息发送的目的地及内容
  Destination destination = new ActiveMQTopic("topicListener2");
  for (int i = 0; i < 8; i++) {
    publisher.publish(destination, msg + i);
  }
  return msg + " 发送完毕";
}

执行结果省略。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • java使用Base64实现文件加密解密

    java使用Base64实现文件加密解密

    这篇文章主要为大家详细介绍了java实现Base64给文件加密、解密,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-03-03
  • java基于C/S结构实现多线程聊天室

    java基于C/S结构实现多线程聊天室

    这篇文章主要为大家详细介绍了java基于C/S结构实现多线程聊天室,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • java数据库开发之JDBC的完整封装兼容多种数据库

    java数据库开发之JDBC的完整封装兼容多种数据库

    这篇文章主要介绍了java数据库开发之JDBC的完整封装兼容多种数据库,需要的朋友可以参考下
    2020-02-02
  • Java关系操作符简写介绍

    Java关系操作符简写介绍

    下面小编就为大家带来一篇Java关系操作符简写介绍。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-05-05
  • 使用Maven将springboot工程打包成docker镜像

    使用Maven将springboot工程打包成docker镜像

    这篇文章主要介绍了使用Maven将springboot工程打包成docker镜像,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • JVM系列之String.intern的性能解析

    JVM系列之String.intern的性能解析

    这篇文章主要介绍了JVM系列之String.intern的性能解析,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • spring boot启动加载数据原理分析

    spring boot启动加载数据原理分析

    实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求。这时spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来实现。下面给大家详细介绍下,需要的的朋友参考下吧
    2017-04-04
  • 解决问题:Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources

    解决问题:Failed to execute goal org.apache.m

    这篇文章主要给大家介绍了关于解决问题:Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources的相关资料,文中将解决的办法介绍的非常详细,需要的朋友可以参考下
    2023-03-03
  • java  HashMap扩容详解及实例代码

    java HashMap扩容详解及实例代码

    这篇文章主要介绍了java HashMap扩容详解及实例代码的相关资料,需要的朋友可以参考下
    2017-02-02
  • Java16新特性record类使用细节示例详解

    Java16新特性record类使用细节示例详解

    这篇文章主要为大家介绍了Java16新特性record类使用细节示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09

最新评论