代码之家  ›  专栏  ›  技术社区  ›  danilo

CakePHP 3.6身份验证不起作用

  •  -2
  • danilo  · 技术社区  · 6 年前

    下面是CakePHP教程:
    https://book.cakephp.org/3.0/en/tutorials-and-examples/bookmarks/part-two.html

    我的CakePHP版本是:3.6.0
    我的Php版本是:7.2

    这是我的src\Controller\AppController。php

    <?php
    namespace App\Controller;
    
    use Cake\Controller\Controller;
    use Cake\Event\Event;
    class AppController extends Controller
    {
    
        public function initialize()
        {
            parent::initialize();
    
            $this->loadComponent('RequestHandler');
            $this->loadComponent('Flash');
            $this->loadComponent('Auth', [
                'authenticate' => [
                    'Form' => [
                        'fields' => [
                            'username' => 'email',
                            'password' => 'password'
                        ]
                    ]
                ],
                'loginAction' => [
                    'controller' => 'Users',
                    'action' => 'login'
                ],
                'unauthorizedRedirect' => $this->referer() 
            ]);
    
            $this->Auth->allow(['display']);
    
        }
    }
    

    在我的src\Controller\UsersController中。php I添加了以下方法:

    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('Your username or password is incorrect.');
        }
    }
    

    我创建了src\Template\Users\login。ctp文件:

    <h1>Login</h1>
    <?= $this->Form->create() ?>
    <?= $this->Form->control('email') ?>
    <?= $this->Form->control('password') ?>
    <?= $this->Form->button('Login') ?>
    <?= $this->Form->end() ?>
    

    我的浏览器正在通知错误:ERR\u TOO\u MANY\u重定向

    如果我尝试转到以下url:localhost:8765/书签

    浏览器重定向到: http://localhost:8765/users/login?redirect=%2Fusers%2Flogin%3Fredirect%3D%252Fusers%252Flogin%253Fredirect%253D%25252Fusers%25252Flogin%25253Fredirect%25253D%2525252Fusers%2525252Flogin%2525253Fredirect%2525253D%252525252Fusers%252525252Flogin%252525253Fredirect%252525253D%25252525252Fusers%25252525252Flogin%25252525253Fredirect%25252525253D%2525252525252Fusers%2525252525252Flogin%2525252525253Fredirect%2525252525253D%252525252525252Fusers%252525252525252Flogin%252525252525253Fredirect%252525252525253D%25252525252525252Fusers%25252525252525252Flogin%25252525252525253Fredirect%25252525252525253D%2525252525252525252Fusers%2525252525252525252Flogin%2525252525252525253Fredirect%2525252525252525253D%252525252525252525252Fusers%252525252525252525252Flogin%252525252525252525253Fredirect%252525252525252525253D%25252525252525252525252Fusers%25252525252525252525252Flogin%25252525252525252525253Fredirect%25252525252525252525253D%2525252525252525252525252Fusers%2525252525252525252525252Flogin%2525252525252525252525253Fredirect%2525252525252525252525253D%252525252525252525252525252Fusers%252525252525252525252525252Flogin%252525252525252525252525253Fredirect%252525252525252525252525253D%25252525252525252525252525252Fusers%25252525252525252525252525252Flogin%25252525252525252525252525253Fredirect%25252525252525252525252525253D%2525252525252525252525252525252Fusers%2525252525252525252525252525252Flogin%2525252525252525252525252525253Fredirect%2525252525252525252525252525253D%252525252525252525252525252525252Fusers%252525252525252525252525252525252Flogin%252525252525252525252525252525253Fredirect%252525252525252525252525252525253D%25252525252525252525252525252525252Fusers%25252525252525252525252525252525252Flogin%25252525252525252525252525252525253Fredirect%25252525252525252525252525252525253D%2525252525252525252525252525252525252Fusers%2525252525252525252525252525252525252Flogin%2525252525252525252525252525252525253Fredirect%2525252525252525252525252525252525253D%252525252525252525252525252525252525252Fbookmarks

    1 回复  |  直到 6 年前
        1
  •  1
  •   kicaj    6 年前

    您应该添加 login 允许列表的操作:

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

    或者阅读以下评论: https://stackoverflow.com/a/49928585/182823