使用基本身份验证时如何识别用户?

时间:2014-05-14 14:56:16

标签: symfony authentication curl fosrestbundle

我在FOSRestBundle中使用基本身份验证。通过CURL命令检查我的API表明它运行正常。

但是现在,我想知道执行POST操作的用户(想象一下,用户想要对某些内容发表评论!谁是该用户?)。实际上,正如您在下面的CURL命令中所看到的,用户和他的密码都是用base64编码的。

# POST
curl -v -H "Accept: application/json" \
 -H "Content-type: application/json" \
 -H "Authorization: Basic YW1pbmU6c3RpZ21hdGFn" \
 -X POST -d '{"test":{"x":"Amine","y":"Jallouli"}}'
 http://localhost/tuto/web/app_dev.php/api/v1/test/add

有什么建议吗?

仅供参考,这是我在security.yml中的配置

firewalls:
    #.....
    rest_api:
        pattern: ^/api/
        stateless: true
        http_basic:
            provider: fos_userbundle

1 个答案:

答案 0 :(得分:2)

您应该使用此@amine

$user = $this->get('security.context')->getToken()->getUser();