Symfony 4:login_check重定向后,会话中的身份验证令牌丢失

时间:2018-02-12 07:30:52

标签: authentication symfony4 symfony-security

最近,我使用PdoSessionHandler对数据库中的会话进行了一些代码更改。我正在使用Guard身份验证。 checkCredentials工作正常,工作正常,插入"会话"表也​​很好。但是/ login_check重定向后,会话中的身份验证令牌会丢失。

身份验证令牌以序列化格式存储在" _security_secured_area"在会话中,会话也保存在数据库中,但是从/ login_check到/ login_redirect会话的重定向具有相同的ID,但缺少身份验证令牌详细信息。可能它无法从数据库中填充auth详细信息。

这是我的packages / security.yaml

firewalls:
    # disables authentication for assets and the profiler, adapt it according to your needs
    dev:
        pattern:  ^/(_(profiler|wdt)|css|images|js)/
        security: false

    secured_area:
        pattern:    ^/
        anonymous: ~
        guard:
            authenticators:
                - App\Security\LoginFormAuthenticator
        logout:
            path:   _logout
            target: _public_signin

        logout_on_user_change: true
        remember_me:
            secret:   '%kernel.secret%'
            lifetime: 2592000 # 30 days in seconds
            path:     /
            domain: ~
            remember_me_parameter: _stay_signedin
            # by default, the feature is enabled by checking a
            # checkbox in the login form (see below), uncomment the
            # following line to always enable it.
            #always_remember_me: true
            token_provider: token_service

这是我的gurardAuthenticator:

/**
 * Override to change what happens after successful authentication.
 *
 * @param Request        $request
 * @param TokenInterface $token
 * @param string         $providerKey
 *
 * @return RedirectResponse
 */
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
    /** @var User $user */
    $user = $token->getUser();

    if ($user->getNewUser() === true) {
        $url = '_onboard';
    } elseif ($user->getResetPass() === true) {
        $url = '_change_temp_password';
    } else {
        $url = '_login_redirect';
    }
    //$request->getSession()->save();
    // MAS: TODO Add Audit probably in listener
    return new RedirectResponse($this->urlGenerator->generate($url));
}

在AuthenticationSuccess之后,它会自动重定向到SecurityController.php中的loginReditrectAction(),但是这里PostAuthenticationGuardToken丢失,AuthenticationEvent返回AnonymousToken。 我在SecurityContrller.php中的loginRedirectAction()中打印会话时发现的另一个观察结果是" _security_secured_area"会话数据丢失。

 #session: Session {#149 ▼
#storage: NativeSessionStorage {#148 ▶}
-flashName: "flashes"
-attributeName: "attributes"
-data: &2 array:2 [▼
  "_sf2_attributes" => &1 array:4 [▼
    "_csrf/https-kinetxx" => "rvR8Rr2UcDM_-y16ehk_jgYvMREJ8mTNouYCT16RtfY"
    "_security.last_username" => "ktx_provider"
    "userTimeZone" => "America/Chicago"
    "practiceTimeZone" => "America/New_York"
  ]
  "_symfony_flashes" => &3 []
]

My SecurityController.php

/**
 * @Route("/login_redirect", name="_login_redirect")
 *
 * @param Request $request
 *
 * @return RedirectResponse
 */
public function loginRedirectAction(Request $request)
{
   dump($request);
   dump($this->get('security.authorization_checker'));
   die;
}

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我遇到了这个问题,解决方案是将我的提供者实体更改为

implements UserInterface, \Serializable

implements AdvancedUserInterface, \Serializable, EquatableInterface

并添加所需的方法:isEqualTo(UserInterface $user)isAccountNonExpired()isAccountNonLocked()isCredentialsNonExpired()isEnabled()

相关问题