如何在HTTP头中设置REMOTE_USER?

4
我有一个关于Apache设置的问题。
我安装了一个部分接受外部认证的Web应用程序:
- 我使用Apache来管理对我的应用程序Web页面的访问。 - 如果认证成功,环境变量REMOTE_USER将被设置为用户的名称。 - 然后,用户名通过HTTP标头传递到我的应用程序中,以便应用程序在用户会话中打开。 - 这主要是应用程序的Apache配置。我只在我的应用程序配置文件中设置包含用户名的变量(HTTP标头)的名称。
这里的问题是:我可以成功进行身份验证(大部分时间),但我的HTTP标头设置为空。
一些额外的细节:
  • I use Apache and the mod_perl modules (AuthenNIS + AuthzNIS + Net-NIS) to authenticate to my app with NIS account.
  • With the following Apache config file I have the authentication form when I try to access my application but the REMOTE_USER header is set to 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 set REMOTE_USER "username",应用程序将在相应的用户会话中打开。
为了查看REMOTE_USER的值,我使用Firebug Firefox模块显示http标头的值+我的应用程序有一个脚本显示传递给它的变量的值。
我在一个显示http请求中服务器变量的值的index.php页面上测试了一个几乎相同的Apache配置。区别在于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个回答

12

不要使用以下内容,因为如果使用像mod_authn_ntlm(本地计算机的ntlm)这样的模块设置了REMOTE_USER,则会在执行阶段遇到问题(请参见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

2
这是适用于Apache 2.4的正确答案。旧的风格(RewriteCond等)仅适用于2.2。 - Jake
RequestHeader 对我来说在2.4版本也有效!RewriteRule 破坏了 Web 服务之间的后续请求,导致出现递归错误!这是正确的答案。 - koullislp
1
非 SSL 版本的 RequestHeader set X-Remote-User expr=%{REMOTE_USER} 只适用于 Apache 2.4.10+。 - howinator
我认为这里提供的示例更正确:RequestHeader set X-Remote-User %{REMOTE_USER}e env=REMOTE_USER链接 - dma_k
2
我很想给一个负一分,因为错误的代码块被放在了第一位。尽管其他文本有警告,但全栈溢出开发人员很容易陷入麻烦。当然,那些粗心的人可能应该受到惩罚,但还是... - labyrinth
如何在头部传递x-remote-user。请给我一些示例建议。 - NewbieiOS

2
在Apache 2.2服务器中,我们提供以下配置。我们有一个C# ASP.NET Core 2.1应用程序,在我们的HTTP请求头中,我们会得到以下用户名称:
   <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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接