我如何使用Cookie在CakePHP的认证? [英] How can I use cookies for authentication in CakePHP?

查看:244
本文介绍了我如何使用Cookie在CakePHP的认证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用由其他页面设置Cookie在我的域用户进行身份验证。
说我有needpassword.example.com使用CakePHP写的,
并通过auth.example.com生成的cookie(使用一个Perl CGI程序)。

I am trying to use a cookie that is set by other page in my domain to authenticate the user. Say I have needpassword.example.com written using cakephp, and the cookie is generated by auth.example.com (using a Perl CGI program).

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

To login in to needpassword.example.com, I need to redirect to auth.example.com to set the cookie, and then use CakePHP to parse the cookie.

我如何解析这个cookie?我如何修改验证组件做这些?

How do I parse this cookie? And how do I modify the Auth component to do these?

和我怎么能覆盖验证类改为向auth.example.com进行身份验证,而不是使用用户模式?通过重写Auth.php的鉴别方法是什么?

And how can I override the Auth class to instead go to the auth.example.com to authenticate, and not using the User model? By overriding the identify method in Auth.php?

非常感谢。

推荐答案

由于您的需求声音outwith AuthComponent的原定设计,你有两个选择。

Since your needs sound outwith AuthComponent's originally intended design you have two options.

首先,如果真的不适合您的需求,您可以创建和维护自己的AuthComponent。通过复制 /cake/libs/controller/components/auth.php /app/controller/components/auth.php

Firstly, if it really doesn't fit your needs, you could create and maintain your very own AuthComponent. Do this by copying /cake/libs/controller/components/auth.php to /app/controller/components/auth.php.

这将让你彻底重写组件,但缺点是,你将不再接收更新,当您升级蛋糕AuthComponent。

This would allow you to rewrite the component completely, but the downside is you will no longer receive updates to AuthComponent when you upgrade cake.

其次,你可以几​​乎任何在CakePHP中使用以下模式扩展:

Secondly, you can extend just about anything in CakePHP using the following pattern:

// 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

.. and replace all instances of AuthComponent in your controllers with your AppAuthComponent.


  • 您只需要定义你希望的方式来代替。

  • 您可以运行从原来的AuthComponent方法期间使用的 父:: ...

  • 方法参数应保持在同一顺序作为原始API 的一致性。

  • 如果你希望那些API后,添加更多的方法参数,把它们,例如:

  • You only need to define the methods you wish to replace.
  • You can run methods from the original AuthComponent (even ones you have redefined) at any point during your methods using parent::...
  • The method arguments should remain in the same order as the original API for consistency.
  • If you wish to add more method arguments, put them after the API ones, eg:

函数识别($ USER = NULL,$条件= NULL,$定制=阵列()){...}

这个方法可以让你做出特定应用的定制,同时还采用了核心定义的最新方法在必要。

This approach allows you to make application-specific customisation while still using the latest methods defined in the core where necessary.

这篇关于我如何使用Cookie在CakePHP的认证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆