簡體   English   中英

帶有Symfony 3中的Guard身份驗證系統的LDAP

[英]LDAP with Guard Authentication System in Symfony 3

我假裝要做的是將內部用戶的LDAP包含在ddbb配置的Guard Authentication System中。 感謝https://knpuniversity.com/screencast/symfony-security,我已經建立了我的Guard身份驗證系統,並且工作得很好。

但是我還需要嘗試以前通過LDAP模式登錄。 更准確地說,功能必須是這樣的:

用戶嘗試登錄配置有MySQL數據庫的Guard System Authentication,並且:

1-檢查表MySQL中的用戶中是否存在該用戶。 如果存在,則轉到步驟2。如果不存在,則返回false並使用錯誤消息進行身份驗證。

2-檢查用戶是否以LDAP模式存在。 如果存在,請轉到步驟3。如果不存在,請轉到步驟4。

3-嘗試使用用戶名和密碼通過LDAP登錄。 如果身份驗證通過,則表明已登錄。如果無法通過LDAP匹配密碼,則向錯誤返回false並顯示錯誤消息。

4-選中LDAP選項后,我們將嘗試通過Guard Authentication System登錄。 如果驗證通過,則該用戶已登錄。如果通過Guard與MySQL用戶表的密碼不匹配,則向錯誤返回false並顯示錯誤消息。

最后,在LoginFormAuthenticator文件中,我可以管理所需的此行為,如以下代碼所示。

<?php

namespace AppBundle\Security;

use ...
use Zend\Ldap\Ldap;
use Zend\Ldap\Exception\LdapException;

class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
{
    use TargetPathTrait;

    private $em;
    private $router;
    private $passwordEncoder;
    private $csrfTokenManager;

    public function __construct(...
    }

    public function getCredentials(Request $request)
    {
    ...
    }

    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        $username = $credentials['username'];
        $ldapPassword = $credentials['password'];
        $ldaphost = 'ldap.example.com';    // your ldap servers
        $baseDn = 'dc=example,dc=es';

        $options = [
            'host'              => $ldaphost,
            'username'          => $username,
            'password'          => $ldapPassword,
            'bindRequiresDn'    => false,
            'accountDomainName' => 'example.es',
            'baseDn'            => $baseDn,
        ];


        $userInterface = $this->em->getRepository('AppBundle:User')
            ->findOneBy(['email' => $username]);
        $ldap = new Ldap($options);

        try {
            $ldap->bind();
            $userInterface->setIsAuthenticationLDAP(true);
        } catch (LdapException $zle){
            $userInterface->setIsAuthenticationLDAP(false);
        }

        return $userInterface;
    }

    public function checkCredentials($credentials, UserInterface $user)
    {
        $password = $credentials['password'];

        if($user->isAuthenticationLDAP()){
            $user->setLoginAttempts(0);
            $this->em->persist($user);
            $this->em->flush();
            return true;
        } else {
           if($this->passwordEncoder->isPasswordValid($user, $password)) {
               $user->setLoginAttempts(0);
               $this->em->persist($user);
               $this->em->flush();
               return true;
           } else {
               if($user->getLoginAttempts() == '0') $user->setFirstLoginAttempt(new \DateTime('now'));
               $user->setLoginAttempts($user->getLoginAttempts() + 1);
               if($user->getLoginAttempts() >= 5) {
                   $user->setLockedDateTime(new \DateTime('now'));
                   $user->setLoginAttempts(0);
               }
               $this->em->persist($user);
               $this->em->flush();
           }
       }

       return false;
    }

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

    protected function getLoginUrl()
    {
        return $this->router->generate('fos_user_security_login');
    }
}

我希望任何人都能喜歡這個答案。

暫無
暫無

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

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