簡體   English   中英

Symfony登錄的其他安全檢查

[英]Additional security checks on Symfony login

我正在使用symfony 2.5,但遇到了障礙。

我根據symfony文檔使用SecurityContext來對用戶進行身份驗證。 具體來說,我目前有:

$securityContext = $this->container->get('security.context');

# If authenticated through Symfony
if ($securityContext->isGranted('IS_AUTHENTICATED_FULLY'))
{
    return $this->redirect($this->generateUrl('accounts_dashboard'), 301);

}
.....

效果很好,但是此應用程序從多個位置注冊了用戶,當時我們還沒有控制重復注冊。 這意味着我們對於同一電子郵件地址有多個條目。

因此,當有人嘗試登錄並且在同一電子郵件地址下有多個帳戶時,登錄過程將失敗,因為它選擇了錯誤的帳戶。

我在數據庫中還有其他字段可以用來匹配正確的帳戶,例如活動狀態,上次登錄IP甚至上次登錄日期。 我的問題是我不確定如何在登錄期間創建其他檢查以正確驗證該信息。

什么是正確的方法而不必重新整理整個登錄流程,以便在調用SecurityContext和其余登錄過程之前,可以對數據庫中提供的電子郵件地址進行其他檢查? 本質上,我只是在嘗試提交登錄后嘗試進行其他檢查,以確保選擇了正確的帳戶,而不是數據庫中的第一個匹配項。

您需要實現一個自定義的UserProvider。 例:

<?php
namespace Acme\Security\Authorization;


use Symfony\Bridge\Doctrine\Security\User\EntityUserProvider;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;

class MyCustomEmailUserProvider extends EntityUserProvider
{

    /**
     * {@inheritdoc}
     */
    public function loadUserByUsername($username)
    {

        //your code to get user by email goes here
        // if you found User, you need to return it from this method, otherwise throw an exception

        throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
    }
}

您不必擴展EntityUserProvider ,您可以考慮自己的UserProviderInterface實現。

將其注冊為服務(假設使用security.my_custom_email_user_provider服務名稱),添加所需的依賴項,然后將其添加到security.yml

providers:
    main:
        id: security.my_custom_email_user_provider

然后在您的登錄表單上使用此提供程序:

firewalls:
      firewall_name:
            form_login:
                 provider: main

查閱Cookbook文章以獲取更多信息。

UserProvider應該按用戶名返回用戶,別無其他,它不應執行繁瑣的邏輯。 我想你可以嘗試創建自己的認證作為解釋這里通過實現SimpleFormAuthenticatorInterface接口。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM