Symfony 2中的两个单独的登录页面 [英] Two separate login pages in Symfony 2

查看:66
本文介绍了Symfony 2中的两个单独的登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何拥有两个单独的登录页面:.com页面的默认登录名和特定用户(例如,路线/special)的默认登录名.

I'm trying to figure out how to have two separate login pages: a default login for the .com page and one for specific users, for example for the route /special.

在一个SF2项目中是否容易做到这一点?

Is this easily possible with in one SF2 project?

更新:

我的防火墙中有以下配置(我正在使用fosub)

I have the following configuration in my firewall (I'm using fosub)

providers:
    custom:
        id: ib.user_provider
    fos_userbundle:
        id: fos_user.user_manager
    my_fos_facebook_provider:
        id: my.facebook.user

firewalls:
    special:
        pattern: ^/special
        form_login:
            provider: fos_userbundle
            login_path: /special/login
            check_path: /special/login_check
            use_referer: false
            default_target_path: /special
            success_handler: ib.login_handler
            provider: custom
    main:
        pattern: ^/.*
        form_login:
            provider: fos_userbundle
            login_path: /login
            check_path: /login_check
            use_referer: false
            default_target_path: /
            provider: custom
        fos_facebook:
            always_use_default_target_path: true
            app_url: "http://apps.facebook.com/%facebook_app_id%/"
            server_url: "http://aw.com/aw/web/app_dev.php/"
            login_path: /login
            check_path: /login_check/facebook
            default_target_path: /checkFB
            success_handler: facebook_auth_success_handler
            provider: my_fos_facebook_provider
        logout:
            #handlers: ["fos_facebook.logout_handler"]
            target: /
        anonymous:    ~  

在ib.login_handler中,我有以下内容:

In the ib.login_handler I have the following:

public function onAuthenticationSuccess(Request $request,TokenInterface $token)
{
    if ($this->security->isGranted('ROLE_CATEGORIZER'))
    {
        $response = new RedirectResponse($this->router->generate('MyCoBundle_mailAdmin_index'));
    }


    return $response;
}

使用这种配置,如果我转到mydomain.com/special,则会收到以下错误:Fehler:Umleitungsfehler(英语:错误:重定向错误)

With this configuration if I go to mydomain.com/special I get the following error: Fehler: Umleitungsfehler (in english: Error: Redirection error)

更新:

在Chrome中,我得到:找不到"GET/special/login"的路由

in chrome I get: No route found for "GET /special/login"

404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException »

此登录路径没有特殊的路由.我要实现的只是,特殊用户只能访问路径/special下的页面.

I don't have a special route for this login path. What I want to achieve is just, that a special user has only access to pages under the path / special.

推荐答案

有可能,您可以定义许多受保护的区域=>许多防火墙.

It's possible, you can define many protected zones => many firewalls.

让我们看看此 app/config/security.yml 配置以了解操作方法:

Let's see this app/config/security.yml configuration to know how :

firewalls:

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

    special_area:
        pattern:  ^/special
        anonymous: ~
        form_login:
            check_path: /special/login_check
            login_path: /special/login
        logout:
            path:   /special/logout
            target: /

    general_area:
        pattern:  ^
        anonymous: ~
        form_login:
            check_path: /login_check
            login_path: /login
        logout:
            path:   /logout
            target: /


access_control:
    - { path: ^/_internal, role: IS_AUTHENTICATED_ANONYMOUSLY, ip: 127.0.0.1 }

    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/special/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }

请注意,必须在 genereal_area 上方定义 special_area ,因为general_area的模式与其他所有模式都匹配...

Beware that special_area must be defined above genereal_area because general_area's pattern matches every other ones...

您必须在捆绑包routing.yml中添加什么:

What you must add in your bundle routing.yml :

_security_login_special:
    pattern:  /special/login
    defaults: { _controller: FOSUserBundle:Security:login }

_security_check_special:
    pattern:  /special/login_check

_security_logout_special:
    pattern:  /special/logout

并且您必须为general_area添加另一个角色,因此您需要覆盖FOSUserManager并使其在用户加载时添加此补充角色...(更多信息,请参见:

And you have to add another role for general_area, so you need to override FOSUserManager and make it add this supplementary role on user loading... (More information here : https://github.com/FriendsOfSymfony/FOSUserBundle/blob/1.2.0/Resources/doc/user_manager.md)

这篇关于Symfony 2中的两个单独的登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆