web-dev-qa-db-ja.com

ログイン[Auth-> identify()]はCakePHP3では常にfalse

CakePHP 2を使用した後、CakePHP 3を使用し始めましたが、認証ログインの作成に問題があります。

新しい認証関数$this->Auth->identify()は常にfalseを返します。

データベースでは、パスワードは完全に暗号化されており、ユーザーを取得するクエリも問題ありません。

私のコード:

AppController:

[...]
class AppController extends Controller{
    public function initialize(){
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
            'loginRedirect' => [
                'controller' => 'Admin',
                'action' => 'index'
            ],
            'logoutRedirect' => [
                'controller' => 'Pages',
                'action' => 'display'
            ]
        ]);
    }

    public function beforeFilter(Event $event)
    {
        $this->Auth->allow(['display']);
    }
}

UserController:

[...]
class UsersController extends AppController{
    public function beforeFilter(Event $event)
    {
    parent::beforeFilter($event);
    $this->Auth->allow(['logout']);
    }
[...]
    public function login()
    {
        if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            if ($user) {
                $this->Auth->setUser($user);
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->error(__('Invalid username or password, try again'));
        }
    }
[...]

ユーザー(モデルエンティティ):

<?php
namespace App\Model\Entity;

use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;

class User extends Entity{
    protected $_accessible = [*];
    protected function _setPassword($password){
        return (new DefaultPasswordHasher)->hash($password);
    }
}

見る:

<div class="users form">
<?= $this->Flash->render('auth') ?>
<?= $this->Form->create() ?>
    <fieldset>
        <legend><?= __('Please enter your username and password') ?></legend>
        <?= $this->Form->input('username') ?>
        <?= $this->Form->input('password') ?>
    </fieldset>
<?= $this->Form->button(__('Login')); ?>
<?= $this->Form->end() ?>
</div>
10
Beto

CakePHP3は、デフォルトで2とは異なるハッシュアルゴリズム(bcryptとSHA1)を使用するため、パスワードの長さを長くする必要があります。安全のために、パスワードフィールドをVARCHAR(255)に変更してください。

CakePHP 3が、this-> Auth-> identity()とデータベース内のハッシュされたパスワードからメモリ内のハッシュされたパスワードを識別しようとすると、一部の文字が欠落しているため、一致しません。 255に変更することは必要以上ですが、さらに安全なハッシュが将来使用される場合、将来の証明に役立ちます。文字数は1バイトに格納できるため、255をお勧めします。

24
cs01

解決済み:データベースのタイプが必要未満でした。 varchar(255)に変更され、正常に動作するようになりました:)

5
Beto

私も同じ問題を抱えていました。ログイン[Auth-> identify()]が機能しませんでした。 dbでパスワードの長さを変更すると、問題が解決します。

1
Neeraj

こんにちは私のスニペットをLoginAuthに共有します、CakePHP 3.1では、すべてのテストはOKです、税関(Table + view login BootStrap 3 + SQL base + custom bootstrap.php for Spanish in Inflector :: rules( *******))

のすべてのコード

https://bitbucket.org/snippets/eom/rLo49

1
Sergio