【问题标题】:Cakephp authentication: 2.4.2Cakephp 认证:2.4.2
【发布时间】:2014-01-02 23:43:41
【问题描述】:

我的问题是用户表的型号名称不同:Breeder。我的登录页面总是显示不正确的用户名或密码,因为输入的密码没有经过哈希处理。

这里是视图:

<?php
    echo $this->Form->create();
    echo $this->Form->input('username');
    echo $this->Form->input('password');
    echo $this->Form->end('Log in');
    echo $this->Html->link('Register', '/breeders/register');
?>

这是 AppController:

类 AppController 扩展控制器 {

public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session', 'Paginator',
  'Auth' => array(
    'loginAction' => array('controller' => 'breeders', 'action' => 'login'),
    'loginRedirect' => array('controller' => 'breeders', 'action' => 'desk'),
    'logoutRedirect' => array('controller' => 'breeders', 'action' => 'login'),
    'authorize' => array('Controller'),
    'authenticate' => array(
      'Form' => array(
        'fields' => array(
          'username' => 'login',
          'password' => 'password'),
        'passwordHasher' => array(
          'className' => 'Simple',
          'hashType' => 'sha256'
          )
        )
      )
    )
  );

public function isAuthorized($user)
{
    return true;
}

public function beforeFilter() {

    $this->Auth->allow('login', 'logout', 'register', 'profile');
}
}

我的登录方式:

public function login() {
  $this->set('title_for_layout', __('Connection'));

  if ($this->Session->read('Auth.Breeder')) {
    return $this->redirect('/');
  }

  if ($this->request->is('post')) {
    if ($this->Auth->login()) {
      return $this->redirect($this->Auth->redirectUrl());
    }
  }
}

以及模型中的 beforeSave 方法:

public function beforeSave($options = array()) {

  if (!$this->id) {
    $passwordHasher = new SimplePasswordHasher();
    $this->data['Breeder']['password'] = $passwordHasher->hash(
      $this->data['Breeder']['password']
      );
  }
  return true;
}

我不知道我需要添加什么来使密码被散列。欢迎任何帮助。

【问题讨论】:

    标签: php cakephp authentication


    【解决方案1】:

    你为什么不使用改变用户模型

    array('userModel' => '饲养员')

    参考http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#configuring-authentication-handlers

    【讨论】:

    • 在测试几种可能性的过程中,我似乎把它取下来了,但我试过了。现在我刚刚添加了它,因为我的登录功能有效(我手动散列密码,所以它应该散列两次)我想这不是解决方案。还是谢谢你
    【解决方案2】:

    由于没有手动哈希我无法做到这一点,所以我在控制器登录功能中使用了这段代码:

    public function login() {
    
      $this->set('title_for_layout', __('Connection'));
    
      if ($this->Session->read('Auth.Breeder')) {
        return $this->redirect('/');
      }
    
      if ($this->request->is('post')) {
        $passwordHasher = new SimplePasswordHasher();
        $this->request->data['Breeder']['password'] = $passwordHasher->hash(
          $this->request->data['Breeder']['password']
        );
        $breeder = $this->Breeder->find('first', array('conditions' => array('Breeder.login' => $this->request->data['Breeder']['username']), 'fields' => array('id')));
    
        $this->request->data['Breeder'] = array_merge($this->request->data['Breeder'], array('id' => $breeder['Breeder']['id']));
    
        if ($this->Auth->login($this->request->data['Breeder'])) {
          return $this->redirect($this->Auth->redirectUrl());
        }
      }
    }
    

    如果有人有更好的解决方案,请不要犹豫,写出来。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-26
      • 2012-01-18
      相关资源
      最近更新 更多