使用 Apache 和 LDAP 重定向用戶

使用 Apache 和 LDAP 重定向用戶

在工作中,我們遇到這樣的問題:當使用者存取透過 Apache2 管理的 URL 時,我們希望根據使用者是否是特定 LDAP 群組的成員來重新導向使用者。

分三種情況:

  • 使用者提供有效的憑證並且是 LDAP 群組的成員 => 重定向到應用程式 ABC(有效)
  • 使用者提供了有效的憑證,但不是 LDAP 群組的成員 => 重定向到維護頁面(不起作用,這是問題所在)
  • 使用者提供無效的信用或點擊取消 => 重定向到維護(有效)

我們的問題是:如果使用者有效且不是 LDAP 群組「THE-GROUP」的成員,我們如何重新導向使用者?

現在,除非您按一下「取消」或「THE-GROUP」的成員,否則將永遠顯示用於輸入憑證的覆蓋層。

我們的應用程式 ABC 託管在我們的場所,在 Ubuntu 上運行。我們的反向代理是在 Ubuntu 16.04 上運行的 Apache 2.4.18-2ubuntu3.10。客戶將使用來自世界各地的各種瀏覽器和作業系統,因此不能選擇透過 IP 進行過濾。反向代理位於我們的 DMZ 中,應用程式伺服器位於外部。創建了 NAT 規則,以便兩個系統可以透過 8080(運行連接埠 ABC)進行通訊。

我們的(測試)配置如下所示:

<VirtualHost *:443>
    ServerName testabc.company.com

    SSLProxyEngine On
    SSLEngine On
    SSLCertificateKeyFile  /etc/ssl/private/our_company.key
    SSLCertificateFile /etc/ssl/certs/company_com/fullchain.cer


    ErrorLog ${APACHE_LOG_DIR}/LDAP_test_error.log
    CustomLog ${APACHE_LOG_DIR}/LDAP_test_access.log combined

    # error document shown to unauthorized users
    DocumentRoot /var/www/Maintenance_Page
    ErrorDocument 401 /TTT/index.html

        <Location />
            ProxyPass http://internal-vm-name:8080/
            ProxyPassReverse http://internal-vm-name:8080/
        </Location>

    # the following block applies to all proxied content
    <Proxy "*">
        AuthType Basic
        AuthBasicProvider ldap
        AuthUserFile /dev/null
        AuthName "Auth with our LDAP Server"

        # configuration of the mod_authnz_ldap module
        AuthLDAPURL "ldap://SOMETHING"
        AuthLDAPBindDN "FOO,BAR "
        AuthLDAPBindPassword "FOOBAR"

        # Only users belonging to group THE-GROUP can access ABC,
        # all others will see the error document specified above.
        Require ldap-group CN=THE-GROUP,OU=Company,DC=ad,DC=Company,DC=com
    </Proxy>
</VirtualHost>

# virtual host required to access images and style-sheets from the error document
<VirtualHost *:80>
    ServerName maintenance.company.com
    DocumentRoot /var/www/Maintenance_Page

    ErrorLog ${APACHE_LOG_DIR}/maintenance -error.log
    CustomLog ${APACHE_LOG_DIR}/maintenance.log combined
</VirtualHost>

# redirection from HTTP to HTTPS
<VirtualHost *:80>
    ServerName abc.company.com
    Redirect Permanent /  https://abc.company.com/
    Redirect /  https://abc.company.com/
</VirtualHost>

答案1

對於你的問題沒有好的解決方案。您正在使用 HTTP 基本驗證,這表示瀏覽器正在嘗試開啟頁面,正在取得 401 狀態,並發送 WWW-Authenticate: Basicrealm="Whatever" 標頭。

此時,瀏覽器會顯示身份驗證對話框,輸入使用者和密碼後,它們將被傳送到授權標頭內的網頁伺服器。

如果伺服器拒絕請求,它將再次發送帶有 WWW-Authenticate 標頭的 401 狀態。瀏覽器將無限次地再次彈出該對話框。伺服器在任何時候都不能說密碼被永久拒絕。

如果您使用 mod_dbd 支援的資料庫,則可以使用 mod_rewrite 為該群組執行 SQL 查詢並相應地重定向。我不認為 LDAP 身份驗證處理程序可以實現這一點。

相關內容