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实现Excel导入导出操作详解

    Java实现Excel导入导出操作详解

    在平常的办公工作中,导入导出excel数据是常见的需求,今天就来看一下通过Java如何来实现这个功能,感兴趣的朋友可以了解下
    2022-02-02
  • springboot+thymeleaf打包成jar后找不到静态资源的坑及解决

    springboot+thymeleaf打包成jar后找不到静态资源的坑及解决

    这篇文章主要介绍了springboot+thymeleaf打包成jar后找不到静态资源的坑及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • 详解使用zxing库生成QR-Code二维码

    详解使用zxing库生成QR-Code二维码

    这篇文章主要介绍了详解使用zxing库生成QR-Code二维码的相关资料,需要的朋友可以参考下
    2017-05-05
  • java异步编程详解

    java异步编程详解

    这篇文章主要介绍了java异步编程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • Java RMI引起的log4j漏洞问题重现

    Java RMI引起的log4j漏洞问题重现

    java的log4j框架出现了一个大漏洞对服务器案例引起了不小的影响,如果你使用的是spring框架,用的是logback,不受这个问题的影响,下面跟着小编看下Java RMI引起的log4j漏洞问题重现,感兴趣的朋友一起看看吧
    2021-12-12
  • Java位运算和逻辑运算的区别实例

    Java位运算和逻辑运算的区别实例

    Java位运算和逻辑运算的区别实例,请参考下面代码,希望对你有所帮助
    2013-02-02
  • java实现稀疏矩阵的压缩与解压的方法

    java实现稀疏矩阵的压缩与解压的方法

    这篇文章主要介绍了java实现稀疏矩阵的压缩与解压 ,把该稀疏矩阵压缩以三元组形式表示并以文件形式保存,再写另一个程序读取文件中的信息把压缩后的三元组还原成原来的稀疏矩阵,需要的朋友可以参考下
    2022-03-03
  • SpringBoot整合POI实现Excel文件读写操作

    SpringBoot整合POI实现Excel文件读写操作

    EasyExcel是一个基于Java的、快速、简洁、解决大文件内存溢出的Excel处理工具,这篇文章主要介绍了SpringBoot整合POI实现Excel文件读写操作,首先准备环境进行一系列操作,本文给大家介绍的非常详细,需要的朋友参考下吧
    2023-10-10
  • java实现服务器巡查的代码

    java实现服务器巡查的代码

    接到上级领导任务,需要实现一个这样的需求,一大批服务器,需要检查服务器能否ping通,ssh密码是否正常,以及检查服务器的cpu,内存,硬盘占用情况,下面通过java代码实现服务器巡查功能,需要的朋友一起看看吧
    2021-12-12
  • springBoot接入阿里云oss的实现步骤

    springBoot接入阿里云oss的实现步骤

    这篇文章主要介绍了springBoot接入阿里云oss的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01

最新评论