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

Posted

技术标签:

【中文标题】我无法在 Cakephp 4 中进行身份验证,错误 500【英文标题】:i can't authenticated in Cakephp 4 , error 500 【发布时间】:2021-12-06 18:43:44 【问题描述】:

我正在尝试通过用户俱乐部进行身份验证,但收到错误 500 内部服务器,我不明白为什么,仅在使用身份验证时才会发生。也许是配置错误?

firefox 控制台错误 - GET http://localhost/learnwithus/users 500 内部服务器错误

错误日志

    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',
    ];

【问题讨论】:

查看日志,在配置应用程序中将调试设置为 true,并将错误日志发布到您的问题中。 你的意思是在 boostrap CLI 中的盐水? 不行,你需要查看app/logs/error.log 我已经使用 error.log 中的堆栈跟踪编辑了我的帖子。 【参考方案1】:

显然您没有正确或以任何方式实现身份验证中间件。

以下是有关如何操作的完整说明:

https://book.cakephp.org/authentication/2/en/index.html

【讨论】:

以上是关于我无法在 Cakephp 4 中进行身份验证,错误 500的主要内容,如果未能解决你的问题,请参考以下文章

CakePHP身份验证插件身份关联

具有两个模型会话的 Cakephp 身份验证组件

cakephp : 使用 $validate 数组验证登录表单

使用 CakePHP 'Acl' 组件

在 cakephp 中编辑用户配置文件后如何更改身份验证用户名

Cakephp身份验证重置/重新加载