php - 提供由 SSO SAML 2.0 控制的静态 Twig 路由的 Symfony 应用程序?
问题描述
我有一个静态网站,只有在用户通过 SAML2 SSO 进行身份验证时才能访问其页面。具体来说,这些页面是用 Twig 编写的,内容存储在 JSON 文件中,这些文件作为变量提供给 Twig 模板。
我想知道是否有一种简单的方法可以利用像 Symfony 这样的 PHP 框架来做到这一点。理想情况下,也不会有数据库层。一旦用户通过了身份验证,应该设置一些 cookie,只允许他们根据需要四处游荡。
我的背景是 Drupal,所以这就是为什么我正在寻找 Symfony 的方向。
我确实意识到这个问题有点宽泛,所以如果有更合适的地方来询问这个问题,那么请投票结束并指出我正确的方向。
解决方案
我已经完成了这个功能,如果这对其他人有用,我会发布我的解决方案......
对于 Symfony 5 项目,我使用了https://github.com/hslavich/OneloginSamlBundle。config/packages/hslavich_onelogin_saml.yaml
根据包的 README.md 并根据您的 SP 和 IdP 的配置方式填写。一个专业提示,baseurl
配置值应该设置为应用程序域并 /saml
连接到它(例如http://myapp.com/saml
),有一个错误会剥离最后一个路径值(acs
in /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/metadata
ROLE_USER
推荐阅读
- google-cloud-platform - 如何在 Big Query 中使用带有列号引用的 insertAll 插入数据?
- javascript - Firebase Functions 引发内部错误的函数
- node.js - 插入后角度更新组件
- javascript - 为什么在尝试确定数组是否为因子链时此解决方案不起作用
- python - While 循环不... whileing
- c# - 向/从文件写入/读取大型二进制数据的好方法是什么?
- sql-server - 使用合并插入最新记录
- r - 按月对 Rasterstack 进行子集化
- java - 如何强制 Spring Boot 从 application.properties 读取数据库连接配置?
- excel - Excel中百分位函数的动态列