首页 > 解决方案 > 提供由 SSO SAML 2.0 控制的静态 Twig 路由的 Symfony 应用程序?

问题描述

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

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

我的背景是 Drupal,所以这就是为什么我正在寻找 Symfony 的方向。

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

标签: phpsymfonysingle-sign-onsaml-2.0

解决方案


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

对于 Symfony 5 项目,我使用了https://github.com/hslavich/OneloginSamlBundleconfig/packages/hslavich_onelogin_saml.yaml根据包的 README.md 并根据您的 SP 和 IdP 的配置方式填写。一个专业提示,baseurl配置值应该设置为应用程序域 /saml连接到它(例如http://myapp.com/saml),有一个错误会剥离最后一个路径值(acsin /saml/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角色。在通过 IdP 成功进行身份验证后,用户将被重定向回来并被授予会话,然后可以访问站点内的所有路由。/saml/metadataROLE_USER


推荐阅读