【问题标题】:Symfony 4 redirect after user authentication on IIS (sf_redirect cookie)Symfony 4 在 IIS 上进行用户身份验证后重定向(sf_redirect cookie)
【发布时间】:2019-11-29 23:57:08
【问题描述】:

我正在使用 Symfony 4,并且需要在访问任何页面之前对用户进行身份验证。这些是我的防火墙设置:

firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        anonymous: ~
        access_denied_handler: App\Security\AccessDeniedHandler
        guard:
            authenticators:
                - App\Security\UserAuthenticator
        logout:
            path: app_logout        # logout-route
            target: app_loggedout   # where to go after successful logout

这是 access_control(也在 security.yaml 中):

access_control:
    - { path: ^/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/loggedout, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin, roles: ROLE_ADMIN }
    - { path: ^/, roles: ROLE_USER }

调用 UserAuthenticator 类,自动对用户进行身份验证(因为 $_SERVER 变量中提供的用户名)。

但现在:我希望用户在身份验证后被重定向到原始 URL。例如。如果用户想去 SITE/test,这应该在他们通过 UserAuthenticator 后自动发生。 我已经设法通过使用 Symfony 似乎自动设置的 sf_redirect-cookie 来实现这一点,它包含如下内容:

{"token":"b2282c","route":"app_some_url","method":"GET","controller":{"class":"App\\Controller\\SecurityController","method":"some_method","file":"<PATH>\\src\\Controller\\SecurityController.php","line":23},"status_code":302,"status_text":"Found"}

在我的 UserAuthenticator 中,我检查 cookie 是否存在、是否已填充并设置了路由,然后返回一个 RedirectRespone 导致 onAuthenticationSuccess() 中的路由。

问题是:虽然这在 Apache(开发系统)上运行良好,但 IIS(产品系统)上完全缺少 sf_redirect-cookie。并不是 IIS 根本不会设置 cookie($_COOKIE['PHPSESSID'] 在那里),只是缺少 sf_redirect。

我一直在尝试使用重定向到登录事件的自定义订阅者,但我不想使用解决方法,所以问题是:如何告诉 Symfony 在 IIS 上设置 sf_redirect-cookie ,或者我需要如何配置IIS来设置它?

【问题讨论】:

  • 您是否在身份验证器中使用了github.com/symfony/symfony/blob/master/src/Symfony/Component/…?如果不能,您可以将您的身份验证器添加到您的问题中
  • 感谢您回来。不,我没有使用 TargetPathTrait,我什至不知道它存在,Symfony 文档中自定义 UserAuthenticator 的示例不包括这一点。我刚刚将它添加为return new RedirectResponse($this-&gt;getTargetPath($request-&gt;getSession(), 'main'));,它就像一个魅力。随时留下您的评论作为答案,我很乐意接受它:)

标签: php symfony iis cookies symfony4


【解决方案1】:

您可以将TargetPathTrait添加到您的身份验证器中,以在成功登录后重定向用户。

public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
    ....

    return new RedirectResponse($this->getTargetPath($request->getSession(), $providerKey));

    ....
}

【讨论】:

    猜你喜欢
    • 2014-09-22
    • 2021-09-04
    • 2012-12-26
    • 1970-01-01
    • 2023-03-20
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多