【问题标题】:Drupal 7: Sharing authentication with HTTP-AuthDrupal 7:使用 HTTP-Auth 共享身份验证
【发布时间】:2012-11-13 10:41:31
【问题描述】:

我想做what is described here,但使用 Drupal 7。

在第 7 版之前,密码作为 md5 哈希值保存在数据库中,因此您可以使用 Auth_MySQL

在此示例中,我试图仅允许有效的 drupal 用户访问 gitweb

文件:/etc/apache2/sites-enabled/default-ssl

    <Directory /usr/share/gitweb>
            AuthName "site name"
            AuthType Basic

            Auth_MySQL On
            Auth_MySQL_Authoritative on

            Auth_MySQL_Host localhost
            Auth_MySQL_Username drupal_user
            Auth_MySQL_Password drupal_password
            Auth_MySQL_DB drupal_database
            Auth_MySQL_Password_Table users
            Auth_MySQL_Username_Field name
            Auth_MySQL_Password_Field pass
            Auth_MySQL_Encryption_Types PHP_MD5
            Auth_MySQL_Password_Clause " AND status=1"
            Auth_MySQL_Empty_Passwords Off

            AuthBasicAuthoritative Off
            AuthUserFile /dev/null

            require valid-user
    </Directory>

查看数据库,使用select name,pass from users;,密码哈希是这样的:$S$DSmryVGZQg2AsLOFBT68xoQaEqPA1TWe4gi2gezh93tAjrbskFUi,因为它们是“盐渍”的,而不是像旧版 drupal 中的经典 md5 哈希?

我知道可以使用函数user_check_password($password, $account)=>api 来检查密码是否与散列密码匹配。

如何让 Apache 使用 Drupal 7 用户/密码作为认证系统?

【问题讨论】:

    标签: php mysql apache drupal drupal-7


    【解决方案1】:

    可能需要注意的是,根据 mod-auth-external(或 mod-authnz-external)docs,与身份验证器通信的默认/首选方法是通过 pipe 方法,而不是而不是“环境”。在接受的答案中,http.conf(或 apache2.conf,或您的虚拟主机的 )将包含以下行:

    DefineExternalAuth drupal pipe /var/www/html/drupal-authentication.php
    

    然后 /var/www/html/drupal-authentication.php 将更改为:

    $username = trim(fgets(STDIN));
    $password = trim(fgets(STDIN));
    

    最后,如果您在“bootstrap.inc”的日志中收到 REMOTE_ADDR 错误,您可以change how you load bootstrap 来防止此错误触发。在/var/www/html/drupal-authentication.php:

    $_SERVER['HTTP_HOST'] = 'www.example.com';
    $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
    
    define('DRUPAL_ROOT', dirname(realpath(__FILE__)));
    require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    

    【讨论】:

    • 这很有趣。我会试试看。谢谢你:)
    【解决方案2】:

    终于找到了解决办法:mod-auth-external

    安装模块(如果尚未安装)

    sudo apt-get install libapache2-mod-authnz-external
    sudo a2enmod authnz_external
    

    将此添加到 http.conf

    DefineExternalAuth drupal environment /var/www/drupal-authentication.php
    

    将此添加到受保护目录中的 .htaccess 中:

    AuthType basic
    AuthName "GitWeb"
    AuthBasicProvider external
    AuthExternal drupal
    Require valid-user
    

    编辑 /var/www/drupal-authentication.php(或您的 drupal 安装位置)

    #!/usr/bin/php5
    <?php
    
    empty($_SERVER['SHELL']) && die('shells only please');
    
    define('DRUPAL_ROOT', '/var/www');
    require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    
    $username = getenv('USER');
    $password = getenv('PASS');
    
    $auth = user_authenticate($username, $password);
    
    if (!$auth) {
      exit(1);
    }
    
    exit(0);
    

    这个例子正在运行:当访问 /gitweb 目录或其中的任何文件时,浏览器会询问用户名和密码。只有当用户名和密码与drupal的匹配时才能登录成功。

    安全

    我不确定这有多安全。它需要测试。

    性能

    每个http请求都会调用drupal-authentication.php,所以性能很低。您可以使用mod_authz_socache 提高效率,如here 所述

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    相关资源
    最近更新 更多