3

我使用 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="")

在标题中有:

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

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

现在我猜测config.yml或 有什么问题security.yml。以下是一些选定的部分:

配置.yml:

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

安全性.yml:

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

1 回答 1

3

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

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

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

于 2015-12-03T10:48:47.793 回答