FOSRestBundle在我的项目中完美运行但没有身份验证。现在,我的目标是通过身份验证发出请求。
为此,我在security.yml
中添加了此防火墙firewalls:
# ...
main:
pattern: ^/
form_login:
provider: fos_userbundle
remember_me: true
login_path: /login
check_path: /login_check
default_target_path: minn_ads_default_index
csrf_provider: form.csrf_provider
remember_me:
key: %secret%
rest_api:
pattern: ^/api/
stateless: true
http_basic:
provider: fos_userbundle
# ...
access_control:
# ...
- { path: ^/api/, role: IS_AUTHENTICATED_FULLY }
要检查curl命令中的身份验证,我尝试了以下命令:
curl -i \
-H 'Accept: application/json' \
-H 'Authorization:Basic dG9uaXZkdjoxMjM0' \
http://localhost/tuto/app_dev.php/api/test/1
其中dG9uaXZkdjoxMjM0 = encode_base64('tonivdv:1234')
结果是:
HTTP/1.1 302 Found
Date: Fri, 11 Apr 2014 13:56:08 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.4.9-4ubuntu2.4
Set-Cookie: PHPSESSID=4dtr168vmj1eg523a07kbkjkh1; path=/
Cache-Control: no-cache
Location: http://localhost/tuto/web/app_dev.php/login
Vary: Accept-Language
X-Debug-Token: 220df7
Transfer-Encoding: chunked
Content-Type: application/json
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="1;url=http://localhost/tuto/web/app_dev.php/login" />
<title>Redirecting to http://localhost/tuto/web/app_dev.php/login</title>
</head>
<body>
Redirecting to <a href="http://localhost/tuto/web/app_dev.php/login">http://localhost/tuto/web/app_dev.php/login</a>.
</body>
</html>
所以我想知道FOSUserBundle 的CSRF令牌是否是无法在CURL命令中进行身份验证的原因?
答案 0 :(得分:0)
我找到了解决方案。
我只需要在rest_api
防火墙之前定义main
防火墙。
答案 1 :(得分:0)
好吧,你的rest_api防火墙永远不会被使用,因为它是在主防火墙之后总是匹配的。这意味着您的API通过有状态的form_login身份验证而不是通过http_basic来保护。
firewalls:
rest_api:
pattern: ^/api/
stateless: true
http_basic:
provider: fos_userbundle
main:
pattern: ^/
form_login:
provider: fos_userbundle
remember_me: true
login_path: /login
check_path: /login_check
default_target_path: minn_ads_default_index
csrf_provider: form.csrf_provider
remember_me:
key: %secret%
access_control:
# ...
- { path: ^/api/, role: IS_AUTHENTICATED_FULLY }