如何在HTTP标头中设置REMOTE_USER?

时间:2015-10-27 12:59:10

标签: apache http authentication header

我的Apache设置存在问题。

我安装了部分接受外部身份验证的Web应用程序:

  • 我使用Apache来管理对我的应用程序网页的访问。
  • 如果验证成功,则使用用户名设置环境变量REMOTE_USER。
  • 然后通过HTTP标头将用户名传递给我的应用程序,以便在用户会话上打开应用程序。
  • 这主要是应用程序的Apache配置。我只设置了包含我的应用程序配置文件中的用户名的变量(HTTP标头)的名称。

问题在于:我可以成功进行身份验证(大部分时间),但我的HTTP标头设置为null。

其他一些细节:

  • 我使用Apache和mod_perl模块(AuthenNIS + AuthzNIS + Net-NIS)通过NIS帐户向我的应用程序进行身份验证。
  • 使用以下Apache配置文件,当我尝试访问我的应用程序但是REMOTE_USER标头设置为null时,我有验证表单。

        Listen 2208
        <VirtualHost *:2208>
    
                    RewriteEngine on
                    DocumentRoot "/path/to/static"
    
    
                    <Directory "/path/to/static">
    
                                Options +Indexes FollowSymLinks MultiViews
                                AllowOverride None
                                Order allow,deny
                                Allow from all
    
    
                                AuthType Basic
                                AuthName "Authentifiez vous"
                                PerlAuthenHandler Apache2::AuthenNIS
                                PerlAuthzHandler Apache2::AuthzNIS
                                PerlSetVar AllowAlternateAuth no
                                require valid-user
    
                    </Directory>
    
    
                    RewriteEngine on
                    RewriteRule . - [E=RU:%{LA-U:REMOTE_USER}]
                    RequestHeader set REMOTE_USER %{RU}e
    
                           RewriteRule ^/apps$ /apps/ [R]
                           RewriteRule ^/static/style/(.*) /path/to/static/june_2007_style/blue/$1 [L]
                           RewriteRule ^/static/scripts/(.*) /path/to/static/scripts/packed/$1 [L]
                           RewriteRule ^/static/(.*) /path/to/static/$1 [L]
                           RewriteRule ^/favicon.ico /path/to/static/favicon.ico [L]
                           RewriteRule ^/robots.txt /path/to/static/robots.txt [L]
                           RewriteRule ^(.*) http://localhost:2209$1 [P]
    
        </VirtualHost>
    

如果我设置RequestHeader设置REMOTE_USER&#34;用户名&#34;应用程序将在相应的用户会话中打开。

要查看REMOTE_USER的值,我使用Firebug Firefox模块显示http标头的值+我的应用程序有一个脚本,显示传递给它的变量值。

我在index.php页面上测试了几乎相同的Apache配置,该页面显示了http请求中服务器变量的值。不同之处在于RewriteRules。

        <?PHP
                    foreach($_SERVER as $key_name => $key_value) {
                    print $key_name . " = " . $key_value . "<br>";

                    }
        ?>

在这种情况下,我得到一个带有用户名值的REMOTE_USER和HTTP_REMOTE_USER。

我不明白我的问题在哪里。

Apache 2.2.31 RedHat 6.5

提前致谢!

2 个答案:

答案 0 :(得分:7)

请勿使用以下内容,因为如果REMOTE_USER设置为mod_authn_ntlm(ntlm与本地计算机,请参阅https://support.microsoft.com/en-us/kb/896861)等模块,则会遇到执行阶段的问题。

RewriteCond %{LA-U:REMOTE_USER} (.+)
RewriteRule . - [E=RU:%1]
RequestHeader set X-Remote-User %{RU}e

使用以下方法:

RequestHeader set X-Remote-User expr=%{REMOTE_USER}

还有mod_ssl

的解决方案
RequestHeader set X-Remote-User %{REMOTE_USER}s

答案 1 :(得分:2)

在Apache 2.2服务器中,我们进行了以下配置。我们有C#ASP.NET Core 2.1应用程序,并在HTTP Request标头中获得如下用户名

   <LocationMatch ^/mylocation>
     AuthName "NTLM Authentication"
     NTLMAuth on
     NTLMAuthHelper "/usr/bin/ntlm_auth --helper-protocol=squid-2.5-ntlmssp"
     NTLMBasicAuthoritative on
     NTLMBasicRealm xxx_yy
     AuthType NTLM
     require valid-user
     RewriteCond %{LA-U:REMOTE_USER} (.+)
     RewriteRule . - [E=RU:%1]
     RequestHeader set X-Remote-User %{RU}e

   </LocationMatch>

在我们的C#ASP.NET Core 2.1应用程序应用程序中,我们在HTTP请求标头中获得以下信息

Key: X-Remote-User, Value=xxx_yy\abcdefg