Symfony2/SonataUserBundle-注册后自动登录
在我的项目中,我使用symfony2.8、SonataAdminBundle和SonataUserBundleSymfony2/SonataUserBundle-注册后自动登录,symfony,fosuserbundle,sonata-admin,sonata-user-bundle,Symfony,Fosuserbundle,Sonata Admin,Sonata User Bundle,在我的项目中,我使用symfony2.8、SonataAdminBundle和SonataUserBundle $userManager->updateUser($user); $response = new RedirectResponse($this->generateUrl('sonata_admin_dashboard')); $this->authenticateUser($user, $response); $this->setFlash('notice',
$userManager->updateUser($user);
$response = new RedirectResponse($this->generateUrl('sonata_admin_dashboard'));
$this->authenticateUser($user, $response);
$this->setFlash('notice', 'success!');
return $response;
我覆盖FOSUserBundle中RegistrationController的registerAction。成功注册后,我希望用户自动登录到SonataAdmin后端
$userManager->updateUser($user);
$response = new RedirectResponse($this->generateUrl('sonata_admin_dashboard'));
$this->authenticateUser($user, $response);
$this->setFlash('notice', 'success!');
return $response;
办理注册后,我在行动中称之为:
$userManager->updateUser($user);
$response = new RedirectResponse($this->generateUrl('sonata_admin_dashboard'));
$this->authenticateUser($user, $response);
$this->setFlash('notice', 'success!');
return $response;
我的FOSUser配置如下所示:
$userManager->updateUser($user);
$response = new RedirectResponse($this->generateUrl('sonata_admin_dashboard'));
$this->authenticateUser($user, $response);
$this->setFlash('notice', 'success!');
return $response;
fos_user:
db_driver: orm
firewall_name: admin
user_class: Application\Sonata\UserBundle\Entity\User
service:
user_manager: sonata.user.orm.user_manager
registration:
confirmation:
enabled: false
form:
validation_groups: [pwValidation]
firewalls:
admin:
pattern: /admin/(.*)
context: admin
form_login:
provider: fos_userbundle
login_path: /admin/login
use_forward: true
check_path: /admin/login_check
failure_path: null
default_target_path: /admin/dashboard
always_use_default_target_path: true
logout:
path: /admin/logout
anonymous: true
我的security.yml
如下所示:
$userManager->updateUser($user);
$response = new RedirectResponse($this->generateUrl('sonata_admin_dashboard'));
$this->authenticateUser($user, $response);
$this->setFlash('notice', 'success!');
return $response;
fos_user:
db_driver: orm
firewall_name: admin
user_class: Application\Sonata\UserBundle\Entity\User
service:
user_manager: sonata.user.orm.user_manager
registration:
confirmation:
enabled: false
form:
validation_groups: [pwValidation]
firewalls:
admin:
pattern: /admin/(.*)
context: admin
form_login:
provider: fos_userbundle
login_path: /admin/login
use_forward: true
check_path: /admin/login_check
failure_path: null
default_target_path: /admin/dashboard
always_use_default_target_path: true
logout:
path: /admin/logout
anonymous: true
每次我注册一个新用户时,我都会被重定向到SonataAdmin登录屏幕,屏幕上显示401状态码,Symfony profiler会说我通过了匿名令牌的身份验证
$userManager->updateUser($user);
$response = new RedirectResponse($this->generateUrl('sonata_admin_dashboard'));
$this->authenticateUser($user, $response);
$this->setFlash('notice', 'success!');
return $response;
我已经尝试跳过这个函数了
$userManager->updateUser($user);
$response = new RedirectResponse($this->generateUrl('sonata_admin_dashboard'));
$this->authenticateUser($user, $response);
$this->setFlash('notice', 'success!');
return $response;
$this->authenticateUser($user, $response);
并手动将其称为:
$userManager->updateUser($user);
$response = new RedirectResponse($this->generateUrl('sonata_admin_dashboard'));
$this->authenticateUser($user, $response);
$this->setFlash('notice', 'success!');
return $response;
$token = new UsernamePasswordToken($user, null, 'admin', $user->getRoles());
$this->container->get('security.token_storage')->setToken($token);
$this->container->get('session')->set('_security_main', serialize($token));
但如果我尝试这个,我会得到同样的行为
$userManager->updateUser($user);
$response = new RedirectResponse($this->generateUrl('sonata_admin_dashboard'));
$this->authenticateUser($user, $response);
$this->setFlash('notice', 'success!');
return $response;
有人能告诉我我做错了什么吗?我找到了自动身份验证不起作用的原因。
$userManager->updateUser($user);
$response = new RedirectResponse($this->generateUrl('sonata_admin_dashboard'));
$this->authenticateUser($user, $response);
$this->setFlash('notice', 'success!');
return $response;
问题是我的/register
路由不在我想要验证用户身份的防火墙后面。
我将路由更改为/admin/register
,并将此路由的访问控制设置设置为:
$userManager->updateUser($user);
$response = new RedirectResponse($this->generateUrl('sonata_admin_dashboard'));
$this->authenticateUser($user, $response);
$this->setFlash('notice', 'success!');
return $response;
-{ path: ^/admin/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
之后,通过调用方法$this->authenticateUser($user,$response)对用户进行正确身份验证代码>
$userManager->updateUser($user);
$response = new RedirectResponse($this->generateUrl('sonata_admin_dashboard'));
$this->authenticateUser($user, $response);
$this->setFlash('notice', 'success!');
return $response;