dongshu4221 2016-08-13 18:11
浏览 72

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

I've some trouble with LDAP authentication with a custom login form, however it works perfectly with the http_basic_ldap so it's not an error of LDAP.

My files : app/config/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: ~

Note that commented http basic works perfectly !

app/config/services.yml

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

Controller/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,
                            )
                    );
            }
    }

and Resources/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>

Thank you for answers ;)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类