加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

CakePHP 2:覆盖AuthComponent的“密码”方法

发布时间:2020-12-13 16:35:40 所属栏目:PHP教程 来源:网络整理
导读:我的目标是为每个用户提供唯一的盐,而不是为每个用户使用Configure :: read(‘Security.salt’). 我知道CakePHP 2.x不再自动密码密码.这允许我对密码执行模型验证,这是非常好的.但是,我没有看到我可以覆盖AuthComponent的“密码”方法.所以即使我可以控制密
我的目标是为每个用户提供唯一的盐,而不是为每个用户使用Configure :: read(‘Security.salt’).

我知道CakePHP 2.x不再自动密码密码.这允许我对密码执行模型验证,这是非常好的.但是,我没有看到我可以覆盖AuthComponent的“密码”方法.所以即使我可以控制密码在保存到数据库之前是如何进行散列的,所以我无法控制在执行实际登录时密码是如何散列的.从食谱:

You don’t need to hash passwords before calling
$this->Auth->login().

我可以做什么来使$this-> Auth-> login()使用自定义的密码哈希方法?

谢谢.

更新:我结束了汉尼拔Lecter博士的回答(创建一个自定义认证对象).以下是如何做到这一点:

旧代码:

$this->Auth->authenticate = array('Form' => array('fields' => array('username' => 'email')));

新代码(将“Form”更改为“Custom”):

$this->Auth->authenticate = array('Custom' => array('fields' => array('username' => 'email')));

创建“app / Controller / Component / Auth / CustomAuthenticate.php”,使其如下所示:

<?php
App::uses('FormAuthenticate','Controller/Component/Auth');

class CustomAuthenticate extends FormAuthenticate {
}

从“lib / Cake / Controller / Component / Auth / BaseAuthenticate.php”复制“_findUser”和“_password”方法,并将它们粘贴到“CustomAuthenticate”类中.然后对“_findUser”方法进行以下两个修改:

>从“$conditions”数组中删除此行:$model. ” . $fields [‘password’] => $这 – > _password($密码),
>更改if(empty($result)|| empty($result [$model])){to if(empty($result)|| empty($result [$model])|| $result [$model] $fields [‘password’]]!= $this-> _password($password,$result [$model] [‘id’])){

然后对“_password”方法进行以下两个修改:

>通过更改保护函数_password($password){保护函数_password($password,$id){
>通过更改return Security :: hash($password,null,true)来更新盐值;返回Security :: hash($password,Configure :: read(‘Security.salt’).$id);

最后,更新所有出现的AuthComponent :: password以使用与上述相同逻辑的Security :: hash.

你可能创建一个 custom auth object,并哈希密码,但你喜欢.看看 existing auth objects,了解它们的工作原理.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读