我经常使用AuthComponent,但它不是AuthenticationMiddleware的新手。除了用户名字段是用户名而不是电子邮件,我几乎完全遵循https://book.cakephp.org/authentication/2/en/index.html。但是,当我尝试获取需要身份验证的页面时,Cake在AuthenticationComponent :: doIdentityCheck()中抛出UnauthenticatedException。这可以理解,因为Cake应该已经将我重定向到了/ users / login,但没有。我尝试过
$service->loadAuthenticator('Authentication.Form', [
'loginUrl' => '/users/login' // case 1
// or case 2 'loginUrl' => \Cake\Routing\Router::url('/users/login'),
// or case 3 omit this to use the default
在上述所有情况下,Cakes都会在消息中抛出UnauthenticatedException
未找到身份。您可以通过将requireIdentity配置为false来跳过此检查。
也http://localhost:8765/users/login带我到正确的页面,如果我允许未经身份验证的用户,也可以按http://localhost:8765/users/看到用户列表:
// UsersController.php
public function initialize() : void {
parent::initialize();
$this->Authentication->allowUnauthenticated(['index', 'login']);
}
我的环境:CakePHP 4.0,身份验证插件2.0。有没有办法验证AuthenticationMiddleware是否确实已设置并添加到中间件队列中?
答案 0 :(得分:1)
如果中间件未运行,您将收到另一个错误,该错误会抱怨请求对象上缺少
>>> App\{MODEL_CLASS}::truncate()
属性。
您需要在服务对象上配置$pattern = "/([\p{L}]+)/u";
$string = "你好,世界!Привет мир! !مرحبا بالعالم"; // <- input
if (preg_match_all($pattern, $string, $match)) { // <- this checks for matches
// Characters found, do something here
}else{
// No Charcters matching, do something else
}
选项,以便针对未经身份验证的请求发出重定向,如果不配置该选项,则会收到异常。此外,如果希望成功登录后能够重定向用户,则可能需要设置authentication
选项(该查询字符串参数将保存最初访问的URL)。
unauthenticatedRedirect
文档似乎似乎没有,只有mentioned in the migration notes。您可能要为此over at GitHub打开一个问题。快速入门指南示例已更新,其中包括重定向配置。