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 }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘