Symfony FOSUserBundle-登录-凭据无效
我对FosUserBundle有意见 我偶然遵循了这个教程 最后它说你现在可以登录到“/app.com/app_dev.php/login!” 这是 我的路由文件:Symfony FOSUserBundle-登录-凭据无效,symfony,login,fosuserbundle,credentials,Symfony,Login,Fosuserbundle,Credentials,我对FosUserBundle有意见 我偶然遵循了这个教程 最后它说你现在可以登录到“/app.com/app_dev.php/login!” 这是 我的路由文件: app: resource: "@AppBundle/Controller/" type: annotation # easy_admin_bundle: # resource: "@EasyAdminBundle/Controller/" # type: annotati
app:
resource: "@AppBundle/Controller/"
type: annotation
# easy_admin_bundle:
# resource: "@EasyAdminBundle/Controller/"
# type: annotation
# prefix: /admin
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
security:
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
providers:
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_token_generator: security.csrf.token_manager
# if you are using Symfony < 2.8, use the following config instead:
# csrf_provider: form.csrf_provider
logout: true
anonymous: true
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: ROLE_ADMIN }
我的安全文件:
app:
resource: "@AppBundle/Controller/"
type: annotation
# easy_admin_bundle:
# resource: "@EasyAdminBundle/Controller/"
# type: annotation
# prefix: /admin
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
security:
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
providers:
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_token_generator: security.csrf.token_manager
# if you are using Symfony < 2.8, use the following config instead:
# csrf_provider: form.csrf_provider
logout: true
anonymous: true
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: ROLE_ADMIN }
安全性:
编码器:
FOS\UserBundle\Model\UserInterface:bcrypt
角色层次结构:
角色\管理员:角色\用户
角色超级管理员:角色管理员
供应商:
fos_用户包:
id:fos\u user.user\u provider.username
防火墙:
主要内容:
模式:^/
表格(u)登入:
提供商:fos_用户包
csrf_令牌_生成器:security.csrf.token_管理器
#如果您使用的是Symfony<2.8,请改用以下配置:
#csrf\u提供程序:form.csrf\u提供程序
注销:正确
匿名:是的
访问控制:
-{path:^/login$,角色:已通过身份验证\u匿名}
-{path:^/register,role:IS_AUTHENTICATED_ANONYMOUSLY}
-{路径:^/正在重置,角色:是否以匿名方式进行身份验证}
-{path:^/admin/,role:role_admin}
您必须使用FOS\UserBundle\Model\User::setPlainPassword()方法,而不是setPassword()手动将用户详细信息放入数据库可能是问题所在。除非经过正确的过程,否则最终将得到一个未加密的密码,因此当您尝试使用bcrypt(如安全文件中所述)进行身份验证时,密码将不匹配
你有两个选择。命令行或以编程方式执行
命令行创建 看一看这张照片 遵循创建用户的步骤,您应该会取得更大的成功。我知道你试过了,但是按照教程进行,然后再试一次。值得尝试这样做:
$ php bin/console fos:user:create adminuser --super-admin
并按要求输入额外数据
控制器创建 从控制器的角度来看,您需要使用
fos\u user.user\u manager
服务来设置新用户
比如说:
$userManager = $this->get('fos_user.user_manager');
$user = $userManager->createUser();
public function newUser( Request $request ) {
// get a blank user, you can use new User if you like but I like to use their tools seeing as they took the time to build them.
$user = $this->get('fos_user.user.manager')->createUser();
// build the form
$form = $this->createForm(UserType::class, $user);
// bind the form data (if there) to the entity.
$form->handleRequest( $request );
if ( $form->isSubmitted && $form->isValid() ) {
// this will properly encode the password, if changed, remove any plain text versions and flush into the db.
$this->get('fos_user.user.manager')->updateUser($user);
// send the user somewhere with a congrats message
} else {
// anything for invalid form;
}
return $this->render('forms/form.html.twig', [
'form' => $form->createView()
]);
}
然后在表单处理脚本中:
$userManager->updateUser($user);
这将解析在表单中收集并通过正确的密码编码器从表单中提取的plainPassword
数据。然后它将使plainPassword
属性为空,以便在数据库中保持安全
因此,完成的控制器方法可能如下所示:
$userManager = $this->get('fos_user.user_manager');
$user = $userManager->createUser();
public function newUser( Request $request ) {
// get a blank user, you can use new User if you like but I like to use their tools seeing as they took the time to build them.
$user = $this->get('fos_user.user.manager')->createUser();
// build the form
$form = $this->createForm(UserType::class, $user);
// bind the form data (if there) to the entity.
$form->handleRequest( $request );
if ( $form->isSubmitted && $form->isValid() ) {
// this will properly encode the password, if changed, remove any plain text versions and flush into the db.
$this->get('fos_user.user.manager')->updateUser($user);
// send the user somewhere with a congrats message
} else {
// anything for invalid form;
}
return $this->render('forms/form.html.twig', [
'form' => $form->createView()
]);
}
您是如何将用户放入数据库的?手动通过数据库,因为我无法使用命令promt执行此操作,我不知道原因:((这是一张我在尝试插入用户时收到的控制台错误的照片,我无法添加密码,当涉及添加密码时,我无法再在控制台终端中写入密码,用户名可以,电子邮件可以,但当它说请选择密码:我无法写入任何内容时(