dousao9569 2015-03-04 16:42
浏览 43
已采纳

Symfony2 FOSUserBundle两个登录表单错误配置检查路径

i need to have 2 login form.

1 -> main site 2 -> my backend

i have see many thread for this.

I try to do it, but i have an error : "You must configure the check path to be handled by the firewall using form_login in your security firewall configuration."

But it's set in security.yml

My route is ok, my form is ok, and i don't see where is my error.

This is my security.yml

admin_login:
        context: backend
        pattern: ^/th-admin/login
        anonymous: true

    admin_check:
        context: backend
        pattern: ^/th-admin/login_check
        anonymous: true

    admin_access_denied:
        context: backend
        pattern: ^/th-admin/access_denied
        anonymous: true

    admin:
        context: backend
        pattern: ^/th-admin

        form_login:
            provider: fos_userbundle
            csrf_provider: form.csrf_provider
            #use_referer: true # this is not needed if you always use default target
            #always_use_default_target_path: true
            #default_target_path: th_admin_dashboard
            login_path: th_admin_login
            check_path: th_admin_check

        logout:
            path:   th_admin_logout
            target: th_admin_login
        anonymous: false
        access_denied_url: th_admin_denied




    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            csrf_provider: form.csrf_provider
            login_path: fos_user_security_login
            check_path: fos_user_security_check
        logout:       true
        anonymous:    true


access_control:
    - { path: ^/, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/th-admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/th-admin/access_denied, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/th-admin/, role: ROLE_ADMIN }

This is my fos_user configuration :

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: TH\UserBundle\Entity\User

This is my custom form :

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


        <form class="m-t" role="form" method="post" action="{{ path("th_admin_check") }}">

            <input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />

            <div class="form-group">
                <input name="_username" value="{{ last_username }}" type="email" class="form-control" placeholder="{{ 'admin.login.email'|trans }}" required="">
            </div>

            <div class="form-group">
                <input name="_password" type="password" class="form-control" placeholder="{{ 'admin.login.password'|trans }}" required="">
            </div>
            <input type="hidden" name="_submit" value="Login" />

            <button type="submit" class="btn btn-primary block full-width m-b">{{ 'admin.login.login'|trans }}</button>

        </form>

This is my routes :

th_admin_denied:
    path:     /access_denied
    defaults: { _controller: THAdminBundle:Default:accessDenied }

th_admin_login:
    path:     /login
    defaults: { _controller: FOSUserBundle:Security:login }

th_admin_check:
    path:     /login_check
    defaults: { _controller: FOSUserBundle:Security:check }

th_admin_logout:
    path:     /logout
    defaults: { _controller: FOSUserBundle:Security:logout }

th_admin_dashboard:
    path:     /
    defaults: { _controller: THAdminBundle:Dashboard:index }

I have try to clear cache, hard clear cache (empty folder with rm -rf)

When i set the other check url, this work but this go to the main firewall. The main firewall work. But my admin backend firewall doesn't work...

Anyone know where is my error ?

Thx

  • 写回答

1条回答 默认 最新

  • dtl19910708 2015-03-05 08:34
    关注

    My problem is the firewall configuration.

    Need to have an order in access_control and does'nt create useless firewall. Thx all for your reply.

    This is the good configuration :

    firewalls:
    
        admin_firewall:
            pattern: ^/th-admin
    
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
                #use_referer: true # this is not needed if you always use default target
                #always_use_default_target_path: true
                #default_target_path: th_admin_dashboard
                login_path: th_admin_login
                check_path: th_admin_check
    
            logout:
                path:   th_admin_logout
                target: th_admin_login
            anonymous: ~
            access_denied_url: th_admin_denied
    
    
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
                login_path: fos_user_security_login
                check_path: fos_user_security_check
            logout:       true
            anonymous:    true
    
    
    access_control:
        - { path: /th-admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: /th-admin/access_denied, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/th-admin, role: ROLE_ADMIN }
        - { path: ^/th-admin/$, role: ROLE_ADMIN }
        - { path: ^/, role: IS_AUTHENTICATED_ANONYMOUSLY }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?