繁体   English   中英

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

[英]Authentication via LDAP in Symfony 3

我试图在我的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}”。 {“route”:“login”,“route_parameters”:{“_ controller”:“UrlBuilderBundle \\ Controller \\ SecurityController :: loginAction”,“_ route”:“login”},“request_uri”:“ http://www.url -builder.dev/app_dev.php/auth/login “,”方法“:”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'。 如何确保正确处理反斜杠?

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

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

对于Active Directory,您应该在form_login_ldap配置中使用以下DN字符串: dn_string: '{username}' 实际上,您甚至不需要全部定义它,因为这是默认值。 请参阅: http//symfony.com/doc/current/security/ldap.html#dn-string

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

我在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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM