【问题标题】:LDAP with Guard Authentication System in Symfony 3Symfony 3 中带有 Guard 身份验证系统的 LDAP
【发布时间】:2017-11-07 10:38:14
【问题描述】:

我假装要做的是将内部用户的 LDAP 包含在 ddbb 配置的 Guard 身份验证系统中。 感谢https://knpuniversity.com/screencast/symfony-security,我已经构建了我的 Guard 身份验证系统并且工作得非常好。

但我还需要尝试以前通过 LDAP 模式登录。更准确地说,功能必须是这样的:

用户尝试登录使用 MySQL 数据库配置的 Guard System 身份验证,并且:

1- 检查 MySQL 表 User 中是否存在用户。如果存在,我们转到第 2 步。如果不存在,则返回 false 到身份验证并显示错误消息。

2-检查用户是否存在于LDAP模式。如果存在则执行步骤 3。如果不存在则执行步骤 4。

3-尝试使用用户名和密码通过 LDAP 登录。如果认证成功,则表示已登录。如果无法通过 LDAP 匹配密码,则向认证返回 false 并显示错误消息。

4-检查LDAP选项后,我们将尝试通过Guard Authentication System登录。如果验证没问题,则用户已登录。如果无法通过 Guard 的密码与 MySQL 用户表匹配,则返回 false 并显示错误消息进行验证。

【问题讨论】:

    标签: mysql security authentication ldap symfony-3.3


    【解决方案1】:

    在 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');
        }
    }
    

    我希望任何人都能喜欢这个答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-28
      • 1970-01-01
      相关资源
      最近更新 更多