knpuniversity / guard
为 Symfony 的安全组件提供 Guard-style 身份验证
Requires
- php: >=5.3.9
- symfony/security-core: ~2.6
- symfony/security-http: ~2.6
Requires (Dev)
- phpunit/phpunit: ~4.7
README
为 Silex 和其他任何地方添加简单而美观的认证到 Symfony 的安全组件。
此库自 Symfony 2.8 起已弃用,无法与 Symfony 3 兼容。
原始目的是从人们那里收集反馈和使用案例,以便我们可以将此功能合并到 Symfony 本身(参见 symfony/symfony#14673)。
现在它很好(参见 Symfony 新闻)。
升级到 Symfony 3
在 Symfony 2.8 中,使用官方的 Guard 组件。
步骤 1 - 从 composer.json 中删除库
确保您使用的是 Symfony 2.8,打开 composer.json
文件并删除库
之前
{ "require": { "php": ">=5.5", "symfony/symfony": "~2.8", "...": "...", "knpuniversity/guard-bundle": "~0.1@dev" }, }
现在
{ "require": { "php": ">=5.5", "symfony/symfony": "~2.8", "...": "..." }, }
步骤 2 - 从 AppKernel 中删除它
如果您使用的是 Symfony 框架,请从 AppKernel.php
中删除 KnpUGuardBundle。
步骤 3 - 修改防火墙
打开并修改 security.yml
文件,在您的防火墙(的)键(中)替换 knpu_guard
为 guard
之前
# app/config/security.yml security: # ... firewalls: # ... main: anonymous: ~ logout: ~ knpu_guard: authenticators: - app.form_login_authenticator # maybe other things, like form_login, remember_me, etc # ...
现在
# app/config/security.yml security: # ... firewalls: # ... main: anonymous: ~ logout: ~ guard: authenticators: - app.form_login_authenticator # maybe other things, like form_login, remember_me, etc # ...
步骤 4 - 更新验证器
更新验证器(的)类中的使用。
注意: checkCredentials() 现在必须返回 true 才能使身份验证成功。在 KnpUGuard 中,如果您没有抛出 AuthenticationException,它将通过。
之前
use KnpU\Guard\Authenticator\AbstractFormLoginAuthenticator; use KnpU\Guard\...; // ... class FormLoginAuthenticator extends AbstractFormLoginAuthenticator { // ... public function checkCredentials($credentials, UserInterface $user) { // ... if ($password !== 'correctPassword') { throw new AuthenticationException(); } // do nothing, allow authentication to pass } // ... }
现在
use Symfony\Component\Security\Guard\AbstractFormLoginAuthenticator; use Symfony\Component\Security\Guard\...; // ... class FormLoginAuthenticator extends AbstractFormLoginAuthenticator { // ... public function checkCredentials($credentials, UserInterface $user) { // ... if ($password !== 'correctPassword') { // returning anything NOT true will cause an authentication failure return; // or, you can still throw an AuthenticationException if you want to // throw new AuthenticationException(); } // return true to make authentication successful return true; } // ... }
步骤 5 - 我们可以测试它
就这样!试一试,然后升级到 Symfony 3 :)
文档
在此处找到完整教程: https://knpuniversity.com/screencast/guard
基本用法
查看 教程 以获取真实文档。但这里有一个基本思路。
Guard 通过创建一个单独的类 - 一个 验证器 - 来处理关于如何对用户进行身份验证的 所有 事情。并且验证器实现了 KnpU\Guard\GuardAuthenticatorInterface)
以下是教程中的实际示例
贡献
发现了一个错误或这个库不支持的使用案例? 提交问题,以便我们使事物变得更好。
许可
此库受 MIT 许可证的保护。在 LICENSE 文件中查看完整许可证。