2009-07-17 160 views
2

我正在尝试使用由我的域中的其他页面设置的cookie来验证用户身份。 假设我有使用cakephp编写的needpassword.example.com, ,并且cookie由auth.example.com(使用Perl CGI程序)生成。如何在CakePHP中使用Cookie进行身份验证?

要登录到needpassword.example.com,我需要重定向到auth.example.com以设置cookie,然后使用CakePHP来解析cookie。

我该如何解析这个cookie?我如何修改Auth组件来完成这些?

如何覆盖Auth类而不是去auth.example.com进行身份验证,而不是使用用户模型?通过重写Auth.php中的标识方法?

非常感谢。

+0

您是否检查过是否使用绝对URL(即http://auth.example.com)设置AuthComponent :: loginAction参数? – deizel 2009-07-17 13:15:40

回答

2

由于您的需求听起来与AuthComponent最初设计的设计不同,所以您有两种选择。首先,如果它确实不符合你的需求,你可以创建和维护你自己的AuthComponent。通过将/cake/libs/controller/components/auth.php复制到/app/controller/components/auth.php来完成此操作。

这将允许您完全重写组件,但缺点是升级蛋糕时,您将不再接收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); 
    } 
} 

..和你AppAuthComponent取代的AuthComponent所有实例的控制器。

  • 您只需要定义您想要替换的方法。
  • 可以使用parent::...
  • 方法参数应保持in the same order as the original API一致性运行期间,你的方法从原来的AuthComponent在任何一点的方法(甚至是那些您已经重新定义)。
  • 如果你想添加更多方法的参数,把他们的API的人后,如:

    function identify($user = null, $conditions = null, $custom = array()) { ... }

这种方法可以让你做出领域的应用需求,同时还采用了最新的方法必要时在核心中定义。

0

假设我了解您的问题...只要auth.example.com将cookie设置为域名“.example.com”,用户浏览器就会将它与请求一起发送到needpassword.example.com和您可以用下面的访问它在你的PHP脚本:

$auth = $_COOKIE['auth'];

然后,您可以更改cookie的有以下:

setcookie("auth", "value", time() + 300, "/", ".example.com");

(注:时间()+ 300套cookie将来的有效期限为5分钟,您可能需要更改此项)

+0

嗨,它看起来很容易,这正是我想要的。 那么我怎样才能覆盖Auth类去取代auth.example.com进行身份验证,而不是使用User模型? – 2009-07-17 05:34:51

相关问题