Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony 3.3,身份验证,bcrypt,错误凭据_Symfony_Security_Bcrypt - Fatal编程技术网

Symfony 3.3,身份验证,bcrypt,错误凭据

Symfony 3.3,身份验证,bcrypt,错误凭据,symfony,security,bcrypt,Symfony,Security,Bcrypt,我正在开发一个Symfony 3.3 web应用程序,其中包括一些捆绑包 我试图定义一种在数据库中注册管理员用户的方法,如下所示。我可以在数据库中创建该用户,但之后我无法以该用户的身份登录:我总是获得错误的凭据。虽然我知道事情可以安排得更好,但我想了解我没有看到的东西 因此,我有一个AdminBundle,其中我定义了一个用户类: <?php namespace myApp\AdminBundle\Entity; use FOS\UserBundle\Model\User as Ba

我正在开发一个Symfony 3.3 web应用程序,其中包括一些捆绑包

我试图定义一种在数据库中注册管理员用户的方法,如下所示。我可以在数据库中创建该用户,但之后我无法以该用户的身份登录:我总是获得错误的凭据。虽然我知道事情可以安排得更好,但我想了解我没有看到的东西

因此,我有一个AdminBundle,其中我定义了一个用户类:

<?php

namespace myApp\AdminBundle\Entity;


use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;


/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        parent::__construct();

    }
}
?>
我的security.yml有:

security:
    encoders:
        myApp\AdminBundle\Entity\User:
            algorithm: bcrypt
            cost: 10
            iterations: 1

   role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

   providers:
       our_db_provider:
            entity:
               class: myApp\AdminBundle\Entity\User
               property: username
....
   firewalls:
       admin_area:
           pattern:   ^/myApp/Admin
           anonymous: ~
           form_login:
              login_path: /myApp/Admin/login
              check_path: /myApp/Admin/login_check
           logout:
              path:   /myApp/Admin/logout
              target: /myApp/Admin/admin
           http_basic:
              realm: "Admin reserved"

           context: primary_auth
而我的config.yml有(实际上我不确定是否需要以下内容):

如果我呼叫
http://localhost/myApp/Admin/register/admin
,将以下记录插入表fos\U user中:

--
-- Dumping data for table `fos_user`
--

INSERT INTO `fos_user` (`id`, `username`, `username_canonical`, `email`, `email_canonical`, `enabled`, `salt`, `password`, `last_login`, `confirmation_token`, `password_requested_at`, `roles`) VALUES
(1, 'admin', 'admin', 'aaa@aaa.aaa', 'aaa@aaa.aaa', 1, NULL, '$2y........yq', NULL, NULL, NULL, 'a:1:{i:0;s:16:\"ROLE_SUPER_ADMIN\";}');
但是,我无法登录。日志说:

[2017-08-…]security.INFO:身份验证请求失败。{“异常”:“[对象](Symfony\Component\Security\Core\exception\badCredentialexception(代码:0):错误的凭据。位于/…/vendor/Symfony/Symfony/src/Symfony/Component/Security/Core/Authentication/Provider/UserAuthenticationProvider.php:91,Symfony\Component\Security\Core\exception\badCredentialexception\badException(代码:0):提供的密码无效。位于/…../vendor/symfony/symfony/src/symfony/Component/Security/Core/Authentication/Provider/DaoAuthenticationProvider.php:67)“}[]

谢谢,谢谢你的帮助


另外,我的应用程序运行得很好,如果我在内存中使用
安全提供程序

就不需要手动对密码进行哈希运算。用户管理器在更新时对其进行哈希处理。只需设置普通密码

$user = new User();
...
$user->setPlainPassword('pw');
...
$userManager->updateUser($user);
如果要手动对其进行散列,请修复
encodePassword
call。它接受两个参数:普通密码和salt

$encoded = $encoder->encodePassword('pw', $user->getSalt());

你试过sha512编码器吗?是的,我应该看到的。我不知道
setPlainPassword
。Inded它也可以与
encodePassword
配合使用,即使
$user->getSalt()=NULL
。完美的谢谢。
$user = new User();
...
$user->setPlainPassword('pw');
...
$userManager->updateUser($user);
$encoded = $encoder->encodePassword('pw', $user->getSalt());