Symfony FOSOAuthServerBundle:未检测到访问令牌

Symfony FOSOAuthServerBundle: access token not detected

我正在使用 FOSOAuthServerBundle 作为我的 oauth 端点。我使用 Resource Owner Password Credentials 授予方法成功生成了令牌:

{
    "access_token": "MY-FOO-TOKEN",
    "expires_in": ​3600,
    "token_type": "bearer",
    "scope": "read",
    "refresh_token": "MY-BAR-REFRESH-TOKEN"
}

现在我想用它来获取一些受保护的资源。所以我做了:

curl -X GET -H "Authorization: Bearer MY-FOO-TOKEN" "http://localhost:8000/api/a-bar-resource"

Bearer 似乎没有被检测到。


信息:

echo $this->get('security.token_storage')->getToken(); 给出: AnonymousToken(user="anon.", authenticated=true, roles="")

在headers中有:

["authorization"]=> /** <-- Is the lowercase OK? **/
  array(1) {
    [0]=>
    string(93) "Bearer MY-FOO-TOKEN"
  }

我也尝试将 access_token 作为查询参数传递,但没有成功。

现在我猜 config.ymlsecurity.yml 有问题。以下是部分精选内容:

config.yml:

fos_oauth_server:
[...]
    service:
        options:
            supported_scopes: read
        user_provider: fos_user.user_provider.username_email

security.yml:

security:
[...]
    firewalls:
        api:
            pattern:    ^/api
            fos_oauth:  true
            stateless:  true
            anonymous:  false
    access_control:
            - { path: ^/api, roles: [ IS_AUTHENTICATED_ANONYMOUSLY ] }

我终于找到了导致问题的原因。由于相同的模式,我有一个防火墙使另一个防火墙无效:

security:
[...]
    firewalls:
        other_firewall: #this one
            pattern:    ^/
            anonymous:  true
        api:
            pattern:    ^/api
            fos_oauth:  true
            stateless:  true
            anonymous:  true

我还认为在一个防火墙中进行的身份验证在另一个防火墙中不可用,因此将防火墙的数量减少到严格的最低限度(一个不安全,一个具有身份验证等)是一个很好的做法。然后使用access_control进行精细访问。