【问题标题】:Symfony app that serves static Twig routes gated by SSO SAML 2.0?提供由 SSO SAML 2.0 控制的静态 Twig 路由的 Symfony 应用程序?
【发布时间】:2021-10-29 22:53:30
【问题描述】:

我有一个静态网站,只有通过 SAML2 SSO 进行身份验证的用户才能访问其页面。具体来说,这些页面是用 Twig 编写的,内容存储在 JSON 文件中,这些文件作为变量提供给 Twig 模板。

我想知道是否有一种简单的方法可以利用像 Symfony 这样的 PHP 框架来执行此操作。理想情况下,也不会有数据库层。一旦用户通过了身份验证,应该设置一些 cookie,只允许他们根据需要四处游荡。

我的背景是 Drupal,所以这就是我寻找 Symfony 方向的原因。

我确实意识到这个问题有点宽泛,所以如果有更合适的地方来询问这个问题,请投票结束并指出​​正确的方向。

【问题讨论】:

  • 是的,对于固执己见的答案非常主观。尽管我不确定“简单的方法”,但这绝对是可能的。我建议通过 packagist 查找支持 SAML2 的捆绑包,看看是否可以找到符合您要求的维护包。

标签: php symfony single-sign-on saml-2.0


【解决方案1】:

我已经完成了这个功能,如果这对其他人有用的话,我会发布我的解决方案...

对于 Symfony 5 项目,我使用了https://github.com/hslavich/OneloginSamlBundle。根据包的 README.md 并根据您的 SP 和 IdP 的配置方式填写 config/packages/hslavich_onelogin_saml.yaml。一个专业提示,baseurl 配置值应该设置为应用程序域 with /saml 连接到它上面(例如http://myapp.com/saml),有一个错误会剥离最后一个之间的所有内容路径值(/saml/acs 中的acs)和域。

更新config/packages/security.yaml 看起来像:

security:
    enable_authenticator_manager: true
    # https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
    providers:
        saml_provider:
            saml:
                user_class: App\Security\User
                default_roles:
                    - ROLE_USER
        # used to reload user from session & other features (e.g. switch_user)
        app_user_provider:
            id: App\Security\UserProvider
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        app:
            saml:
                provider: saml_provider
                # Match SAML attribute 'uid' with username.
                # Uses getNameId() method by default.
                username_attribute: eduPersonTargetedID
                # Use the attribute's friendlyName instead of the name
                check_path: saml_acs
                login_path: saml_login
            logout:
                path: saml_logout
        main:
            lazy: true
            provider: app_user_provider

            # activate different ways to authenticate
            # https://symfony.com/doc/current/security.html#the-firewall

            # https://symfony.com/doc/current/security/impersonating_user.html
            # switch_user: true

    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    access_control:
        - { path: ^/saml/login, roles: PUBLIC_ACCESS }
        - { path: ^/saml/metadata, roles: PUBLIC_ACCESS }
        - { path: ^/, roles: ROLE_USER }

最终结果是/saml/login/saml/metadata 是公开可用的,而所有其他路由都需要ROLE_USER 角色。在通过 IdP 成功进行身份验证后,用户将被重定向回来并被授予会话,然后可以访问站点内的所有路由。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 2017-07-27
    • 2017-04-11
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    相关资源
    最近更新 更多