Symfony登录会话数据

时间:2018-12-28 17:21:21

标签: php symfony authentication

我遵循了本教程https://symfony.com/doc/current/security/form_login_setup.html,并且使用的是最新版本(4.2.x),但似乎该应用程序从未设置此处描述的会话值:

https://symfony.com/doc/current/security/form_login_setup.html#redirecting-to-the-last-accessed-page-with-targetpathtrait

在哪里以及如何设置?在文档中说它将自动设置,但是当我在调试控制台中检查会话时,没有类似的东西。现在,用户/管理员将仅重定向到设置的路径,而不会重定向到请求的页面(例如somecontroller / action / id)

security.yaml

 for widget in f.winfo_children():
       if isinstance(widget, tk.Text):
            widget.delete('1.0', "end")
       elif isinstance(widget,ttk.Label):
            widget['text'] = ''    

LoginFormAuthentificator.php

    firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        anonymous: true
        guard:
            authenticators:
                - App\Security\LoginFormAuthenticator

        logout:
            path:   app_logout
            target: /
        remember_me:
            secret:   '%kernel.secret%'
            lifetime: 604800 # 1 week in seconds
            path:     /
            secure: true
            name: REMEMBERUSER
        form_login:
            use_referer: true

我还尝试通过将提供者密钥硬编码为 main 并在登录表单中使用_target_path设置隐藏的输入字段来进行尝试。两者都不会导致用户体验打开受限的链接,并且在登录后将用户重定向到该路径。

倾销

  

$ this-> getTargetPath($ request-> getSession(),$ providerKey)

返回

1 个答案:

答案 0 :(得分:0)

默认情况下,变量_target_path仅在用户访问私有URL时设置(例如:/ my-account)。如果他没有连接,并且想访问该URL,他将返回登录页面,并设置_target_path变量。如上所述,仅当用户想要访问私有网址时,才定义变量。

因此,无论URL是什么,您都必须告诉symfony您也要保存。文档在这里(示例代码在下面)

https://symfony.com/doc/current/security/form_login_setup.html#redirecting-to-the-last-accessed-page-with-targetpathtrait

您可以使用make:subscriber命令生成默认文件,或手动创建文件。

然后,只需将示例代码从symfony文档复制并粘贴到生成的文件中。这是放入生成文件中的代码。

namespace App\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Http\Util\TargetPathTrait;

class RequestSubscriber implements EventSubscriberInterface
{
    use TargetPathTrait;

    private $session;

    public function __construct(SessionInterface $session)
    {
        $this->session = $session;
    }

    public function onKernelRequest(RequestEvent $event): void
    {
        $request = $event->getRequest();
        if (!$event->isMasterRequest() || $request->isXmlHttpRequest()) {
            return;
        }

        $this->saveTargetPath($this->session, 'main', $request->getUri());
    }

    public static function getSubscribedEvents()
    {
        return [
            KernelEvents::REQUEST => ['onKernelRequest']
        ];
    }
}

就是这样,现在所有路由都使用公共/私有URL重定向。

NB:对于现在的私有URL,当用户未登录但在浏览器中键入url / dashboard时,然后重定向到登录表单,会话中保存的最后一个URL将是同一页,因此登录页。但是,用户将不得不根据其角色手动重定向。我在下面留下一个例子。

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

class SecurityController extends AbstractController
{
    /**
     * @Route("/login", name="app_login")
     */
    public function login(AuthenticationUtils $authenticationUtils): Response
    {
        if ($this->getUser()) {
            if( in_array('ROLE_ADMIN', $this->getUser()->getRoles() ) ):
                return $this->redirectToRoute('backend_home');
            elseif ( in_array('ROLE_VENDOR', $this->getUser()->getRoles() ) ):
                return $this->redirectToRoute('frontend_account_vendor_dashboard');
            elseif ( in_array('ROLE_CUSTOMER', $this->getUser()->getRoles() ) ):
                return $this->redirectToRoute('frontend_account_history');
            else:
                return $this->redirectToRoute('frontend_home');
            endif;
        }

        // get the login error if there is one
        $error = $authenticationUtils->getLastAuthenticationError();
        // last username entered by the user
        $lastUsername = $authenticationUtils->getLastUsername();

        return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
    }

    /**
     * @Route("/logout", name="app_logout")
     */
    public function logout()
    {
        throw new \Exception('This method can be blank - it will be intercepted by the logout key on your firewall');
    }
}