如何在 Apache 中为 CakePhp 应用程序配置密码保护例外?

发布于 2024-09-09 10:29:21 字数 1563 浏览 1 评论 0原文

我有一个 CakePHP 应用程序,我想用密码保护它。棘手的是,除了一个特定的地址(CakePHP 控制器的一个函数)之外,所有文件/位置都只能通过密码访问。

地址如下:

http://example.com/MyApp/MyController/MyFunction?MyParam=MyValue

所有其他位置只能通过密码访问

http://example.com/MyApp/MyController/MyOtherFunction
http://example.com/MyApp/MyController/MyOtherFunction
http://example.com/MyApp/MyOtherController/MyOtherFunction

嗯,我首先尝试了根 .htaccess-File,但 CakePHP 的整个重写使其变得非常困难,并且在 .htaccess-Files 中不允许 指令。所以我用 尝试了它,但真正的文件总是相同的:index.php。 mod_rewrite 将所有地址重写为

http://example.com/MyApp/app/webroot/ index.php?url=$1

在下一步中,我在 apache 配置中尝试了它,并将此部分放在那里,

<LocationMatch ^/MyApp/MyController/MyFunction.*>
  AuthType Basic
  AuthName "Secure Area"
  AuthUserFile /path/to/.htpasswd
  Require user MyUser
</LocationMatch>

正则表达式匹配,但这是错误的方式。它保护 MyFunction 但不保护其余部分。

I have a CakePHP Application which I want to protect with a password. The tricky thing is, that all files/locations should be only accessible with a password EXCEPT one specific Address (a function withing a CakePHP-controller)

The Address is like that:

http://example.com/MyApp/MyController/MyFunction?MyParam=MyValue

All other locations should be only accessible with a password

http://example.com/MyApp/MyController/MyOtherFunction
http://example.com/MyApp/MyController/MyOtherFunction
http://example.com/MyApp/MyOtherController/MyOtherFunction

Well, I tried it first in the root .htaccess-File, but the whole rewrite-thing of CakePHP makes it very difficult and in .htaccess-Files are no <LocationMatch> directive allowed. So I tried it with <FilesMatch>, but the real File is always the same: index.php. mod_rewrite rewrites all Addresses to

http://example.com/MyApp/app/webroot/index.php?url=$1

In the next step I tried it in the apache-configuration and put there this section

<LocationMatch ^/MyApp/MyController/MyFunction.*>
  AuthType Basic
  AuthName "Secure Area"
  AuthUserFile /path/to/.htpasswd
  Require user MyUser
</LocationMatch>

Well the regex matched, but it was the wrong way. It protects MyFunction but not the rest.

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

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

发布评论

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

评论(1

何以笙箫默 2024-09-16 10:29:21

您使用 .htpasswd 吗?您可能更好地使用 Cake Auth,然后您可以在适当的控制器中执行此操作:

function beforeFilter() {
    $this->Auth->allow('MyFunction');
}

Are you using .htpasswd? You might be better using Cake Auth, then you can do this in the appropriate controller:

function beforeFilter() {
    $this->Auth->allow('MyFunction');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文