默认登录在 cakephp 3.2 中不起作用

Default login is not working in cakephp 3.2

我是 cakephp 3 的新手。2.I 使用 cakephp 代码完成登录, 但是需要创建一个默认登录名(意味着不管数据库中存在的登录 ID 和密码是什么,如果我提供默认登录用户名和密码,登录就可以了。)

代码如下

if(($data['email'] == 'admin@gmail.com') && ($data['password'] == '123456')) { 
    $getMasterLogin = $this->Users->find('all')->where(['Users.type' => 1])->first();
    $user = $this->Auth->identify($getMasterLogin);//pj($user);exit;//alaway returnig false
    //$this->Auth->setUser($user);
    return $this->redirect(['action' => 'index']);
}

这里 $this->Auth->identify 总是返回 false。 我已经按照烹饪书完成了登录部分,但我无法执行此默认设置 login.I 非常需要它。 对不起 。 请建议我。

虽然这是一个非常非常糟糕的主意,并且会使您的系统面临各种黑客攻击。您应该在数据库中创建一个管理员用户。

但是,您实际问题的答案是:

identify() 不接受任何参数,而是根据当前请求数据进行身份验证,因此如果您的数据库中没有匹配该数据的用户,它将始终 return 错误。您要做的只是按如下方式调用 setUser($getMasterLogin)

if(($data['email'] == 'admin@gmail.com') && ($data['password'] == '123456')) { 
    $getMasterLogin = $this->Users->find('all')->where(['Users.type' => 1])->first();
    $this->Auth->setUser($getMasterLogin);
    return $this->redirect(['action' => 'index']);
}

您可以通过 http://cakesf.herokuapp.com/ 加入 CakePHP 支持 slack 频道,您将能够实时提问并获得答案!