簡體   English   中英

具有RESTful身份驗證的Symfony2 App,使用FOSRestBundle和FOSUserBundle

[英]Symfony2 App with RESTful authentication, using FOSRestBundle and FOSUserBundle

我正在為我的JS驅動的應用程序制作REST API。

在登錄期間,登錄表單通過AJAX提交到我的API的url /rest/login

  • 如果登錄成功,則返回204
  • 如果失敗,則返回401

雖然我已經為API和應用程序本身分離了防火牆,但它們共享相同的上下文,這應該意味着,當用戶對API進行身份驗證時,他也會針對應用程序進行身份驗證。 因此,當服務器返回204時,頁面將重新加載,它應該將用戶重定向到應用程序,因為他現在已登錄。

我嘗試使用FOSUserBundle的預制check_login頁面並指向/rest/login

login:
    path: /rest/login
    defaults:
        _controller: FOSUserBundle:Security:check
    methods: [ POST ]

這不起作用,因為它總是返回重定向,無論如何。 我閱讀了symfony的文檔,但無法找到如何創建自定義check_login頁面。 我需要的是這樣的事情

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();
        }
    }
}

我不知道該怎么做。 我在任何文檔中找不到任何幫助我的東西。 我將感謝能夠指出正確方向的任何建議。


編輯:為了進一步簡化,我的目標是什么。 我希望我的登錄功能與普通的form_login完全相同 我只想改變它發回的響應 - 而不是重定向我想成功204和失敗401。

我找到了一個簡單的解決方案。 我只需要編寫一個實現AuthenticationSuccessHandlerInterfaceAuthenticationFailureHandlerInterface

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

問題解決了,無需復雜的身份驗證提供程序:-)

我理解你的問題,因為我通過類似的情況,但使用SOAP服務。 在中間,我可以重新搜索安全性wsse,Symfony2已經提供了解決方案

http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html

它使用真實令牌,您可以在FOSUserBundle中與用戶匹配。 我認為唯一的想法是你要比較的字段“密碼”與數據庫中的字段(加密)相同,然后我決定只為此用途創建一個額外的字段。

我希望它對你有所幫助。

問候

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM