gpt4 book ai didi

php - CakePHP 3 下的摘要认证

转载 作者:搜寻专家 更新时间:2023-10-31 21:24:41 24 4
gpt4 key购买 nike

我正在尝试使用 Cakephp 3.1 下的身份验证组件创建摘要身份验证,但我遇到了问题。我正在使用下面的代码,在上一个弹出窗口中输入正确的用户名和密码后,我会立即弹出 HTTP-Authentication 弹出窗口。然后,如果我按取消,我会得到这个:Cake\Auth\BasicAuthenticate->unauthenticated。

有人可以告诉我我做错了什么吗?

AppController.php

$this->loadComponent('Auth', [
'authorize' => 'Controller',
'loginRedirect' => [
'controller' => 'Users',
'action' => 'index'
],
'authenticate' => [
'Digest' => [
'fields' => ['username' => 'username', 'password' => 'digest_hash'],
'userModel' => 'Users',
],
],
'loginAction' => [
'controller' => 'Users',
'action' => 'login',
],
'storage' => 'Memory',
'unauthorizedRedirect' => false
]);

用户表.php

public function beforeSave(Event $event)
{
$entity = $event->data['entity'];

// Make a password for digest auth.
$entity->digest_hash = DigestAuthenticate::password(
$entity->username,
$entity->plain_password,
env('SCRIPT_NAME')
);
return true;
}

在客户端

    public function digest(){
$http = new Client();
$response = $http->get('http://localhost/project/api/v1/users/view/22', [], [
'auth' => [
'type' => 'digest',
'username' => 'Digest',
'password' => 'my_password',
]
]);

当我检查 Debug-kit 环境时,我有这个:

PHP_AUTH_DIGEST     username="Digest", realm="localhost", nonce="57ac3609a5b79", uri="/project/api/v1/users/view/22", response="af0e1fe455aa7f1475df715ef5231b56", opaque="421aa90e079fa326b6494f812ad13e79", qop=auth, nc=00000001, cnonce="0bb461453700ebc1"

最佳答案

这可能为时已晚,但对某些人仍然有帮助!

Well using $this->Auth->unauthorizedRedirect = false,. causes AuthComponent to throw a ForbiddenException exception instead of redirecting to another page unless you submit valid username and password.

正确获取注册:

显然,正确注册/添加用户的摘要密码对于使摘要身份验证成为可能很重要。

documentation 中所述我们可以通过在 UsersTable.php 中添加以下代码来添加摘要哈希密码:

  public function beforeSave(Event $event)
{
$entity = $event->data['entity'];

// Make a password for digest auth.
$entity->digest_hash = DigestAuthenticate::password(
$entity->username,
$entity->plain_password,
env('SERVER_NAME')
);
return true;
}

但我们应该小心上面提到的变量/术语:

1. $entity->digest_hash (this should be equivalent to the field you have made to
save password, eg. password_hash)

2. $entity->username (this should be equivalent to the field you have made to
save username, eg. email)

3. $entity->plain_password (again this should be equivalent to the field you have made to
save password, eg. password_hash)

4. env('SERVER_NAME') (this is third parameter for making digest password,
"SERVER_NAME" is default value and we can left it this way.)

作为结论,如果我们有一个电子邮件(用于用户名)和 password_hash(用于密码),那么上面的函数将是:

 public function beforeSave(Event $event)
{
$entity = $event->data['entity'];

// Make a password for digest auth.
$entity->password_hash= DigestAuthenticate::password(
$entity->email,
$entity->password_hash,
env('SERVER_NAME')
);
return true;
}

之所以我关注上面的事情是因为他们有可能犯错误。

关于php - CakePHP 3 下的摘要认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38891650/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com