ActiveMQ 之安全认证
簡介
默認安裝的activemq 使用 ActiveMQConnectionFactory 時沒有用戶名/密碼。只要有人知道了我們activemq服務器的ip和端口,就可以連上去消費掉我們的消息,所以我們需要給activemq設置一個username和pasword;
ActiveMQ也提供了安全認證。就是用戶名密碼登錄規則。ActiveMQ如果需要使用安全認證的話,必須在activemq的核心配置文件中開啟安全配置。配置文件就是conf/activemq.xml
配置文件:
conf/login.config
user代表用戶信息配置文件,group代表用戶組信息配置文件。尋址路徑為相對當前配置文件所在位置開始尋址。
conf/users.properties
戶信息配置文件,格式:用戶名=密碼
conf/groups.properties
用戶組信息配置文件,格式:組名=用戶名,用戶名…
二、配置用戶名/密碼
方式1:
在conf下的activemq.xml的broker節點插入一個子節點如下:
<plugins><simpleAuthenticationPlugin><users><authenticationUser username="vincent" password="123456" groups="users,admins" /></users></simpleAuthenticationPlugin> </plugins>重啟activemq 并執行
ConnectionFactory factory = new ActiveMQConnectionFactory(null,null,"tcp://localhost:61616");Connection connection = null;Session session = null;try {connection = factory.createConnection();connection.start();session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);Destination destination = session.createQueue("first_mq");MessageProducer producer = session.createProducer(destination);TextMessage msg = session.createTextMessage("");for(int i=0;i<10;i++) {msg.setText("你好" + i);try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}producer.send(msg);}} catch (JMSException e) {e.printStackTrace();}finally {if(session != null) {try {session.close();} catch (JMSException e) {e.printStackTrace();}}}
使用配置的用戶名 / 密碼將能連接成功。
方式2
在conf下的activemq.xml的broker節點插入一個子節點如下:
conf/users.properties
新增一個用戶名/密碼:vincent2=123456
conf/groups.properties
添加 vincent2 至 admins 用戶組:admins=admin,vincent2
重啟activemq 并執行沒有用戶名/密碼的連接:
使用配置的用戶名 / 密碼將能連接成功。
三、總結
安全認證 方式1 和 方式2 能共存,方式1 更簡單。
總結
以上是生活随笔為你收集整理的ActiveMQ 之安全认证的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 投票算法
- 下一篇: 1038: 顺序表中重复数据的删除