我无法在Cakephp 4中进行身份验证,错误500

x33g5p2x  于 2022-11-11  发布在  PHP
关注(0)|答案(1)|浏览(117)

我试图通过用户俱乐部进行身份验证,但我收到一个错误500内部服务器,我不明白为什么,它只发生在我使用身份验证。也许是一个配置错误?
500内部服务器错误500内部服务器错误
Error.log

Request URL: /Users/login
Client IP: 192.168.152.1

2021-10-18 20:59:57 Error: [Exception] The request object does not contain the required `authentication` attribute in /opt/lampp/htdocs/learnwithus/vendor/cakephp/authentication/src/Controller/Component/AuthenticationComponent.php on line 142
Stack Trace:
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/authentication/src/Controller/Component/AuthenticationComponent.php:93
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/cakephp/src/Event/EventManager.php:309
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/cakephp/src/Event/EventManager.php:286
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/cakephp/src/Event/EventDispatcherTrait.php:92
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/cakephp/src/Controller/Controller.php:575
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/cakephp/src/Controller/ControllerFactory.php:96
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/cakephp/src/Http/BaseApplication.php:313
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/cakephp/src/Http/Runner.php:77
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/cakephp/src/Http/Middleware/CsrfProtectionMiddleware.php:159
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/cakephp/src/Http/Runner.php:73
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/cakephp/src/Http/Middleware/BodyParserMiddleware.php:159
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/cakephp/src/Http/Runner.php:73
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/cakephp/src/Routing/Middleware/RoutingMiddleware.php:161
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/cakephp/src/Http/Runner.php:73
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/cakephp/src/Routing/Middleware/AssetMiddleware.php:68
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/cakephp/src/Http/Runner.php:73
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/cakephp/src/Error/Middleware/ErrorHandlerMiddleware.php:126
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/cakephp/src/Http/Runner.php:73
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/debug_kit/src/Middleware/DebugKitMiddleware.php:60
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/cakephp/src/Http/Runner.php:73
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/cakephp/src/Http/Runner.php:58
- /opt/lampp/htdocs/learnwithus/vendor/cakephp/cakephp/src/Http/Server.php:90
- /opt/lampp/htdocs/learnwithus/webroot/index.php:40

我的控制器

<?php

    class UsersController extends AppController
    {

        public function beforeFilter(\Cake\Event\EventInterface $event) {
            parent::beforeFilter($event);
            $this->Authentication->addUnauthenticatedActions(['login']);
        }

        public function login() {
            $this->request->allowMethod(['get', 'post']);
            $result = $this->Authentication->getResult();
            if ($result->isValid()) {
                $redirect = $this->request->getQuery('redirect', [
                    'controller' => 'Users',
                    'action' => 'index',
                ]);

                return $this->redirect($redirect);
            }
            if ($this->request->is('post') && !$result->isValid()) {
                $this->Flash->error(__('Invalid username or password'));
            }
        }

        public function logout() {
            $result = $this->Authentication->getResult();
            if ($result->isValid()) {
                $this->Authentication->logout();
                return $this->redirect(['controller' => 'Users', 'action' => 'login']);
            }
        }

    }

用户表

<?php

class UsersTable extends Table
{
    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config): void
    {
        parent::initialize($config);

        $this->setTable('users');
        $this->setDisplayField('id');
        $this->setPrimaryKey('id');
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator): Validator
    {
        return $validator
            ->notEmpty('username', 'A username is required')
            ->email('username')
            ->notEmpty('password', 'A password is required')
            ->notEmpty('role', 'A role is required')
            ->add('role', 'inList', [
                'rule' => ['inList', ['admin', 'author']],
                'message' => 'Please enter a valid role'
            ]);
    }

    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules): RulesChecker
    {
        $rules->add($rules->isUnique(['username']), ['errorField' => 'username']);

        return $rules;
    }
}

使用者

class User extends Entity
{
    // Make all fields mass assignable for now.
    protected $_accessible = ['*' => true];

    // ...

    protected function _setPassword($password)
    {
        if (strlen($password) > 0) {
            return (new DefaultPasswordHasher)->hash($password);
        }
    }
    /**
     * Fields that are excluded from JSON versions of the entity.
     *
     * @var array
     */
    protected $_hidden = [
        'password',
    ];
}
ee7vknir

ee7vknir1#

显然,您没有正确地或以任何方式实现身份验证中间件。
以下是完整的操作说明:


相关问题