【问题标题】:FOSOAuthServerBundle with FOSUserBundle - How to make it works?带有 FOSUserBundle 的 FOSOAuthServerBundle - 如何使其工作?
【发布时间】:2014-01-27 20:20:33
【问题描述】:

目前我的项目运行良好。我使用 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,那将非常有帮助。

【问题讨论】:

  • 你能让它工作吗?您能否发布工作配置,它可能对我和其他人有所帮助?谢谢
  • @manonthemoon 能否请您提供配置以便其他人使用?这对我也很有帮助。

标签: php symfony oauth fosuserbundle fosoauthserverbundle


【解决方案1】:

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

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

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

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

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

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

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 用户和 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

【讨论】:

  • 另见:developers.google.com/accounts/docs/OAuth2WebServer 对于 OAuth 流程,有一个很好的解释可以解释另外一个完美的链接来解释这 4 种不同的方法:aaronparecki.com/articles/2012/07/29/1/oauth2-simplified
  • "“创建新客户端”这一步基本上是 FOSUserBundle 用于 OAuth 的“注册”过程。"它不应该是“login_check 之后的最后一步???如果登录已注册...我允许他访问,所以我创建了一个新客户端,它创建了一个新的访问令牌...否?
  • 到目前为止,我的项目一切正常。从技术上讲,您可以随时创建客户端对象,只要您不使用客户端生成令牌,它就不会链接到用户,一旦使用客户端凭据创建令牌,客户端将生成链接到的 FOSUser 令牌用户。
  • 我在您的个人资料中看到您来自巴黎。我是法国人,也来自巴黎。您认为我们可以通过邮件或 Skype 讨论 FOSOAuthServerBundle 吗?因为我仍然很难集成这个捆绑包。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-02
  • 2018-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-03
  • 1970-01-01
相关资源
最近更新 更多