使用身份验证插件,我无法通过json验证我的凭据。我可以使用Form,Jwt和Basic(进行测试可以正常工作)。返回的错误是“ FAILURE_CREDENTIALS_INVALID”。这是示例代码“为简洁起见”
public function token()
{
$result = $this->Authentication->getResult();
if ($result->isValid()) {
//new jwt token etc
$message = true;
$this->log($result->getStatus());
}else{
$message = false;
$this->log($result->getStatus());
}
$this->set([
'message' => $message,
]);
$this->viewBuilder()->setOption('serialize', ['message']);
}
我的应用程序类具有方法
public function getAuthenticationService(ServerRequestInterface $request) : AuthenticationServiceInterface
{
$service = new AuthenticationService();
$service->setConfig([
'unauthenticatedRedirect' => '/users/login',
'queryParam' => 'redirect',
]);
$fields = [
'username' => 'email',
'password' => 'password'
];
// Load the authenticators, you want session first
$service->loadAuthenticator('Authentication.Session');
$service->loadAuthenticator('Authentication.Form', [
'fields' => $fields,
'loginUrl' => '/users/login'
]);
//Testing jwt
$service->loadAuthenticator('Authentication.Jwt', [
'secretKey' => Security::getSalt()]);
// Load identifiers
$service->loadIdentifier('Authentication.Password', compact('fields'));
$service->loadIdentifier('Authentication.JwtSubject');
return $service;
}
如果我传入jwt,则该应用程序可以很好地处理json,但是以某种方式我无法弄清楚当旧的过期时如何请求一个新的json。
这是我的中间件队列:
//some code
$middlewareQueue
->add(new ErrorHandlerMiddleware(Configure::read('Error')))
->add(new AssetMiddleware([
'cacheTime' => Configure::read('Asset.cacheTime'),
]))
->add(new RoutingMiddleware($this))
->add($csrf)
->add(new BodyParserMiddleware())
->add(new AuthenticationMiddleware($this));
//more code
我不要使用基本
答案 0 :(得分:2)
如评论中所述,如果要在令牌检索端点上使用表单身份验证器进行身份验证,则需要确保在身份验证器loginUrl
选项中包括该端点的URL路径。 / p>
该选项接受一个数组,因此它应该像这样简单:
$service->loadAuthenticator('Authentication.Form', [
'fields' => $fields,
'loginUrl' => [
'/users/login',
'/api/users/token.json',
],
]);
您收到的错误是因为表单身份验证器根本没有应用在令牌端点上,因此身份验证服务将转到下一个身份验证器,即JWT身份验证器,该身份验证器未绑定到特定端点,因此可以在所有端点上运行,并且JWT身份验证器当然希望使用不同的有效负载,它将查找令牌,如果找不到令牌,它将返回错误状态FAILURE_CREDENTIALS_INVALID
。