Symfony FOSOAuthServerBundle:未检测到访问令牌

时间:2015-11-12 22:56:26

标签: symfony oauth fosoauthserverbundle

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

{
    "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"

似乎没有检测到持票人。

INFOS:

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

标题中有:

["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 ] }

1 个答案:

答案 0 :(得分:3)

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

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

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

相关问题