Authentication cakephp3.0auth&;单点登录

Authentication cakephp3.0auth&;单点登录,authentication,cakephp-3.0,Authentication,Cakephp 3.0,我使用的是cakephp 3.0,在登录页面中,尽管我试图输入正确的凭据,但收到了无效的用户名或密码消息。 在我的用户表中,我有用户名和密码字段,我想用它们来验证用户身份。 除了包含密码散列的步骤外,我还尝试了遵循3.0的蛋糕书文档。还使用了与cakephp文档中类似的表结构 是因为它访问了错误的表吗?我已经提到了App Controller中的字段 此外,如果我必须实现单点登录,是否有插件可以帮助我?请提供此类教程的链接,因为这对我的研究生学习项目很重要 我的模型文件: class Users

我使用的是cakephp 3.0,在登录页面中,尽管我试图输入正确的凭据,但收到了无效的用户名或密码消息。 在我的用户表中,我有用户名和密码字段,我想用它们来验证用户身份。 除了包含密码散列的步骤外,我还尝试了遵循3.0的蛋糕书文档。还使用了与cakephp文档中类似的表结构 是因为它访问了错误的表吗?我已经提到了App Controller中的字段

此外,如果我必须实现单点登录,是否有插件可以帮助我?请提供此类教程的链接,因为这对我的研究生学习项目很重要

我的模型文件:

class UsersTable extends Table
{
    public function initialize(array $config)
    {
        $this->table('users');
        $this->displayField('User_id');
        $this->primaryKey('User_id');
        $this->addBehavior('Timestamp');
    }


    public function validationDefault(Validator $validator)
    {
        $validator
            ->add('id', 'valid', ['rule' => 'numeric'])
            ->notEmpty('id', 'create')
            ->notEmpty('username','Username is required')
            ->notEmpty('password','Password is required')
            ->notEmpty('role');


        return $validator;
    }


    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->isUnique(['username']));
        return $rules;
    }
}
我的用户控制器文件

<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Core\App;
use Cake\Event\Event;

class UsersController extends AppController
{ 

    public function view($id = null)
    {
        $user = $this->Users->get($id, [
            'contain' => []
        ]);
        $this->set('user', $user);
        $this->set('_serialize', ['user']);
    }

    public function register()
    {
        $user = $this->Users->newEntity();
        if ($this->request->is('post')) {
            $user = $this->Users->patchEntity($user, $this->request->data);
            if ($this->Users->save($user)) {
                $this->Flash->success('The user basic details has been saved.');
            } else {
                $this->Flash->error('The user could not be saved. Please, try again.');
            }
        }
        $this->set(compact('user'));
        $this->set('_serialize', ['user']);
    }


    public function edit($id = null)
    {
        $user = $this->Users->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $user = $this->Users->patchEntity($user, $this->request->data);
            if ($this->Users->save($user)) {
                $this->Flash->success('The user has been saved.');
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error('The user could not be saved. Please, try again.');
            }
        }
        $this->set(compact('user'));
        $this->set('_serialize', ['user']);
    }


    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $user = $this->Users->get($id);
        if ($this->Users->delete($user)) {
            $this->Flash->success('The user has been deleted.');
        } else {
            $this->Flash->error('The user could not be deleted. Please, try again.');
        }
        return $this->redirect(['action' => 'index']);
    }




    public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);
        // Allow users to register and logout.
        // You should not add the "login" action to allow list. Doing so would
        // cause problems with normal functioning of AuthComponent.
        $this->Auth->allow(['register', 'logout']);
    }

    public function login()
    {
        if ($this->request->is('post')) {
            $user = $this->Auth->identify();
            $this->set('user',$user);
            if ($user) {
                $this->Auth->setUser($user);
                $this->Flash->error(__('Login successful'));

                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->error(__('Invalid username or password, try again'));
        }

    }

    public function logout()
    {
        return $this->redirect($this->Auth->logout());
    }

     public function index()
    {
        $this->set('users', $this->paginate($this->Users));
        $this->set('_serialize', ['users']);
    }
}
class AppController extends Controller
{
    public function initialize()
    {
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
            'authenticate' => [
                'Form' => [
                    'fields' => [
                        'username' => 'username',
                        'password' => 'password'
                    ]
                ]
            ],
            'loginRedirect' => [
                'controller' => 'Orders',
                'action' => 'view'
            ],
            'logoutRedirect' => [
                'controller' => 'Pages',
                'action' => 'display',
                'home'
            ]
        ]);


        $this->Auth->allow(['display']);
    }

    public function beforeFilter(Event $event)
    {
        $this->Auth->allow(['index', 'view', 'display','login']);
    }
}
我的登录页面:

<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->button(__('Register')); ?>
        <?=$ this->Form->end() ?>
</div>

我发现了问题所在。 首先,我没有包括密码散列器方法。 在尝试包含它时,我将它添加到了错误的模块中。必须为实体类文件添加它,而我将它添加到UserTable.php。 这对我的案子毫无帮助。我按照手册去了T。 在添加密码哈希器模块后,我添加了一个新用户。 此外,只有当密码长度为varchar(255)时,它才有效 因此,创建了用户和哈希密码。
然后我就可以登录了

,那么您为什么不完全按照文档示例操作呢?密码散列在这里有点重要。我在模型文件中包含了一个密码散列方法,但它似乎没有帮助。你能给我们你的数据库吗?Users表应该足够了。您需要确保可以将哈希密码中的所有字符存储在数据库中。瓦查尔(45岁)不会工作。varchar(255)将起作用。