通过LDAP进行的Symfony身份验证不适用于登录表单

时间:2016-08-13 18:11:33

标签: php symfony authentication ldap

我使用自定义登录表单进行LDAP身份验证时遇到了一些问题,但它与http_basic_ldap完美配合,因此它不是LDAP的错误。

我的档案: 应用/配置/ security.yml

security:
providers:
    my_ldap:
        ldap:
            service: ldap
            base_dn: ou=People,dc=insa-toulouse,dc=fr
            search_dn: ~
            search_password: ~
            default_roles: ROLE_USER
            uid_key: uid

firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    login_firewall:
        pattern:   ^/login$
        anonymous: ~
    main:
        logout:
            path: /logout
        form_login_ldap:
            login_path: login
            check_path: login_check
            service: ldap
            dn_string: 'uid={username},ou=People,dc=insa-toulouse,dc=fr'
        #anonymous: true
        #http_basic_ldap:
        #    service: ldap
        #    dn_string: 'uid={username},ou=People,dc=insa-toulouse,dc=fr'
    default:
        anonymous: ~

请注意,评论的http basic完美无缺!

应用/配置/ services.yml

services:
ldap:  
    class: Symfony\Component\Ldap\LdapClient
    arguments:
        - srv-ldap

控制器/ SecurityController.php

        <?php

    namespace ClubInfo\PizzaBundle\Controller;

    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

    class SecurityController extends Controller
    {
            /**
             * @Route("/login", name="login")
             */
            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(
                            'ClubInfoPizzaBundle:security:login.html.twig',
                            array(
                                    // last username entered by the user
                                    'last_username' => $lastUsername,
                                    'error'         => $error,
                            )
                    );
            }
    }

资源/ views / security / login.html.twig

{% if error %}
    <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}

<form action="{{ path('login') }}" method="post">
    <label for="username">Username:</label>
    <input type="text" id="username" name="_username" value="{{ last_username }}" />

    <label for="password">Password:</label>
    <input type="password" id="password" name="_password" />

    {#
        If you want to control the URL the user
        is redirected to on success (more details below)
        <input type="hidden" name="_target_path" value="/account" />
    #}

    <button type="submit">login</button>
</form>

感谢您的回答;)

0 个答案:

没有答案
相关问题