【发布时间】:2020-03-27 15:23:09
【问题描述】:
这是我第一次尝试在 Cake 3.8 中使用 Authentication 插件。我遵循了https://book.cakephp.org/authentication/1/en/index.html 中列出的示例。
Angular / Typescript 向 Cake 发送凭据的代码:
/**
* Login
*/
public login( username : string,
password : string,
captchaResponse : string ) {
return this.http.post<any>( 'http://api.mydomain.localhost/users/token.json',
{
username,
password,
captchaResponse
},
{
headers : new HttpHeaders()
.set( 'X-Requested-With', 'XMLHttpRequest' )
}
).pipe( map( response => {
// Store user details and jwt token in local storage
// to keep user logged in between page refreshes
this.user = response.data;
this.setUser( this.user.profile );
this.setToken( this.user.token );
return this.user;
} ) );
}
在Application.php
public function bootstrap() {
// Authentication
$this->addPlugin( 'Authentication' );
}
/**
* Returns a service provider instance.
*
* @param \Psr\Http\Message\ServerRequestInterface $request Request
* @param \Psr\Http\Message\ResponseInterface $response Response
* @return \Authentication\AuthenticationServiceInterface
*/
public function getAuthenticationService( ServerRequestInterface $request, ResponseInterface $response ) {
$service = new AuthenticationService();
// Load identifiers
$service->loadIdentifier( 'Authentication.Password', [
'fields' => [
'username' => 'username',
'password' => 'password',
],
'resolver' => [
'className' => 'Authentication.Orm',
'finder' => 'active',
'userModel' => 'Users',
],
] );
// Load the authenticators
$service->loadAuthenticator( 'Authentication.Form', [
'fields' => [
'username' => 'username',
'password' => 'password',
],
'loginUrl' => '/users/token.json'
] );
$service->loadIdentifier( 'Authentication.JwtSubject' );
// Configure the service. (see below for more details)
return $service;
}
public function middleware( $middlewareQueue ) {
$middlewareQueue
// Authentication Middleware
->add( new AuthenticationMiddleware( $this ) );
return $middlewareQueue;
}
在AppController.php
// Authentication Component
$this->loadComponent( 'Authentication.Authentication' );
我正在从 Angular 7 应用程序向 Cake(充当 REST API)发送凭据。凭据是通过嵌入在请求正文中的发布请求发送的。当我通过$result->isValid() 进行检查时,我不断收到身份验证结果无效。
在UsersController.php中,当我尝试追踪错误时:
$this->log( $this->request->getData(), 'debug' );
$result = $result = $this->Authentication->getResult();
$this->log( $result, 'debug' );
我得到以下输出:
2019-12-03 07:35:30 Debug: Array
(
[username] => myuser
[password] => mypassword
[captchaResponse] =>
)
2019-12-03 07:35:30 Debug: Authentication\Authenticator\Result Object
(
[_status:protected] => FAILURE_CREDENTIALS_MISSING
[_data:protected] =>
[_errors:protected] => Array
(
[0] => Login credentials not found
)
)
我只是不明白为什么 Cake 无法检测到帖子数据中是否存在凭据。其他人遇到同样的问题并有解决方案吗?
谢谢。
【问题讨论】:
-
您发送什么类型的数据?表格数据? JSON? ...
-
表单数据 - 作为键值对包含在帖子正文中。我已经编辑了上面的帖子以包含登录的角度代码。
-
尝试在
FormAuthenticator::_getData中调试。当此函数返回null时,您看到的错误似乎会发生,这可能会以两种方式之一发生。应该很容易缩小范围。 -
谢谢 Greg - 我确实做到了,发现 _getData() 确实在解析请求中的空值。在深入挖掘之后,我相信这是由于 Angular 发送数据的方式 - 当直接作为对象包含在帖子正文中时。相反,需要将数据(键值对)附加到 FormData 对象,并将其用作帖子的正文。添加在答案中。
-
看起来您实际上是在发送 JSON,而不是表单数据。我对 Angular 并不太熟悉,但如果未另行指定,HTTP 客户端默认将原始对象作为 JSON 发送。
标签: authentication plugins cakephp-3.0 credentials missing-data