如何将 HTTP 用户名从 Apache 传递到 Mongrel/Rails?

发布于 2024-08-10 12:49:14 字数 967 浏览 3 评论 0原文

目标:在 Mongrels 上运行 Rails 应用程序,在进行基本 HTTP 身份验证后允许通过 Apache 进行访问

问题:从 Rails

Apache 中读取提供的用户名:

<Proxy balancer://mongrel_cluster>
    BalancerMember http://127.0.0.1:4001
    # ...

    Order deny,allow
    Deny from all

    AuthType Basic
    AuthName "<realm>"
    AuthUserFile "<users-file>"
    AuthGroupFile "<groups-file>"
    Require group <group>

    Satisfy Any
</Proxy>

RewriteEngine On
# ...
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]

效果很好, Apache 强制用户进行身份验证,如果成功则转发到 Rails。我省略了几行来处理静态文件等,并触发它们的身份验证。

从 Rails 的角度来看,环境变量包含常用条目以及 HTTP_X_FORWARDED_HOSTHTTP_X_FORWARDED_SERVERHTTP_X_FORWARDED_FOR。我无法通过将自定义环境变量添加到重写规则来传递它们:

RewriteRule ... [P,QSA,L,E=foo:bar]

有什么想法吗?

The goal: running a Rails application on Mongrels, allowing access through Apache after doing basic HTTP Authentication

The problem: reading the supplied username from within Rails

Apache:

<Proxy balancer://mongrel_cluster>
    BalancerMember http://127.0.0.1:4001
    # ...

    Order deny,allow
    Deny from all

    AuthType Basic
    AuthName "<realm>"
    AuthUserFile "<users-file>"
    AuthGroupFile "<groups-file>"
    Require group <group>

    Satisfy Any
</Proxy>

RewriteEngine On
# ...
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]

That works just fine, Apache forces the user to authenticate and forwards to Rails if successful. I omitted a few lines for handling static files and such, and triggering authentication for them as well.

The environment variables from Rails' perspective contain the usual entries and additionally HTTP_X_FORWARDED_HOST, HTTP_X_FORWARDED_SERVER and HTTP_X_FORWARDED_FOR. I was unable to pass custom environment variables by adding them to the rewrite rule:

RewriteRule ... [P,QSA,L,E=foo:bar]

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2024-08-17 12:49:14

尝试使用 RequestHeader 指令将 REMOTE_USER 放入 HTTP 标头中。这似乎对此线程中的人们有效:

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

Try using the RequestHeader directive to put REMOTE_USER in an HTTP header. This seems to have worked for the folks in this thread:

RewriteCond %{LA-U:REMOTE_USER} (.+)
RewriteRule . - [E=RU:%1]
RequestHeader add X-Forwarded-User %{RU}e
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文