Symfony 3 - 管理两种身份验证模式,注销问题

问题描述 投票:0回答:1

在symfony上,我必须开发一个应用程序,它可以通过站点上的表单来管理连接,但也需要CAS连接。

当涉及到直接连接到站点的连接(对于外部用户)时,它们必须具有表单。

对于CAS连接(作为LDAP一部分的服务内部的用户),当他们访问该站点时,它们会自动重定向到连接站点,并且一旦连接,它们将被重定向到正确的站点。

对于外部用户及其管理的连接,我使用FOSUserBundle。对于CAS身份验证,我使用我公司开发的捆绑包重定向到登录页面,然后获取用户的信息。

问题是,这是我们第一次开发一个必须考虑几种类型连接的项目,并且不可避免地存在我们不理解的冲突。

所以,这是我的代码:

Security.yml:

# app/config/security.yml

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: ROLE_ADMIN

providers:
    app:
       id: bes_auth.user_provider

firewalls:

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

    main:
        logout_on_user_change: true
        pattern: ^/

        guard:
            authenticators:
                - app.security.login_form_authenticator
                - bes_auth.authenticator

            entry_point: Site\PagesBundle\Security\LoginFormAuthenticator

        logout:
            path:   /logout #nom de la route de déconnexion
            target: /
            success_handler: bes_auth.authenticator
        anonymous:    true

access_control:

    #    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    #   - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
    #  - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/.*, role: ROLE_SUPER_ADMIN }
    - { path: ^/profile/.*, role: ROLE_USER }
    - { path: ^/logout, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/.*, role: IS_AUTHENTICATED_FULLY }

rooting.yml:

# app/config/routing.yml
fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"

auth_cas_logout:
    path: /logout

services.yml:

############ Guard with FOSUserBundle ##############

app.security.login_form_authenticator:
    class: Site\PagesBundle\Security\LoginFormAuthenticator
    autowire: true


################ CAS ###########################    

Site\PagesBundle\Security\Auth\AuthCasService:
    autowire: true
    parent: Besancon\AuthBundle\Security\Abstracts\AuthAbstract
    public: false  
    autoconfigure: false

config.yml:

# FOS User Bundle
fos_user:
    db_driver: orm
    firewall_name: main
    user_class: Site\PagesBundle\Entity\User
    service:
        mailer: fos_user.mailer.twig_swift
    registration:
        form:
            type: FOS\UserBundle\Form\Type\RegistrationFormType
            name: fos_user_registration_form
            validation_groups: [Registration, Default]
        confirmation:
            enabled: true
            template: '@FOSUser/Registration/email.txt.twig'
    profile:
        form:
            type: FOS\UserBundle\Form\Type\ProfileFormType
            #validation_groups: [Profile, Default]
    resetting:
        email:
            template: '@FOSUser/Resetting/email.txt.twig'
    group:
        group_class: FosSf3\MainBundle\Entity\Group
    from_email:
        address: mail
        sender_name: username


besancon_auth:
    use_default_provider : true
    user_entity: ~
    homepage: "homepage"
    authentication_service: Site\PagesBundle\Security\Auth\AuthCasService
    type_auth: Cas

    cas:
        hostname: "######.######.fr"
        port: ###
        uri: ""

我可以使用页面底部栏中的“注销”注销。通过利弊,使用断开按钮,我返回到主页,我没有断开连接。

我的base.html.twig:

<a class="nav-link" href="{{ path('deconnexion') }}">Déconnexion</a>

我的控制器功能:

/**
     * @Route("/logout", name="deconnexion")
     */
    public function deconnexionAction()
    {
        $token = $this->get('security.token_storage')->getToken();
        //dump($token->getAttribute('nomComplet'));

    $typeAuth = $token->getAttribute('typeAuth');
    dump($typeAuth);

    if($typeAuth == 'cas')
    {
        return $this->redirectToRoute('auth_cas_logout');
    }

    else
    {
        return $this->redirectToRoute('fos_user_security_logout');
    }

}

我将deconnexion函数的路由编辑为/ logout,链接现在正常工作!但是,它会立即将我重定向到SESHAT(我们服务的身份验证页面),我希望将其重定向到主页以选择我想要识别自己的方式。但匿名访问不起作用

symfony fosuserbundle cas
1个回答
0
投票

您可以尝试将Security.yml中的access_control修改为

#    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
#   - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
#  - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/.*, role: ROLE_SUPER_ADMIN }
- { path: ^/profile/.*, role: ROLE_USER }
- { path: ^/logout, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/.*, role: IS_AUTHENTICATED_FULLY }

因为您的注销路线似乎无法访问。

© www.soinside.com 2019 - 2024. All rights reserved.