CakePHP 2.1 Auth->login() 不起作用,但添加用户可以

2024-01-10

我在 stackoverflow 上搜索了很多帖子来寻找答案,也许我只是忽略了一些东西,但我似乎无法让 $this->Auth->login() 工作。我尝试了其他帖子中的许多不同建议。在描述我尝试过的其他方法时,我会尽可能全面。

我确实已经添加了一个用户。 MD5 散列工作正常。我对密码进行了哈希处理,然后使用 Miracle Salad md5 检查了它http://www.miraclesalad.com/webtools/md5.php http://www.miraclesalad.com/webtools/md5.php

我不使用盐进行散列。我使用的是不加盐的 MD5。

我使用的数据库是Postgresql 9.0。我知道 CakePhp 的某些魔力并不适用于所有数据库(至少有人告诉我)。

应用程序/配置/core.php

Configure::write('Security.level', 'medium');

/**
* A random string used in security hashing methods.
*/

    Configure::write('Security.salt', '');

我使用 Auth->fields 将密码映射到数据库中的 user_password 并将用户名映射到 user_name 。 user_password 和 user_name 是 core_users 表中的列。我也在 beforeFilter() 方法中使用了。

$this->Auth->fields = array('username' => 'user_name', 'password' => 'user_password');

应用程序/控制器/AppController.php

class AppController extends Controller {
public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'pages', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home'),
        'loginAction' => array('admin' => false, 'controller' => 'CoreUsers', 'action' => 'login'),
        /*'fields' => array('password' => 'user_password', 'username' => 'user_name'),*/
        'userModel' => 'CoreUser'
    )
);

public function beforeFilter() {
    Security::setHash('md5');
    $this->Auth->allow('login');
    //debug($this->Auth);
} 
}

我留下了调试,以便您可以看到它们的处理顺序,并且我将向您展示它们是如何打印的。

应用程序/控制器/CoreUsersController.php

    public function login() {
    Security::setHash('md5');
    //debug($this->Auth);

    if ($this->request->is('post')) {
        debug(Security::hash($this->Auth->request->data['CoreUser']['user_password']));
        debug($this->Auth);
        debug(Configure::version());
        debug($this->Auth->request->data['CoreUser']['user_password']);
        debug($this->Auth->request->data['CoreUser']['user_name']);
        if ($this->Auth->login()) {
            debug($this->Auth->request->data['CoreUser']['user_password']);
            $this->redirect($this->Auth->redirect());

        } else {
            debug($this->Auth->request->data['CoreUser']['user_password']);
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }
}
public function logout() {
    $this->redirect($this->Auth->logout());
}

应用程序/模型/CoreUser.php

 App::uses('AuthComponent', 'Controller/Component');
class CoreUser extends AppModel{
public $primaryKey = 'user_id';
public $sequence = 'core_user_id_seq';
public $name = 'CoreUser';
public $validate = array(
    'user_name' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'User name is required'
        )
    ),
    'user_password' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'Password is required'
        )
    ),
    'privilege_id' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'Privilege ID is required'
        ),
        'legalValues' => array(
             'rule' => array('between',1,4),
            'message' => 'Privilege must be between 1 and 4'
        )
    ),
    'user_initial' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'User initials is required'
        )
    ),
    'email' => array(
        'rule' => array('email',true),
        'message' => 'Email must have an \'@\' symbol and a domain e.g. .com' 
    )
);

public function beforeSave() {
    Security::setHash('md5');
    if (isset($this->data[$this->alias]['user_password'])) {
        $this->data[$this->alias]['user_password'] = AuthComponent::password($this->data[$this->alias]['user_password']);
    }
    return true;
}
}

应用程序/视图/CoreUsers/login.ctp

<h3>Login</h3>

<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('CoreUser');?>
<fieldset>
    <legend><?php echo __('Please enter your username and password'); ?></legend>
<?php
    echo $this->Form->input('user_name');
    echo $this->Form->input('user_password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login'));?>
</div>

调试输出

所有这些都来自 CoreUsersController,并按处理顺序进行。

哈希密码。这与我添加用户时数据库中的内容相同。

'098f6bcd4621d373cade4e832627b4f6'

验证对象

object(AuthComponent) {
components => array(
    (int) 0 => 'Session',
    (int) 1 => 'RequestHandler'
)
authenticate => array(
    (int) 0 => 'Form'
)
authorize => false
ajaxLogin => null
flash => array(
    'element' => 'default',
    'key' => 'auth',
    'params' => array()
)
loginAction => array(
    'admin' => false,
    'controller' => 'CoreUsers',
    'action' => 'login'
)
loginRedirect => array(
    'controller' => 'pages',
    'action' => 'index'
)
logoutRedirect => array(
    'controller' => 'pages',
    'action' => 'display',
    (int) 0 => 'home'
)
authError => 'You are not authorized to access that location.'
allowedActions => array(
    (int) 0 => 'login'
)
request => object(CakeRequest) {
    params => array(
        'plugin' => null,
        'controller' => 'CoreUsers',
        'action' => 'login',
        'named' => array(),
        'pass' => array()
    )
    data => array(
        'CoreUser' => array(
            'user_name' => 'testy5',
            'user_password' => 'test'
        )
    )
    query => array()
    url => 'CoreUsers/login'
    base => '/cpm_v2_dev'
    webroot => '/cpm_v2_dev/'
    here => '/cpm_v2_dev/CoreUsers/login'
}
response => object(CakeResponse) {

}
settings => array(
    'loginRedirect' => array(
        'controller' => 'pages',
        'action' => 'index'
    ),
    'logoutRedirect' => array(
        'controller' => 'pages',
        'action' => 'display',
        (int) 0 => 'home'
    ),
    'loginAction' => array(
        'admin' => false,
        'controller' => 'CoreUsers',
        'action' => 'login'
    ),
    'userModel' => 'CoreUser'
)
userModel => 'CoreUser'
  }

CakePHP 版本

'2.1.0'

调用login()之前的密码

'test'

调用login()之前的用户名

 'testy5'

调用login()后的密码

 'test'

以下是我在其他 stackoverflow 帖子中读到过的、我尝试过的内容的快速列表。如果您需要我详细说明,请告诉我。

1)我将用户名和密码映射到数据库中的字段。这是字段注释的地方。我也尝试在 beforeFilter() 方法中执行此操作。使用:

$this->Auth->fields = array('username' => 'user_name', 'password' => 'user_password');

在登录视图中,表单是这样创建的:

$this->Form->input('username');
$this->Form->input('password');

2)我尝试在登录之前手动散列密码,如下所示:

 $this->Auth->request->data['CoreUser']['password'] = Security::hash($this->Auth->request->data['CoreUser']['password'])
   if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
   }

EDIT0

3)我只是尝试按照建议这样做CakePHP 2.0 身份验证登录不起作用 https://stackoverflow.com/questions/10014928/cakephp-2-0-auth-login-not-working

我的 AuthComponent 现在看起来像这样:

     public $components = array(
    'Session',
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'userModel' => 'CoreUser',
                'fields' => array(
                    'username' => 'user_name',
                    'password' => 'user_password'
                )
            )
        ),
        'loginRedirect' => array('controller' => 'pages', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home'),
        'loginAction' => array('admin' => false, 'controller' => 'CoreUsers', 'action' => 'login')
    )
);

如果我不够详细,或者我犯了一个错误,我深表歉意。我已经为此工作了几天,这真的让我筋疲力尽。我很感激我可能收到的任何帮助。谢谢!


我最终解决这个问题的方法是完全按照 CakePHP 的教程进行操作。我也升级到2.1.2了。我运行的是2.1.0。

http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html

然后我慢慢的添加了我需要的配置。有关我引用的 Auth 组件的信息:

http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html

事实上,我的问题是故障排除不力。我会做一堆改变,而不是一次一个。我有两种不同的方式查看我的login.ctp

 $this->Form->input('username');
 $this->Form->input('password');

现在看起来像这样:

 echo $this->Form->input('user_name');
 echo $this->Form->Label('Password');
 echo $this->Form->password('user_password');

第二个版本有效。

编辑0: 这个非常重要。如果不调用 AppController 父级,登录将无法工作。

class CoreUsersController extends AppController{
    public $helpers = array('Html','Form');

    public function beforeFilter() {
         parent::beforeFilter();
    }

Auth 组件的修订版有效:

public $components = array(
    'Session',
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'userModel' => 'CoreUser',
                'fields' => array(
                    'username' => 'user_name',
                    'password' => 'user_password'
                )
            )
        ),
        'loginRedirect' => array('controller' => 'pages', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home'),
        'loginAction' => array('admin' => false, 'controller' => 'CoreUsers', 'action' => 'login')
    )
);

我的盐仍然是一个空字符串:

Configure::write('Security.salt', '');

只需在一处设置 md5 哈希值。模型中的 beforeSave() 中不需要它:

public function beforeFilter() {
    Security::setHash('md5');
    $this->Auth->allow('login','add');  
} 

之前保存():

public function beforeSave() {
    if (isset($this->data[$this->alias]['user_password'])) {
        $this->data[$this->alias]['user_password'] = AuthComponent::password($this->data[$this->alias]['user_password']);
    }
    return true;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

CakePHP 2.1 Auth->login() 不起作用,但添加用户可以 的相关文章

  • Cakephp:将 AppController 抽象到另一个层次,可能吗?

    我想知道是否可以在 AppController 和我的应用程序的其他控制器之间添加另一个抽象控制器 这样我的控制器 例如UsersController 扩展了 SecureController SecureController 扩展了 Ap
  • CakePHP 验证错误消息

    我正在尝试修改 CakePHP 显示错误消息的方式 下面是用于显示错误的通用模板 div class error message Please enter at least 3 characters div 我想像下面这样设置错误的样式
  • CakePHP 3.X 中的自定义 404 页面

    我想为生产环境中出现的所有错误创建一个自定义 404 页面 例如 如果我收到缺少控制器或视图错误 那么它将重定向到http example com 404 html 另外在某些情况下我会故意重定向它http example com 404
  • Godaddy 托管上的 CakePHP 控制台

    我一直在努力让我的 CakePHP 网站在 Godaddy 网格托管 帐户上运行 我的蛋糕应用程序设置是从帐户的子目录托管的 并且可以通过子域访问 我必须调整我的 htaccess 文件才能使其正常工作 现在我需要让 CakePHP 控制台
  • 如何在 CakePHP 中“验证”人名?

    我有一个 PHP 脚本 应该检查 有效 的人名 但最近破解了带有空格的名称 因此我们向验证器添加了空格 除了这样做之外 有没有办法向 CakePHP 的验证器添加黑名单以阻止所有 无效 字符 而不是允许 有效 字符 注意 我 通常 知道如何
  • CakePHP 查找 - 按字符串到整数排序?

    我想使用 CakePHP 从数据库中提取照片数组 按照片标题排序 0 1 2 3 我的查询当前看起来像 ss photos this gt Asset gt find all array conditions gt array kind g
  • 如何配置 nginx 重写规则以使 CakePHP 在 CentOS 上运行?

    大家好 请帮帮我 我正在尝试在运行 Nginx 和 Fact CGI 的 Centos 服务器上设置 cakephp 环境 我已经在服务器上运行了一个 WordPress 站点和一个 phpmyadmin 站点 因此我已经正确配置了 PHP
  • CakePHP PaginationRecallComponent,严格 (2048):PaginationRecallComponent::initialize() 声明

    我尝试插入分页调用组件 http bakery cakephp org articles Zaphod 2012 03 27 paginationrecall for cakephp 2 x http bakery cakephp org
  • 具有“日期之间”的 CakePHP 模型

    我有一个很大的数据集 超过十亿行 数据在数据库中按日期分区 因此 我的查询工具必须在每个查询上指定一个 SQL Between 子句 否则它将必须扫描每个分区 而且 它会在返回之前超时 所以 我的问题是 分区的数据库中的字段是日期 使用 C
  • 动态创建的数据源未传递到 CakePHP 中的关联模型

    我有一个模型 汽车 该汽车有几个关联的模型 让我们考虑其中一个与hasMany关系 轮 在我的 CarsController 中 我使用以下代码动态生成数据源 schemaName this gt Session gt read User
  • 如何防止 CakePHP 中重复表单提交?

    我发现 CakePHP 中的安全组件通过将令牌作为隐藏值添加到表单中来帮助防止 CSRF 我想知道是否有办法防止使用此组件或其他组件 帮助器重复表单提交 在之前的项目中 我使用了保存在会话中的唯一哈希值 该哈希值会在提交时读取并删除 重复提
  • cakephp中的递归是什么意思?

    好吧 我正在关注的教程中有这行代码 但是 它没有为我提供有关递归的明确解释 我是 cakephp 的新手 搜索了这个 递归 我希望有人能为我提供这段代码的外行解释 this gt Author gt recursive 1 谢谢 Googl
  • Netbeans 6.8 中的 CakePHP 帮助程序自动完成

    谁能告诉我如何在 CakePHP 中 启用 CakePHP 的 Helper 自动完成功能 本质上是这样 例如 当我输入 form gt 它给了我一个清单 form的方法和变量 我读过了http bakery cakephp org art
  • 如何在 nginx 反向代理后面安全地检测 CakePHP 中的 SSL?

    CakePHP 我见过的所有版本 检查 SERVER HTTPS 查看请求是否是通过 HTTPS 而不是普通 HTTP 发出的 我使用 nginx 作为负载均衡器 后面是 Apache 应用程序服务器 由于 SSL 连接在负载均衡器处终止
  • 在 CakePHP 中向文章添加评论

    我正在学习 CakePHP 这是我的第一个 MVC 我有一些 最佳实践 问题 这是我对显示新闻文章的看法 h1 h1 p p div class comment style margin left 50px p p div
  • 未找到 CakePHP DebugKit/插件 webroot

    我已经安装了 CakePHP 的调试工具包 但它无法在我的页面上正确加载 它只是页面底部的一堆文本和数组 我的浏览器显示出现 404 错误 debug kit css debug toolbar css debug kit js jquer
  • 如何删除 CakePHP 的尾随计时基准注释

    我已经解决这个可怕的问题几个小时了 尽我所能在谷歌上搜索 但仍然不幸找到解决方案 问题是 CakdPHP 渲染的每个页面都有一个默认的尾随计时基准注释 例如 如何删除最后一行 在此处输入代码 core php or anywhere els
  • 蛋糕控制台 2.2.1:烘焙错误

    运行 MAMP 的 OSX 机器 CakePHP 2 2 1 已正确安装和配置 这意味着当我浏览到 Index php 文件时 所有绿色条都显示出来 我已经完成了博客教程 并且正在开发我的第二个应用程序 其中脚手架已启动并运行 现在我第一次
  • 如何在 CakePHP 3 中将 COUNT(*) 与 find('list') 一起使用?

    在 CakePHP 3 中 我有一个名为 文章 的模型和一个名为 主题 的字段 在尝试检索 100 个最常用文章主题的列表时遇到了障碍 选择以下代码生成的 SQLall可能的字段和not COUNT articles gt find lis
  • 有没有办法让 saveAll() 删除无关的对象?

    我的主机对象有许多与其关联的选项对象 在编辑表单中 用户可以选择 取消 选择选项并保存新的关联集 这是通过对发布的数据使用 saveAll 来实现的 结果是 主机 主 对象已更新 更新先前关联和新关联中包含的选项 关联 对象 并且 将创建未

随机推荐