Symfony2 基本 http 身份验证,可与 curl 配合使用,但不适用于 Android
我正在尝试对我的 symfony2 服务器上的用户进行身份验证。 身份验证可在 cli 中使用curl 进行:
curl "http://localhost:8080/app_dev.php/app/user/profile.json?id=4" -u foo:bar --basic
但是使用 Android 时,我总是收到 401 错误并显示此错误:
A Token was not found in the SecurityContext.
我能做些什么来修复它?
这是我的 security.yml:
security:
providers:
fos_userbundle:
id: fos_user.user_manager
encoders:
BumpMe\UserBundle\Entity\User: sha512
firewalls:
app:
pattern: ^/app/.*
stateless: true
security: true
http_basic:
realm: "API"
main:
pattern: ^/
form_login:
provider: fos_userbundle
logout: true
anonymous: true
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: ROLE_ADMIN }
- { path: ^/app/, role: ROLE_USER }
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
另外,我如何以 base64 发送用户名和密码?因为目前在 Android 上它不希望使用 base64 并且curl 在 base64 上发送它...
我不明白这个错误...
I'm trying to authenticate a user on my symfony2 server.
The authentication works with curl in cli :
curl "http://localhost:8080/app_dev.php/app/user/profile.json?id=4" -u foo:bar --basic
But with Android i always get a 401 error with this error:
A Token was not found in the SecurityContext.
What can I do to fix it ?
Here is my security.yml:
security:
providers:
fos_userbundle:
id: fos_user.user_manager
encoders:
BumpMe\UserBundle\Entity\User: sha512
firewalls:
app:
pattern: ^/app/.*
stateless: true
security: true
http_basic:
realm: "API"
main:
pattern: ^/
form_login:
provider: fos_userbundle
logout: true
anonymous: true
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: ROLE_ADMIN }
- { path: ^/app/, role: ROLE_USER }
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
Also, how can i send the username and the password in base64 ? Because currently on Android it doesn't want the base64 and curl sent it on base64...
I don't understand this error...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,我发现了...
这只是因为 Symfony 想要使用 Base64 编码。
Base64 编码在 Android 上不起作用,因为该方法在编码数据后添加了一个新行,因此,它不一样......
通过简单的修剪就可以了:
我认为它会有所帮助。
Sorry i find out...
It's just because Symfony wanted a base64 encoded.
The base64 encoding didn't work on Android because, the method add a new line after the encoded datas so, it's not the same...
With a simple trim it's fine :
I think it can be helpful.