
本文档介绍如何使用 Symfony 框架和 SAML 2.0 单点登录 (SSO) 来保护静态网站的 Twig 路由。我们将使用 hslavich/OneloginSamlBundle 集成 SAML 认证,并配置 Symfony 的安全组件来限制对特定路由的访问,确保只有经过身份验证的用户才能访问这些页面。
配置 Symfony 项目以支持 SAML 2.0 SSO
本教程将指导你如何设置一个 Symfony 项目,该项目通过 SAML 2.0 SSO 提供静态 Twig 路由。我们将使用 hslavich/OneloginSamlBundle 来简化 SAML 集成。
1. 安装 OneloginSamlBundle
首先,使用 Composer 安装 hslavich/OneloginSamlBundle:
composer require hslavich/onelogin-saml-bundle
2. 配置 OneloginSamlBundle
根据你的服务提供商 (SP) 和身份提供商 (IdP) 的配置,填写 config/packages/hslavich_onelogin_saml.yaml 文件。请参考该 Bundle 的 README.md 文件获取详细的配置说明。
一个重要的注意事项是,baseurl 配置值应该设置为应用程序的域名,并 附加 /saml。例如:http://myapp.com/saml。这是因为 Bundle 中存在一个 bug,它会移除最后一个路径值(例如 /saml/acs 中的 acs)和域名之间的所有内容。
一个典型的配置示例如下:
# config/packages/hslavich_onelogin_saml.yamlhslavich_onelogin_saml: sp: entityid: "your_sp_entity_id" assertion_consumer_service: url: "http://myapp.com/saml/acs" single_logout_service: url: "http://myapp.com/saml/sls" NameIDFormat: "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress" x509cert: "YOUR_SP_CERTIFICATE" privateKey: "YOUR_SP_PRIVATE_KEY" idp: entityid: "your_idp_entity_id" single_sign_on_service: url: "your_idp_sso_url" single_logout_service: url: "your_idp_slo_url" x509cert: "YOUR_IDP_CERTIFICATE" security: username_mapper: HslavichOneloginSamlBundleSecurityUserAttributeUserMapper
请确保将 YOUR_SP_CERTIFICATE, YOUR_SP_PRIVATE_KEY, your_idp_entity_id, your_idp_sso_url, your_idp_slo_url 和 YOUR_IDP_CERTIFICATE 替换为你的实际值。
3. 配置 Symfony 安全组件
更新 config/packages/security.yaml 文件以集成 SAML 认证。
security: enable_authenticator_manager: true providers: saml_provider: saml: user_class: AppSecurityUser default_roles: - ROLE_USER app_user_provider: id: AppSecurityUserProvider # 可选,如果你需要自定义 UserProvider firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false app: saml: provider: saml_provider username_attribute: eduPersonTargetedID # 映射 SAML 属性到用户名 check_path: saml_acs # SAML Assertion Consumer Service 路径 login_path: saml_login # SAML 登录路径 logout: path: saml_logout # SAML 登出路径 main: lazy: true provider: app_user_provider # 可选,如果你需要自定义 UserProvider access_control: - { path: ^/saml/login, roles: PUBLIC_ACCESS } # 允许匿名访问 SAML 登录路径 - { path: ^/saml/metadata, roles: PUBLIC_ACCESS } # 允许匿名访问 SAML 元数据路径 - { path: ^/, roles: ROLE_USER } # 所有其他路径需要 ROLE_USER 角色
解释:
providers: 定义了 saml_provider,它使用 saml 驱动,并指定了用户类和默认角色。 app_user_provider是可选的,如果需要自定义用户提供者则需要配置。firewalls: 定义了 app 防火墙,它使用 saml 认证。username_attribute: 指定了用于匹配用户名的 SAML 属性。 默认情况下,它使用 getNameId() 方法,但你可以将其配置为使用任何其他的 SAML 属性。check_path: 定义了 SAML Assertion Consumer Service (ACS) 的路径。login_path: 定义了 SAML 登录路径。logout: 定义了 SAML 登出路径。access_control: 定义了访问控制规则。/saml/login 和 /saml/metadata 路径允许公开访问。所有其他路径需要 ROLE_USER 角色。
4. 创建 User 类 (AppSecurityUser)
创建一个 AppSecurityUser 类,实现 SymfonyComponentSecurityCoreUserUserInterface 接口。这个类将代表经过 SAML 认证的用户。
username = $username; $this->roles = $roles; } public function getUsername(): string { return $this->username; } public function getRoles(): array { return $this->roles; } public function getPassword(): ?string { return null; // SAML 认证不需要密码 } public function getSalt(): ?string { return null; // SAML 认证不需要 salt } public function eraseCredentials() { // 如果需要,可以在这里清除敏感数据 } public function getUserIdentifier(): string { return $this->username; }}
5. (可选) 创建 UserProvider 类 (AppSecurityUserProvider)
如果你需要自定义用户加载逻辑,可以创建一个 AppSecurityUserProvider 类,实现 SymfonyComponentSecurityCoreUserUserProviderInterface 接口。
loadUserByIdentifier($user->getUserIdentifier()); } public function supportsClass(string $class): bool { return User::class === $class; } public function loadUserByUsername(string $username): UserInterface { return $this->loadUserByIdentifier($username); }}
6. 创建 Twig 模板和控制器
创建你的 Twig 模板和控制器来显示静态内容。例如:
render('home/index.html.twig', [ 'message' => 'Welcome to the secured area!', ]); }}
{# templates/home/index.html.twig #}{{ message }}
You are logged in!
Logout
7. 测试
现在,访问你的应用程序的根路径(例如 http://myapp.com/)。你将被重定向到 IdP 进行身份验证。成功认证后,你将被重定向回应用程序,并可以访问受保护的页面。
总结
通过以上步骤,你已经成功配置了一个 Symfony 项目,该项目使用 hslavich/OneloginSamlBundle 和 SAML 2.0 SSO 来保护静态 Twig 路由。 /saml/login 和 /saml/metadata 路径可以公开访问,而所有其他路由都需要 ROLE_USER 角色。 用户成功通过 IdP 身份验证后,将被重定向回来并获得会话,然后可以访问站点中的所有路由。
注意事项:
确保你的 SP 和 IdP 配置正确。在生产环境中,使用 HTTPS 来保护 SAML 消息。定期更新 hslavich/OneloginSamlBundle 以获取最新的安全补丁。根据你的需求自定义用户加载逻辑。仔细阅读 hslavich/OneloginSamlBundle 的文档以获取更多高级配置选项。
以上就是使用 Symfony 和 SAML 2.0 SSO 保护静态 Twig 路由的详细内容,更多请关注php中文网其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/89617.html
微信扫一扫
支付宝扫一扫