【问题标题】:Authentication in cakePHP 2.4cakePHP 2.4 中的身份验证
【发布时间】:2014-05-19 11:59:33
【问题描述】:

按照我创建的文档,我无法在 cakePHP 2.4 中登录用户。它说 成功登录 并且还重定向到目标 URL(users/loggedin 操作),但 $this->Auth->username$this->Auth->password 保持空白,没有创建会话。当尝试使用错误的凭据登录时,它也会被重定向。还告诉我登录成功。我现在一无所知。

AppController.php

class AppController extends Controller {
  public $components = array(
        'Session',
        'DebugKit.Toolbar',
        'Auth' => array(
            'loginAction' => array('controller' => 'users', 'action' => 'login', 'plugin' => false),
            'loginRedirect' => array('controller' => 'users', 'action' => 'loggedin'),
            'logoutRedirect' => array('controller' => 'users', 'action' => 'loggedout'),
            'authError' => 'UNABLE TO LOG IN.'
        )
    );

    public function beforeFilter(){
        $this->Auth->userModel = 'User';
        $this->Auth->fields = array('username' => 'username', 'password' => 'password');
        $this->Auth->allow('index','display','login','show_reg_form', 'view','signup');
    }

}

UsersController.php

我只贴登录功能:

    public function login() {


    if ($this->request->is('post')) {

            if($this->Auth->login(/*$this->request->data*/)){
                $this->Session->setFlash(__('Successfully logged in.')); 
                return $this->redirect($this->Auth->redirectUrl()); 
            } else {
                $this->Session->setFlash(__('Invalid username or password, try again'));
            }
        }

    }

login.ctp

相关部分是:

<div class="container-login users form">
                        <?php echo $this->Form->create('User');
                             echo $this->Form->input('username');
                             echo $this->Form->input('password');
                            echo $this->Form->end(__('Login',true));?>

                    </div>

【问题讨论】:

  • __('Login',true) 应该是 __('Login')。这不再是 cake1.3。
  • 您是否将参数传递给 $this->Auth->login?或者你只是在这里评论了他们?
  • 我没有通过它们,因为当我上次取消注释该部分时,我可以使用任何东西登录。我阅读了文档,它应该可以在没有指定参数的情况下工作。
  • 所以使用你提到的参数,它也会为不存在的用户创建一个会话。
  • 我从来没有提到你应该使用参数。实际上,如果您手动登录,您只会使用参数。

标签: php cakephp cakephp-2.4


【解决方案1】:

登录的用户详细信息应如下所示:

$id          = $this->Auth->user('id');
$username    = $this->Auth->user('username');
...
$other_field = $this->Auth->user('other_field');

Auth 对象中没有字段用户名。

您无法使用 user 方法从 Auth 读取密码字段。

如果要获取密码,则需要从数据库中查询用户。但是您应该将密码散列并单独保留。

编辑: 由于您可以访问“登录”页面,这意味着创建了一个用户会话。您只需要更改访问用户数据的方式。

编辑2: 通过在 Auth 中添加以下行来指定身份验证处理程序:

'Auth' => array(
    ....
    'authenticate' => array('Form') 
)

编辑3: 再次检查存储密码的数据库字段是否为长度为 40 的 VARCHAR。

【讨论】:

  • 这就是问题所在。当尝试使用错误的凭据登录时,它也会被重定向。并且还告诉我登录成功。
  • 我应该猜到了吧?
猜你喜欢
  • 2014-05-11
  • 2011-03-11
  • 2011-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-24
相关资源
最近更新 更多