我使用 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 ] }