duanhemou9834 2016-07-27 07:58
浏览 70

CakePHP - 自定义密码

Recently I try to change the encoding of the password on CakePHP by directly adding a new file. Unfortunately I do not seem to connect, namely that her works perfectly with the class Simple in sha256

AppController

class AppController extends Controller {
public $components = array('Auth' => array(
    'authenticate'  => array(
        'Form'  => array(
            'fields'    => array('username'   => 'email','password' => 'password'),
            'passwordHasher'    => array(
                'className' => 'Custom'),
            'scope' => array('active'   => '1')
        )
    )
));

UsersController(login)

    App::uses('CustomPasswordHasher', 'Controller/Component/Auth');

class UsersController extends AppController{

    public function login()
    {

        $erreur = false;
        if($this->request->is('post')) {
            if(!empty($this->data)) {
                if(!$this->Auth->user('id')) {
                    if($this->Auth->login()) {
                        echo 'ok';
                    } else {
                        $erreur = "Identifiant incorrect.";
                    }
                } else {
                    $erreur = "Vous êtes déjà connecter.";
                }
            } else {
                $erreur = "Veuillez saisir vos identifiants";
            }
            $this->set(compact('erreur'));
        }
    }
}

CustomPasswordHasher

    App::uses('AbstractPasswordHasher', 'Controller/Component/Auth');
class CustomPasswordHasher extends AbstractPasswordHasher {
    public function hash($password)
    {
        $before = substr(Configure::read('Security.salt'),0,43);
        $after = substr(Configure::read('Security.salt'),43,42);

        $chaine = $before.$password.$after;
        return hash("sha256",$chaine);

    }
    public function check($password, $email, $hashType = null) {
        $this->User = ClassRegistry::init('User');
        $u = $this->User->find('first', array(
            'fields'        => array('user.password'),
            'conditions'    => array('user.email'   => $email)
        ));
        if($u) {
           return $this->hash($password) == $u['User']['password'];
        }
        return false;
    }
}

An idea where my problem might come?

Thank you !

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看