5

目前我的项目运行良好。我使用 FOSUserBundle 来管理我的用户。现在,我想实现 OAuth,所以我使用 FOSOAuthServerBundle。大多数开发人员推荐使用此捆绑包来实现 OAuth。

我遵循了 FOSOAuthServerBundle 的文档。通常,我必须在我的 security.yml 中添加更多信息,但我不确切知道我必须做什么......

这是我的 security.yml :

security:
    encoders:
       Symfony\Component\Security\Core\User\User: plaintext
       Moodress\Bundle\UserBundle\Entity\User: sha512

    role_hierarchy:
       ROLE_ADMIN:       ROLE_USER
       ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    providers:
       main:
           id: fos_user.user_provider.username

    firewalls:
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false
        oauth_token:
            pattern:    ^/oauth/v2/token
            security:   false

        oauth_authorize:
            pattern:    ^/oauth/v2/auth

        main:
            pattern: ^/
            fos_oauth:  true
            stateless:  true
            anonymous: true

我想可能有一些信息要添加到防火墙中??

我真的不知道如何使 FOSOAuthServerBundle 与 FOSUserBundle 一起工作。之前,仅使用 FOSUserBundle,我使用了 FOSUserBundle 的登录表单和登录检查。现在我把 FOSOAuthServerBundle 的所有基本配置都放好了,接下来我要做什么呢?我应该使用哪种形式?哪个登录检查?令牌是由 FOSOAuthServerBundle 自动创建的?在文档中,他们展示了如何创建客户端......我应该在我的项目中添加这个代码吗?如果是...在哪里?:/

我在网上找到了这篇文章:http: //blog.logicexception.com/2012/04/securing-syfmony2-rest-service-wiith.html

我试图实现这一点,但我不敢相信我们需要添加所有这些文件才能使其工作......

如果有人知道如何使用 FOSUserBundle 制作 FOSOAuthServerBundle,那将非常有帮助。

4

1 回答 1

7

我刚刚安装了这个捆绑包并开始使用它。

我认为您需要首先了解有关 OAuth 身份验证如何工作的更多信息。

这样您就会明白 FOSUserBundle 机制与 OAuth 并不完全相同。

您的链接是正确设置捆绑包的最佳信息。

我正在使用 MongoDB 来存储所有 4 个必需的文档:Client、AuthCode、RefreshToken 和 AccessToken

称为“创建新客户端”的步骤基本上是 OAuth 的 FOSUserBundle 的“注册”过程。

OAuth 将使用客户端授予访问权限。

OAuth 的主要思想是保护 API,因此我建议您将配置切换为匿名:false

然后你会看到消息:

{"error":"access_denied","error_description":"OAuth2 authentication required"}

当你调用你的 API

OAuth 的想法是获取访问令牌来调用您的 API。阅读:http ://blog.tankist.de/blog/2013/07/16/oauth2-explained-part-1-principles-and-terminology/

这是需要遵循 OAuth 身份验证过程的时候。

有5种基本方法可以使用:

const GRANT_TYPE_AUTH_CODE = 'authorization_code';
const GRANT_TYPE_IMPLICIT = 'token';
const GRANT_TYPE_USER_CREDENTIALS = 'password';
const GRANT_TYPE_CLIENT_CREDENTIALS = 'client_credentials';
const GRANT_TYPE_REFRESH_TOKEN = 'refresh_token';

要了解每个,请查找有关 OAuth RFC 的更多文档。

它们中的每一个都对应一个特定的调用: /oauth/v2/token?client_id=[CLIENT_ID]&response_type=code&redirect_uri=URL&grant_type=token

参考:https ://github.com/FriendsOfSymfony/oauth2-php/blob/master/lib/OAuth2/OAuth2.php#L182

另请阅读此链接: blog.tankist.de/blog/2013/08/20/oauth2-explained-part-4-implementing-custom-grant-type-symfony2-fosoauthserverbundle/

“测试时间”部分解释了如何使用 OAuth。

我还在努力。

希望能帮助到你。


此链接还指示如何使用 FOSUserBundle User 和 UserManager 可能使用密码 grant_type :如果您正在验证用户,请不要忘记设置用户提供程序。

这是使用 FOSUserBundle 用户提供程序的示例: https ://github.com/FriendsOfSymfony/FOSOAuthServerBundle/blob/master/Resources/doc/index.md

# app/config/config.yml
fos_oauth_server:
    ...

    service:
        user_provider: fos_user.user_manager
于 2014-02-05T12:54:19.223 回答