前提・実現したいこと
CakePHP3.5.16でAuthComponentで認証機能を付けようとしています。その際、passwordキーはdefaultのpassordHasherを使用し、usernameキーはSecurity::encryptで暗号化した物を使用したいと考えています。
実際にログインフォームより、usernameキー値とpasswordキー値を入力して送信すると、$this->Auth->identify()でfalseが戻り、認証に失敗してしまいます。
該当のソースコード
AppController.php
$this->loadComponent('Auth', [ 'authorize' => ['Controller'], 'storage' => 'Session', 'authenticate' => [ 'Form' => [ 'fields' => [ 'username' => 'userid',//usernameキーにはuseridフィールドを使用しています。 'password' => 'password' ], 'userModel' => 'Members', ], ], //(リダイレクト関連一部省略) ]);
Member.php
public function _defaultEncrypt($value) { return Security::encrypt(trim($value), $this->defaultKey); } public function _defaultDecrypt($name, $value) { return $this->dirty($name) ? $value : Security::decrypt(trim($value), $this->defaultKey); } /* * 会員IDの暗号化 * */ protected function _setUserid($value) { if ($value) return $this->_defaultEncrypt($value); } protected function _getUserid($value) { if ($value) return $this->_defaultDecrypt('userid', $value); } /* * パスワードのハッシュ化 * */ protected function _setPassword($value) { $hasher = new DefaultPasswordHasher(); return $hasher->hash($value); }
MembersController.php
/* * 会員アカウントのログイン **/ public function login() { //ログイン済み if($this->_loginSt) return $this->redirect(['action'=>'home']); $member = $this->Members->newEntity(); if($this->request->is(['post','put'])) { $identify = $this->Auth->identify(); if ($identify) { $this->Auth->setUser($identify); } else { $this->set(compact('member')); $this->Flash->error('入力値が間違っているか、メールアドレスの認証をされていない可能性があり>ます'); } }
試したこと
- DBのpasswordフィールドとuseridフィールドに値が入っている事を確認 ⇒ 入っていました。
- useridを平文に変更(Members.phpで実行している暗号化をコメントアウト) ⇒ DBに平文でuseridが入り、identify()でもユーザー情報が取得できました。

回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/08 08:55
2018/06/08 14:50
2018/06/08 15:16
2018/06/10 12:46
2020/10/26 13:09 編集