logo

Palette - Make it Colorful🎨

Palette - Visual Page Builder, no design degree required.

Live Demo Download Palette

滚动

Authentication API

03/10/2025, by Ivan

API 特性

按最少使用的 API 排序:

认证提供者服务
实现 \Drupal\Core\Authentication\AuthenticationProviderInterface 并使用服务标签 'authentication_provider'。

路由上的 _auth 选项
默认的认证管理器(见下文)允许开发者通过在路由参数中指定 _auth 来限制允许的认证机制子集。
示例: _auth: ['basic_auth', 'cookie']

认证管理器
认证管理器 (\Drupal\Core\Authentication\AuthenticationManager) 根据每个服务的优先级调用不同的认证提供者服务。

在非常复杂的使用场景下可以重写认证管理器;但在 99.9% 的情况下,默认实现已足够。

有用的接口
Drupal 提供了 2 个额外的接口用于高级认证。

  • \Drupal\Core\Authentication\AuthenticationProviderFilterInterface - 如果你希望你的认证提供者仅在特定路由下使用,这会非常有用,它允许你对 RouteMatch 和路由对象选项进行检查。
  • \Drupal\Core\Authentication\AuthenticationProviderChallengeInterface - 允许在未认证用户被拒绝访问时发起挑战。这被 Basic Auth 模块 使用。

另见