cakephp 3.0 Auth&单点登录

时间:2015-04-16 21:20:42

标签: authentication cakephp-3.0

我正在使用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;
    }
}

我的UserController文件

<?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']);
    }
}

我的AppController文件

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>

1 个答案:

答案 0 :(得分:0)

我弄清楚出了什么问题。 首先我没有包含密码hasher方法。 在尝试包含它时,我将它添加到了错误的模块中。必须为Entity类文件添加它,而我将它添加到UserTable.php中。 这根本没有帮助我。我按照手册到了T. 添加密码Hasher模块后,我添加了一个新用户。 它也适用于密码长度为varchar(255)的情况 因此,创建了用户以及散列密码。 我当时能够登录