CakePHP 3 删除 WWW-authenticate
CakePHP 3 remove WWW-authenticate
我正在使用 CakePHP 作为 API 应用程序的 REST API。
每个请求在继续之前都会经过身份验证和授权。
问题是,在登录时,如果凭据错误,Cake returns 401 并且浏览器会在弹出窗口中显示其自己的服务器日志。
我相信有一种方法可以通过取消设置 WWW-authenticate header
来阻止它,但我需要知道如何做。有人可以解释如何取消设置 header 吗?
正在 \Cake\Auth\BasicAuthenticate
身份验证适配器中设置 headers。
https://github.com/cakephp/cakephp/blob/3.0.11/src/Auth/BasicAuthenticate.php#L85-L110
它是硬编码的,因此如果您想更改此行为,则必须创建一个 custom/extended 身份验证适配器并覆盖此行为。
这是一个简单的例子:
src/Auth/MyCustomBasicAuthenticate.php
namespace App\Auth;
use Cake\Auth\BasicAuthenticate;
use Cake\Network\Exception\UnauthorizedException;
use Cake\Network\Request;
use Cake\Network\Response;
class MyCustomBasicAuthenticate extends BasicAuthenticate
{
public function unauthenticated(Request $request, Response $response)
{
throw new UnauthorizedException();
}
}
控制器
$this->loadComponent('Auth', [
'authenticate' => [
'MyCustomBasic'
]
]);
另见
我正在使用 CakePHP 作为 API 应用程序的 REST API。
每个请求在继续之前都会经过身份验证和授权。
问题是,在登录时,如果凭据错误,Cake returns 401 并且浏览器会在弹出窗口中显示其自己的服务器日志。
我相信有一种方法可以通过取消设置 WWW-authenticate header
来阻止它,但我需要知道如何做。有人可以解释如何取消设置 header 吗?
正在 \Cake\Auth\BasicAuthenticate
身份验证适配器中设置 headers。
https://github.com/cakephp/cakephp/blob/3.0.11/src/Auth/BasicAuthenticate.php#L85-L110
它是硬编码的,因此如果您想更改此行为,则必须创建一个 custom/extended 身份验证适配器并覆盖此行为。
这是一个简单的例子:
src/Auth/MyCustomBasicAuthenticate.php
namespace App\Auth;
use Cake\Auth\BasicAuthenticate;
use Cake\Network\Exception\UnauthorizedException;
use Cake\Network\Request;
use Cake\Network\Response;
class MyCustomBasicAuthenticate extends BasicAuthenticate
{
public function unauthenticated(Request $request, Response $response)
{
throw new UnauthorizedException();
}
}
控制器
$this->loadComponent('Auth', [
'authenticate' => [
'MyCustomBasic'
]
]);
另见