【问题标题】:Authentication with CakePHP 2.4 only with sha1?仅使用 sha1 使用 CakePHP 2.4 进行身份验证?
【发布时间】:2014-01-10 18:14:02
【问题描述】:

我在 CakePHP 2.4 中遇到了一些身份验证问题。我烘焙了一个简单的应用程序来创建登录。我做了书中所有的事情(授权和教程)。但只有 sha1 有效。但不是 sha256 或 md5。在搜索和测试之后,我找到了一个解决方案,我必须更改书中的示例代码,现在它可以工作了。但我认为,这不是正确的解决方案。

我在 AppController 中做了以下操作:

App::uses('Controller', 'Controller');
    class AppController extends Controller {
    public $components = array(
        'Session',
        'Auth' => array(
            'authenticate' => array(
                'Form' => array(
                    'passwordHasher' => array(
                        'className' => 'Simple',
                        'hashType' => 'sha256'
                    )
                )
            )
        )
    );
}

我用 md5、sha1 和 sha256 对此进行了测试。没问题。如果密码经过适当的哈希处理,则可以登录。

但我注意到,添加用户仅适用于 sha1,因为这是默认哈希值。 My User Models beforeSafe 功能是这样的(来自书中):

App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
class User extends AppModel {
  public function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['password'])) {
        $passwordHasher = new SimplePasswordHasher();
          $this->data[$this->alias]['password'] = $passwordHasher->hash(
              $this->data[$this->alias]['password']
          );
    }
    return true;
  }
}

我想,AppController 中的设置就足够了,也可以更改该区域的 SimplePasswordHasher。但看起来,这还不够。所以我把它改成了这样:

$this->data[$this->alias]['password'] = $passwordHasher->hash(
    $this->data[$this->alias]['password']
);

到这里

$this->data[$this->alias]['password'] = Security::hash(
    $this->data[$this->alias]['password'],
'sha256',
true
);

现在一切都像魅力一样运作。但我的问题:

1) 我是对的,这是必要的还是我的代码有其他问题?

2) 据我所知,$xxx->zzz 仅适用于控制器,xxx::zzz() 适用于任何地方,对吧?

3) 为什么我必须说安全哈希,它应该再次使用 sha256 对字符串进行哈希处理,而我通常在 AppController 中已经说过了?

【问题讨论】:

    标签: authentication login cakephp-2.4


    【解决方案1】:

    1. 据我所知,您的方法是正确的。可能存在一种更清洁的方式,但我使用的是bcrypt,Cookbook 说要做一些非常相似的事情。

    2.你可以像你说的那样想一个简单的解释。基本上->object-operator,用于处理对象,例如$this->doEverything。而::scope-resolution-operator,用于查找类静态属性或方法。通常你可以在任何地方运行它,例如,Security::hash()

    3.看看here,你应该把AppController改成:

    'passwordHasher' => array(
                    'className' => 'Simple',
                    'hashType' => 'sha256'
                )
    

    以适应建议。尽管我已经尝试过了,但没有将SimplePasswordHasher 更改为 SHA256。这是蛋糕似乎不知何故忽略的参考。但那是简单的哈希器,旨在简单的事情,所以你应该做你自己的方法,或者一种更适合你需要的方法。

    【讨论】:

      猜你喜欢
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      • 2020-02-28
      • 2020-08-29
      相关资源
      最近更新 更多