CakePHP2.4.2でBlowfishを使ったログインの処理ではまっております。
以前に一度実装したのですが、今回はなぜかうまく実装ができていません。
usersテーブルへの登録自体はうまくいっているのですが、
どうもログイン時にパスワードが一致しないようです。
sql
1CREATE TABLE users ( 2 id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, 3 username VARCHAR(50), 4 password VARCHAR(255), 5 created DATETIME DEFAULT NULL, 6 modified DATETIME DEFAULT NULL 7);
php
1<?php 2 3//app/Controller/UsersController.php 4App::uses('AppController', 'Controller'); 5 6class UsersController extends AppController { 7 8 public $name = "Users"; 9 public $components = array( 10 'DebugKit.Toolbar', 11 'Session', 12 'Cookie', 13 'Security', 14 'Auth' => array( 15 'authError' => 'ログインしてください。', 16 'authenticate' => array( 17 'Form' => array( 18 'passwordHasher' => 'Blowfish' 19 ) 20 ), 21 'authorize' => array('Controller'), 22 'logoutRedirect' => array('controller' => 'Users', 'action' => 'login'), 23 'loginAction' => array('controller' => 'Users', 'action' => 'login'), 24 ) 25 ); 26 27 public function beforeFilter() { 28 parent::beforeFilter(); 29 $this->Auth->allow('registerUser','login','logout'); 30 $this->Security->requireAuth('registerUser','login'); 31 } 32 33 public function login(){ 34 35 if ($this->request->is('post')) { 36 if($this->Auth->login()){ 37 return $this->flash('認証に成功しました。', '/users/index'); 38 }else{ 39 return $this->flash('認証に失敗しました。', '/users/index'); 40 } 41 } 42 } 43 44 public function registerUser(){ 45 46 if ($this->request->is('post')) { 47 $this->User->create(); 48 if ($this->User->save($this->request->data)) { 49 $this->Session->setFlash(__('The user has been saved.')); 50 exit; 51 #return $this->redirect(array('action' => 'index')); 52 } else { 53 $this->Session->setFlash(__('The user could not be saved. Please, try again.')); 54 } 55 } 56 } 57} 58
php
1 2<?php 3 4App::uses('AppModel', 'Model'); 5App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth'); 6 7// app/Model/User.php 8class User extends AppModel { 9 10 public $validate = array( 11 'username' => array( 12 'notEmpty' => array( 13 'rule' => array('email'), 14 'message' => 'メールアドレスかパスワードに誤りがあります。' 15 ), 16 'isUnique' => Array( 17 'rule' => 'isUnique', 18 'message' => 'すでに登録のあるメールアドレスです。' 19 ) 20 ), 21 'password' => array( 22 'notEmpty' => array( 23 'rule' => array('custom', '/\A[A-Za-z0-9_()\*\+,\-\.\/:;<=>?]{8,150}\z/'), 24 'message' => 'パスワードの形式が不正です。' 25 ) 26 ) 27 ); 28 29 public function beforeSave($options = array()) { 30 if (isset($this->data[$this->alias]['password'])) { 31 $passwordHasher = new BlowfishPasswordHasher(); 32 $this->data[$this->alias]['password'] = $passwordHasher->hash( 33 $this->data[$this->alias]['password'] 34 ); 35 } 36 return true; 37 } 38} 39
app/Controller/AppController.phpなどには手を入れていません。
ちなみにBlowfishを使わずに、
登録・ログインをした場合には正常にログイン処理が行えます。
php
1 public function beforeSave($options = array()) { 2 $this->data['User']['password'] = AuthComponent::password( 3 $this->data['User']['password'] 4 ); 5 return true; 6 } 7}
beforeSaveメソッドを上記に変更、
UsersController.phpのcomponents内の
以下をコメントアウトすると正常に動作。
php
1 'passwordHasher' => 'Blowfish'
おそらくは単純なミスかと思いますが、
はまってしまって抜け出せずにいます。
他に必要な処理などがあるか、
何かお気付きの点があれば教えていただけますと幸いです。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/10/13 17:04