Security 如何restfully登录、Symfony2安全、FOSUserBundle、FOSRestBundle?

Security 如何restfully登录、Symfony2安全、FOSUserBundle、FOSRestBundle?,security,symfony,login,fosuserbundle,Security,Symfony,Login,Fosuserbundle,我希望能够通过ws登录 我试图用curl指向/login来模拟这种情况,但它只处理HTML等。 顺便说一句,它需要一个我不想要的CSRF 因此,我想禁用CRSF(从登录检查),或者自己找到一种方法 我可以覆盖LoginListener(它在哪里?)吗?当捕获路由login\u check时会使用它 有什么线索吗?您不应该使用CURL通过web服务对用户进行身份验证 看看ResettingController.php(在FOSUserBundle/Controller中)和LoginManager

我希望能够通过ws登录

我试图用curl指向
/login
来模拟这种情况,但它只处理HTML等。 顺便说一句,它需要一个我不想要的CSRF

因此,我想禁用CRSF(从
登录检查
),或者自己找到一种方法

我可以覆盖LoginListener(它在哪里?)吗?当捕获路由
login\u check
时会使用它


有什么线索吗?

您不应该使用CURL通过web服务对用户进行身份验证

看看ResettingController.php(在FOSUserBundle/Controller中)和LoginManager.php(在Security中),下面是一个如何使用Symfony安全性对用户进行身份验证的示例:

Controller/ResettingController.php

    /**
 * Authenticate a user with Symfony Security
 *
 * @param \FOS\UserBundle\Model\UserInterface        $user
 * @param \Symfony\Component\HttpFoundation\Response $response
 */
protected function authenticateUser(UserInterface $user, Response $response)
{
    try {
        $this->container->get('fos_user.security.login_manager')->loginUser(
            $this->container->getParameter('fos_user.firewall_name'),
            $user,
            $response);
    } catch (AccountStatusException $ex) {
        // We simply do not authenticate users which do not pass the user
        // checker (not enabled, expired, etc.).
    }
}
在Security/LoginManager.php中

    final public function loginUser($firewallName, UserInterface $user, Response $response = null)
{
    $this->userChecker->checkPostAuth($user);

    $token = $this->createToken($firewallName, $user);

    if ($this->container->isScopeActive('request')) {
        $this->sessionStrategy->onAuthentication($this->container->get('request'), $token);

        if (null !== $response) {
            $rememberMeServices = null;
            if ($this->container->has('security.authentication.rememberme.services.persistent.'.$firewallName)) {
                $rememberMeServices = $this->container->get('security.authentication.rememberme.services.persistent.'.$firewallName);
            } elseif ($this->container->has('security.authentication.rememberme.services.simplehash.'.$firewallName)) {
                $rememberMeServices = $this->container->get('security.authentication.rememberme.services.simplehash.'.$firewallName);
            }

            if ($rememberMeServices instanceof RememberMeServicesInterface) {
                $rememberMeServices->loginSuccess($this->container->get('request'), $response, $token);
            }
        }
    }

    $this->securityContext->setToken($token);
}

有许多方法可以为RESTWeb服务提供身份验证和授权,但最被接受的方法似乎是。Facebook、Twitter、谷歌、Github等都在使用它

Symfony之友的人有一个捆绑包,可以在Symfony2上实现OAuth身份验证和授权:我想这就是您想要的

编辑:有关Oauth的更多信息,Cloudfoundry的人员在几天前发布了一条有趣的消息

关于您可以使用的其他选项,一个简单的选项是基本身份验证:

firewalls:
    main:         
        pattern: ^/rest
        anonymous: ~
        form_login: false            
        provider: fos_user_bundle
        http_basic:
            realm: "REST Service Realm"

EDIT2:我看到仍然有人投票支持这个答案,我认为需要注意的是,在撰写这个答案时,JWT还不是一个选项,但在某些用例中(例如,当API将被您自己的应用程序使用时),它可能是比OAuth更好的选项。因此,这里有一个指向Symfony2/3的良好JWT实现的链接:

Oauth仅在您创建API供第三方根据您的API进行身份验证时才是必需的。如果他的API只被他的应用程序使用,oAuth就过了头,http basic over SSL就足够了。@thenetimp哦,这不是100%正确的。虽然我同意在很多情况下,SSL上的基本身份验证就足够了(这就是我添加它的原因),但ouath不仅在第三方使用您的API时有用,而且在您自己的应用程序(移动、web等)使用它时也有用(请参阅不同的授权类型)。另一种方法是使用身份验证令牌。不要在会话中使用JWT或OAuth。正如@thenetimp所说,这些技术是为第三方身份验证或单点登录而设计的。看见