简体   繁体   English

在json中的Symfony2登录/注销响应

[英]Symfony2 login/logout response in json

A desktop application interacts with my website running on symfony2 and requires response in json, beginning with login success/failure. 桌面应用程序与我在symfony2上运行的网站交互,需要在json中进行响应,从登录成功/失败开始。 Using http_basic and providers if the login is correct everything works fine, but Im not able to send the json response. 如果登录正确,使用http_basic和提供程序一切正常,但我无法发送json响应。 And if the login is incorrect it re-prompts the username and password dialog. 如果登录不正确,则会重新提示用户名和密码对话框。 How can I stop at the first failure attempt and send the json response 如何在第一次失败尝试时停止并发送json响应

{
'message':'invalid password/username'
}

security.yml security.yml

 app:
            pattern:   ^/app.*
            logout: true 
            security: true
            http_basic:             
              provider: webservice  
providers:
        fos_userbundle:
            id: fos_user.user_manager

        webservice:
            id: my_provider

config.yml config.yml

my_provider:      
        parent: doctrine.odm.mongodb.security.user.provider
        arguments: [ Se\PPBundle\Document\User, username]

tl;dr I want to respond with json on login. tl; dr我想在登录时回复json。

From the looks of it you are using the FOS User bundle to handle your authentication. 从它的外观来看,您正在使用FOS用户包来处理您的身份验证。 If this is the case you will need to overload the FOS Security controller login action. 如果是这种情况,您将需要重载FOS安全控制器登录操作。 Change the logic in there to send a difference response on fail instead of just rendering the view with the errors. 更改其中的逻辑以在失败时发送差异响应,而不是仅使用错误呈现视图。 You will also need to customize the routing for the login. 您还需要自定义登录路由。 Something to the effect of: 有效的东西:

_login:
    pattern: /login
    defaults: {_controller: "FOSUserBundle:Security:login", _format:json}

Note the _format:json this will return your responses as "application/json" you will still need to json_encode the return 注意_format:json这将返回你的响应为“application / json”你还需要json_encode返回

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM