如何将FOSUserBundle配置为启用FOSOAuthServerBundle的服务器的身份验证提供程序

cax*_*ton 13 php symfony oauth-2.0 fosuserbundle

我正在尝试设置FOSUserBundle为我FOSOAuthServerBundle启用的服务器的身份验证提供程序.在FOSOAuthServerBundle已经工作了我之前正确试图实现FOSUserBundle,我也有过FOSUserBundle工作没有FOSOAuthServerBundle,但我不能让他们一起工作.

我的问题是应该oauth_authorizesecurity.yml下面的:firewall中为身份验证提供程序指定什么?

# app/config/security.yml
security:
    providers:
    fos_userbundle:
        id: fos_user.user_provider.username

    encoders:
        FOS\UserBundle\Model\UserInterface: sha512

    firewalls:
        oauth_token:
            pattern:    ^/oauth/v2/token
            security:   false

        oauth_authorize:
            pattern:    ^/oauth/v2/auth

            # WHAT GOES HERE?

        api:
            pattern:    ^/api
            fos_oauth:  true
            stateless:  true

    access_control:
        - { path: ^/api, roles: [ IS_AUTHENTICATED_FULLY ] }
Run Code Online (Sandbox Code Playgroud)

我试图验证用户而不是客户端.

非常感谢.

小智 1

您的 config.yml 中应该包含以下内容

fos_user:
   db_driver: orm
   firewall_name: main
   user_class: Zoef\UserBundle\Entity\Userere
Run Code Online (Sandbox Code Playgroud)

对于 oauth 服务器来说是这样的

fos_oauth_server:
    db_driver: orm
    client_class:        {PATH TO ENTITY}\Client
    access_token_class:  {PATH TO ENTITY}\AccessToken
    refresh_token_class: {PATH TO ENTITY}\RefreshToken
    auth_code_class:     {PATH TO ENTITY}\AuthCode
        service:
            user_provider: fos_user.user_provider.username
Run Code Online (Sandbox Code Playgroud)

security.yml 应该如下所示:

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512
    providers:
        fos_userbundle:
            id: fos_user.user_provider.username
    firewalls:
        oauth_token:
            pattern:    ^/oauth/v2/token
            security:   false

    access_control:
    - { path: ^/, roles: ROLE_ADMIN }
Run Code Online (Sandbox Code Playgroud)

您可以测试它是否有效,因为当您到达任何网址时,您应该得到如下响应:

{"error":"access_denied","error_description":"需要 OAuth2 身份验证"}