Php 具有RESTful身份验证的Symfony2应用程序,使用FOSRestBundle和FOSUserBundle

Php 具有RESTful身份验证的Symfony2应用程序,使用FOSRestBundle和FOSUserBundle,php,symfony,fosuserbundle,restful-authentication,fosrestbundle,Php,Symfony,Fosuserbundle,Restful Authentication,Fosrestbundle,我正在为我的JS驱动的应用程序制作RESTAPI 在登录期间,登录表单通过AJAX提交到MyAPI的url/rest/login 如果登录成功,则返回204 如果失败,则返回401 虽然我已经为API和应用程序本身分离了防火墙,但它们共享相同的上下文,这意味着当用户针对API进行身份验证时,他也针对应用程序进行了身份验证。所以,当服务器返回204时,页面将重新加载,并将用户重定向到应用程序,因为他现在已登录 我试着使用FOSUserBundle的pre-madecheck\u login页面

我正在为我的JS驱动的应用程序制作RESTAPI

在登录期间,登录表单通过AJAX提交到MyAPI的url
/rest/login

  • 如果登录成功,则返回204
  • 如果失败,则返回401
虽然我已经为API和应用程序本身分离了防火墙,但它们共享相同的上下文,这意味着当用户针对API进行身份验证时,他也针对应用程序进行了身份验证。所以,当服务器返回204时,页面将重新加载,并将用户重定向到应用程序,因为他现在已登录

我试着使用FOSUserBundle的pre-made
check\u login
页面,并指向那里的
/rest/login

login:
    path: /rest/login
    defaults:
        _controller: FOSUserBundle:Security:check
    methods: [ POST ]
这不起作用,因为不管发生什么,它总是返回重定向。我阅读了symfony的文档,但找不到如何自定义
check\u登录
页面。我需要的是这样的东西

use Symfony\Component\Security\Core\Exception\AuthenticationException;
use FOS\RestBundle\Controller\Annotations\View;    

class SecurityController {

    /**
     * @View(statusCode=204)
     */
    public function loginAction($username, $password) {

        /* first I need to somehow authenticate user 
           using normal authentication, that I've set up */
        ...

        /* Then I need to return 204 or throw exception,
           based on result.
           This is done using FOSRestBundle and it's
           listeners. */

        if(!$succesful) {
            throw new AuthenticationException();
        }
    }
}
我不知道该怎么做。我在任何文档中都找不到任何帮助。我将感谢任何能为我指明正确方向的建议



编辑:为了进一步简化,我的目标是。我希望我的登录功能与普通表单登录完全相同。我只想更改它发回的响应,而不是重定向,我希望成功时为204,失败时为401。

我理解您的问题,因为我遇到了类似的情况,但使用了SOAP服务。在中间,我可以重新搜索安全性WSSE和Syfyy2已经提供了解决方案

它与真实的令牌一起工作,您可以与FOSUserBundle中的用户进行匹配。我看到的唯一一个想法是,您要比较的字段“password”与数据库中的字段“password”相同(使用加密),然后我决定只为此用途创建一个额外字段

我希望它能帮助你


问候

我找到了一个简单的解决办法。我只需要编写一个类,它实现了
AuthenticationSuccessHandlerInterface
AuthenticationFailureHandlerInterface

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;

class AuthenticationRestHandler implements AuthenticationSuccessHandlerInterface, AuthenticationFailureHandlerInterface {

    public function onAuthenticationFailure(Request $request, AuthenticationException $exception) {
        return new Response('', Response::HTTP_UNAUTHORIZED);
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token) {
        return new Response('', Response::HTTP_NO_CONTENT);
    }
}
然后我将其注册为服务,并配置为防火墙的处理程序

services:
  security.authentication_rest_handler:
    class: AuthenticationRestHandler

security:
  firewalls:
    rest:
      pattern: ^rest
      context: app
      form_login:
        check_path: /rest/login
        provider: fos_userbundle
        failure_handler: inspireon.security.authentication_rest_handler
        success_handler: inspireon.security.authentication_rest_handler
        username_parameter: login
        password_parameter: password

问题解决了,不需要复杂的身份验证提供程序:-)

那么,我需要实现全新的自定义身份验证提供程序吗?在我看来,这有点过分了,因为我需要改变的只是侦听器发送204/401的行为,而不是重定向,而不是整个过程。