knpuniversity/guard-bundle

此包已被弃用且不再维护。未建议替代包。

此Bundle为Symfony提供Guard风格的认证

安装次数: 28 604

依赖者: 0

建议者: 0

安全: 0

星标: 45

关注者: 10

分支: 5

开放问题: 0

类型:symfony-bundle

0.3 2015-06-28 19:02 UTC

This package is auto-updated.

Last update: 2022-02-17 20:49:01 UTC


README

为您的Symfony项目添加简单美观的认证。

Build Status

由于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步 - 修改firewall(s)

打开并修改security.yml文件,将firewall(s)中的key(s) 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步 - 更新Authenticator(s)

更新Authenticator(s)类中的uses。

警告: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\Authenticator\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(不会比从Symfony 1到Symfony 2更糟糕);-)

文档

在此处找到完整的教程:https://knpuniversity.com/screencast/guard

基本用法

查看教程以获取真实文档。但以下是基本思路。

Guard通过创建一个单类 - 一个 认证器 - 来处理关于如何认证用户的所有事情。认证器实现了KnpU\Guard\GuardAuthenticatorInterface)

以下是教程中的真实世界示例

目标 代码 教程
通过读取 X-TOKEN 头部进行认证 ApiTokenAuthenticator.php 如何通过API令牌进行认证
表单登录认证 FormLoginAuthenticator.php 如何创建登录表单
社交登录(Facebook) FacebookAuthenticator.php 使用Facebook进行社交登录

贡献

发现了一个此功能不支持的问题或用例?请打开一个问题,以便我们改进。

许可

此库受MIT许可。请参阅LICENSE文件中的完整许可。