我正在开发一个Symfony 3.3 web应用程序,其中包含几个软件包。Symfony 3.3,认证,bcrypt,错误凭证
我试图定义一种方法来在数据库中注册一个管理员用户,如下所示。我能够在数据库中创建用户,但之后我无法以该用户身份登录:我总是收到错误的凭据。虽然我知道可以以更好的方式安排事情,但我想了解我看不到的东西。
所以我有一个AdminBundle,其中我已经定义了一个User类:
<?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();
}
}
?>
和registerAction
/**
* register main user/admin
*
* @Route("/register/{nameuser}", name="Jus_Admin_register")
* @Method("GET")
* @Template()
*/
public function registerAction($nameuser)
{
$user = new User();
$user->setUsername($nameuser);
$user->setEmail('[email protected]');
$plainPassword = 'pw';
$encoder = $this->get('security.encoder_factory')->getEncoder($user);
$encoded = $encoder->encodePassword($user, $plainPassword);
$user->setPassword($encoded);
$user->setEnabled(TRUE);
$user->setSuperAdmin(TRUE);
$userManager = $this->container->get('fos_user.user_manager');
$userManager->updateUser($user);
return $this->render('myAppAdminBundle:Admin:login.html.twig');
}
我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(实际上我不确定我是否需要以下内容):
fos_user:
db_driver: orm # other valid values are 'mongodb' and 'couchdb'
firewall_name: admin_area
user_class: myApp\AdminBundle\Entity\User\
from_email:
address: "%mailer_user%"
sender_name: "%mailer_user%"
如果我打电话http://localhost/myApp/Admin/register/admin
,下面的记录被插入到表fos_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', '[email protected]', '[email protected]', 1, NULL, '$2y........yq', NULL, NULL, NULL, 'a:1:{i:0;s:16:\"ROLE_SUPER_ADMIN\";}');
但是,我无法登录。日志说:
[2017-08- .. ....] security.INFO:身份验证请求失败。 {“exception”:“[object](Symfony \ Component \ Security \ Core \ Exception \ BadCredentialsException(code:0):Bad credentials。at /..../vendor/symfony/symfony/src/Symfony/Component/Security /Core/Authentication/Provider/UserAuthenticationProvider.php:91,Symfony \ Component \ Security \ Core \ Exception \ BadCredentialsException(代码:0):提供的密码无效。at /...../vendor/symfony/symfony/ SRC/Symfony的/分量/安全/核心/认证/供应商/ DaoAuthenticationProvider.php:67)“} []
感谢,我感谢您的帮助
PS。我的应用程序工作得很好,如果我使用in_memory
安全提供程序
你尝试'sha512'编码器? –