如何在CakePHP中使用Cookie进行身份验证?

编程入门 行业动态 更新时间:2024-10-27 04:24:23
本文介绍了如何在CakePHP中使用Cookie进行身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用我网域中其他网页设定的Cookie来验证使用者。 说我有使用cakephp,编写的needpassword.example和cookie是由auth.example(使用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 written using cakephp, and the cookie is generated by auth.example (using a Perl CGI program).

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

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

如何解析此Cookie?如何修改Auth组件来执行这些操作?

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

如何覆盖Auth类,而不是去auth.example来认证,而不是使用User模型?通过覆盖Auth.php中的识别方法?

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

非常感谢。

推荐答案

由于您的需求听起来与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); } }

..并替换<$ c $您的控制器中使用您的 AppAuthComponent 。

如果您想添加更多的方法参数,请将它们放在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:

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

这种方法允许您进行应用程序特定的自定义,同时仍然使用核心中定义的最新方法。

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

更多推荐

如何在CakePHP中使用Cookie进行身份验证?

本文发布于:2023-10-10 12:14:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1478642.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:身份验证   如何在   CakePHP   Cookie

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!