亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring Boot ActiveMQ設置訪問密碼的方法

發布時間:2020-07-03 09:20:58 來源:億速云 閱讀:205 作者:清晨 欄目:開發技術

這篇文章將為大家詳細講解有關Spring Boot ActiveMQ設置訪問密碼的方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

Apache ActiveMQ是Apache出品,是最流行的,能力很強的開源消息總線。默認情況下,程序連接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<&#63;> jmsListenerContainerQueue(ActiveMQConnectionFactory connectionFactory){
    DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
    bean.setConnectionFactory(connectionFactory);
    return bean;
  }

  @Bean
  public JmsListenerContainerFactory<&#63;> 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<&#63;> 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 + " 發送完畢";
}

關于Spring Boot ActiveMQ設置訪問密碼的方法就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

长岛县| 海盐县| 安乡县| 苗栗县| 天长市| 辉县市| 隆尧县| 堆龙德庆县| 东源县| 大理市| 延津县| 崇义县| 白城市| 瑞丽市| 宜昌市| 苗栗市| 集安市| 陆良县| 灵丘县| 东兴市| 赣榆县| 惠东县| 西昌市| 遂宁市| 福贡县| 岚皋县| 宜川县| 家居| 尖扎县| 武强县| 康乐县| 铜川市| 德州市| 铜山县| 山丹县| 固始县| 翼城县| 南雄市| 巍山| 武汉市| 晋州市|