搜尋
首頁Javajava教程詳解JMS 之 Active MQ的安全機制

詳解JMS 之 Active MQ的安全機制

Jun 26, 2017 am 11:44 AM
active安全機制

一、認證

認證(Authentication):驗證某個實體或使用者是否有權限存取受保護資源。

MQ提供兩種外掛程式用於權限認證:
(一)、Simple authentication plug-in:直接把相關的權限認證資訊配置到XML檔案中。

設定conf/activemq.xml 的broke元素新增外掛:

        <plugins><simpleauthenticationplugin><users><authenticationuser></authenticationuser><authenticationuser></authenticationuser><authenticationuser></authenticationuser><authenticationuser></authenticationuser></users></simpleauthenticationplugin></plugins>

程式碼中的認證方式兩種:

1、在建立Connection的時候認證

//用户认证Connection conn = connFactory.createConnection("admin","password");

2、也可以在創建ConnectionFactory工廠的時候認證

ConnectionFactory connFactory = new ActiveMQConnectionFactory("admin","password",url);

 

(二)、JAAS authentication plug-in:#實作了JAAS API,提供了一個更強大的、可自訂的權限方案。

設定方式:

1、在conf目錄中建立login.config 檔案使用者設定 PropertiesLoginModule:

activemq-domain {
    org.apache.activemq.jaas.PropertiesLoginModule required debug=trueorg.apache.activemq.jaas.properties.user="users.properties"org.apache.activemq.jaas.properties.group="groups.properties";
};

2、在conf目錄中建立users.properties 檔案使用者設定使用者:

# 创建四个用户
admin=password  
publisher=password 
consumer=password  
guest=password

3、在conf目錄中建立groups.properties 檔案使用者設定使用者群組:

#创建四个组并分配用户
admins=admin
publishers=admin,publisher
consumers=admin,publisher,consumer
guests=guest

# 4.將該設定插入activemq.xml中:

<!-- JAAS authentication plug-in --><plugins><jaasauthenticationplugin></jaasauthenticationplugin></plugins>

5、設定MQ的啟動參數:

使用dos指令啟動:

D:\tools\apache-activemq-5.6.0-bin\apache-activemq-5.6.0\bin\win64>activemq.bat -Djava.security.auth.login.config=D:/tools/apache-activemq-5.6.0-bin/apache-activemq-5.6.0/conf/login.config

6、在程式碼中的認證方式與Simple authentication plug-in 相同。

二、授權

基於認證的基礎上,可以根據實際使用者角色來授予對應的權限,如有些使用者有佇列寫的權限,有些則只能讀取等等。
兩種授權方式
(一)、目的地層級授權

JMS目的地的三種操作等級:
  Read :讀取目的地訊息權限
  Write:傳送訊息到目的地權限
  Admin:管理目的地的權限

設定方式 conf/activemq.xml :

<plugins><jaasauthenticationplugin></jaasauthenticationplugin><authorizationplugin><map><authorizationmap><authorizationentries><authorizationentry></authorizationentry></authorizationentries></authorizationmap></map></authorizationplugin></plugins>

(二)、訊息層級授權

授權特定的訊息。

開發步驟:
1、實作訊息授權插件,需要實作MessageAuthorizationPolicy介面

public class AuthorizationPolicy implements MessageAuthorizationPolicy {private static final Log LOG = LogFactory.
        getLog(AuthorizationPolicy.class);public boolean isAllowedToConsume(ConnectionContext context,
        Message message) {
        LOG.info(context.getConnection().getRemoteAddress());
        String remoteAddress = context.getConnection().getRemoteAddress();if (remoteAddress.startsWith("/127.0.0.1")) {
            LOG.info("Permission to consume granted");return true;
        } else {
        LOG.info("Permission to consume denied");return false;
    }
    }
}

2、把插件實作類別打成JAR包,放入到activeMq 的lib目錄中

3、在activemq.xml中設定元素

<messageauthorizationpolicy><bean></bean></messageauthorizationpolicy>

##三、自訂安全插件

插件邏輯需要實作BrokerFilter類,並且透過BrokerPlugin實作類別來安裝,用於攔截,Broker層級的操作:

  • 存取消費者和生產者

  • 提交交易

  • 新增和刪除broker的連線

##demo:基於IP位址,限制Broker連線。

package ch02.ptp;import java.util.List;import org.apache.activemq.broker.Broker;import org.apache.activemq.broker.BrokerFilter;import org.apache.activemq.broker.ConnectionContext;import org.apache.activemq.command.ConnectionInfo;public class IPAuthenticationBroker extends BrokerFilter {
    List<string> allowedIPAddresses;public IPAuthenticationBroker(Broker next, List<string>allowedIPAddresses) {super(next);this.allowedIPAddresses = allowedIPAddresses;
    }public void addConnection(ConnectionContext context, ConnectionInfo info) throws Exception {
        String remoteAddress = context.getConnection().getRemoteAddress();if (!allowedIPAddresses.contains(remoteAddress)) {throw new SecurityException("Connecting from IP address "
            + remoteAddress+ " is not allowed" );
        }super.addConnection(context, info);
    }
}</string></string>
安裝外掛:

package ch02.ptp;import java.util.List;import org.apache.activemq.broker.Broker;import org.apache.activemq.broker.BrokerPlugin;public class IPAuthenticationPlugin implements BrokerPlugin {
    List<string> allowedIPAddresses;public Broker installPlugin(Broker broker) throws Exception {return new IPAuthenticationBroker(broker, allowedIPAddresses);
    }public List<string> getAllowedIPAddresses() {return allowedIPAddresses;
    }public void setAllowedIPAddresses(List<string> allowedIPAddresses) {this.allowedIPAddresses = allowedIPAddresses;
    }
}</string></string></string>
#ps:將這連個類別打成jar包放到activemq的lib目錄下

配置自訂外掛程式:

<plugins><bean><property><list>  <value>127.0.0.1</value></list></property></bean></plugins>
 

以上是詳解JMS 之 Active MQ的安全機制的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
如何將Maven或Gradle用於高級Java項目管理,構建自動化和依賴性解決方案?如何將Maven或Gradle用於高級Java項目管理,構建自動化和依賴性解決方案?Mar 17, 2025 pm 05:46 PM

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

如何使用適當的版本控制和依賴項管理創建和使用自定義Java庫(JAR文件)?如何使用適當的版本控制和依賴項管理創建和使用自定義Java庫(JAR文件)?Mar 17, 2025 pm 05:45 PM

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

如何使用咖啡因或Guava Cache等庫在Java應用程序中實現多層緩存?如何使用咖啡因或Guava Cache等庫在Java應用程序中實現多層緩存?Mar 17, 2025 pm 05:44 PM

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

如何將JPA(Java持久性API)用於具有高級功能(例如緩存和懶惰加載)的對象相關映射?如何將JPA(Java持久性API)用於具有高級功能(例如緩存和懶惰加載)的對象相關映射?Mar 17, 2025 pm 05:43 PM

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類負載機制如何起作用,包括不同的類載荷及其委託模型?Java的類負載機制如何起作用,包括不同的類載荷及其委託模型?Mar 17, 2025 pm 05:35 PM

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具