我正在尝试使用我的域中其他页面设置的 cookie 来验证用户身份。
假设我使用 cakephp 编写了 needpassword.example.com,
cookie 是由 auth.example.com 生成的(使用 Perl CGI 程序)。
要登录 needpassword.example.com,我需要重定向到 auth.example.com 以设置 cookie,然后使用 CakePHP 解析 cookie。
我该如何解析这个cookie?如何修改 Auth 组件来执行这些操作?
如何重写 Auth 类以转至 auth.example.com 进行身份验证,而不使用 User 模型?通过覆盖 Auth.php 中的识别方法?
非常感谢。
由于您的需求听起来与 AuthComponent 最初的预期设计不符,因此您有两种选择。
首先,如果它确实不符合您的需求,您可以创建并维护您自己的 AuthComponent。通过复制来完成此操作/cake/libs/controller/components/auth.php
to /app/controller/components/auth.php
.
这将允许您完全重写组件,但缺点是升级 cake 时您将不再收到 AuthComponent 的更新。
其次,您可以使用以下模式扩展 CakePHP 中的几乎任何内容:
// save as: /app/controllers/components/app_auth.php
App::import('Component', 'Auth');
class AppAuthComponent extends AuthComponent {
function identify($user = null, $conditions = null) {
// do stuff
return parent::indentify($user, $conditions);
}
}
..并替换所有实例AuthComponent
在你的控制器中与你的AppAuthComponent
.
- 您只需要定义您想要替换的方法。
- 您可以在使用方法期间的任何时候运行原始 AuthComponent 中的方法(甚至是您重新定义的方法)parent::...
- 方法参数应该保留与原始 API 的顺序相同为了一致性。
-
如果您想添加更多方法参数,请将它们放在 API 参数之后,例如:
function identify($user = null, $conditions = null, $custom = array()) { ... }
这种方法允许您进行特定于应用程序的定制,同时在必要时仍然使用核心中定义的最新方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)