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 smptlib使用465端口发送邮件失败
    • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
    • ¥15 对于squad数据集的基于bert模型的微调
    • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
    • ¥20 steam下载游戏占用内存
    • ¥15 CST保存项目时失败
    • ¥15 树莓派5怎么用camera module 3啊
    • ¥20 java在应用程序里获取不到扬声器设备
    • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
    • ¥15 Attention is all you need 的代码运行