首页 文章

在Symfony 3中通过LDAP进行身份验证

提问于
浏览
0

我试图在我的Symfony应用程序中实现活动目录身份验证 .

我似乎无法成功验证 . 每次尝试通过Web表单登录时都会记录以下内容:

2016-08-17 17:07:08] php.DEBUG:ldap_bind():无法绑定到服务器:无效凭据{“type”:2,“file”:“/ Users / firstname.lastname / Sites / url -builder / url_builder / vendor / symfony / symfony / src / Symfony / Component / Ldap / Adapter / ExtLdap / Connection.php“,”line“:53,”level“:28928} [] [2016-08-17 17: 07:08] security.INFO:身份验证请求失败 . {“exception”:“[object](Symfony \ Component \ Security \ Core \ Exception \ BadCredentialsException(code:0):凭据错误 . 在/Users/firstname.lastname/Sites/url-builder/url_builder/vendor/symfony/ symfony / src / Symfony / Component / Security / Core / Authentication / Provider / UserAuthenticationProvider.php:90,Symfony \ Component \ Security \ Core \ Exception \ BadCredentialsException(code:0):提供的密码无效 . 在/ Users / first .name / Sites / url-builder / url_builder / vendor / symfony / symfony / src / Symfony / Component / Security / Core / Authentication / Provider / LdapBindAuthenticationProvider.php:86)“} [] [2016-08-17 17:07 :08] security.DEBUG:身份验证失败,重定向已触发 . {“failure_path”:“login”} [] [2016-08-17 17:07:08] request.INFO:匹配路由“” . { “路线”: “登录”, “route_parameters”:{ “_controller”: “UrlBuilderBundle \控制器\ SecurityController ::则loginAction”, “_route”: “登录”}, “REQUEST_URI”:“HTTP://www.url -builder.dev/app_dev.php/auth/login","method":"GET“} []

我已将用户提供程序配置为从数据库中读取用户:

providers:
    urlbuilder_provider:
        entity:
            class: UrlBuilderBundle:User
            property: username

我已经通过防火墙部分中的表单配置了Ldap身份验证:

provider: urlbuilder_provider
            # activate different ways to authenticate

            # http_basic: ~
            # http://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate

            form_login_ldap:
                login_path: login
                check_path: login
                service:  app.ldap
                dn_string: 'uid={username},dc=global,dc=com'
                default_target_path: /manage-user

我的登录操作包含以下内容:

public function loginAction(Request $request)
{
    $authenticationUtils = $this->get('security.authentication_utils');

    // get the login error if there is one
    $error = $authenticationUtils->getLastAuthenticationError();

    // last username entered by the user
    $lastUsername = $authenticationUtils->getLastUsername();

    return $this->render(
        'security/login.html.twig',
        array(
            // last username entered by the user
            'last_username' => $lastUsername,
            'error'         => $error,
        )
    );
}

我在一个独立的PHP脚本中测试了ldap身份验证,它按预期工作 . 我将用户详细信息存储在数据库表中,但希望对活动目录进行身份验证 . 我的感觉是问题是用户名和密码是如何提交给LDAP的,但却无法完全指责它 .

我很好,真的很难过,如果有人能够对此有所了解,我会感激不尽 .


更新

我正在验证的AD中的用户名具有域“全局”,因此这意味着用户名将采用以下格式:'global \ firstname.lastname'

我觉得用户名字符串中的反斜杠是我的问题的根源 . 我'var_dump' -ed在bind()中传递的用户名,字符'5c'正在反斜杠之后添加; 'global \ 5c firstname.lastname' . 如何确保正确处理反斜杠?

3 回答

  • 0

    我通过将 dn_string: 'uid={username},dc=global,dc=com' 更改为 dn_string: global\{username} 解决了我的问题 .

    这意味着用户在通过表单登录时不会在其用户名中提供域,因此避免了在反斜杠后添加“5c”的问题 .

  • 0

    我在Laravel中使用了ldap,我的代码看起来像那样 .

    打开连接

    $ldap_connection = ldap_connect(config('app.ldap_server'), config('app.ldap_port'));
    

    检查连接并使用ldap登录

    $username = "test"
         $password = "test"
    
         if($ldap_connection){
                        ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
                        ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0);
                        $ldap_bind = @ldap_bind($ldap_connection, $username, $password);
                        if($ldap_bind){
                            // Here you put the code for save in database
                        }
                    }
    

    结束连接 .

    ldap_close($ldap_connection);
    
  • 1

    对于Active Directory,您应该在 form_login_ldap config中使用以下DN字符串: dn_string: '{username}' . 实际上,你没有't even need to define it all because that'是默认值 . 见:http://symfony.com/doc/current/security/ldap.html#dn-string

    它当前不起作用的原因是因为如果用户使用名称 foo 登录表单,它将尝试绑定DN字符串 uid=foo,dc=global,dc=com .

相关问题