5

내 JS 기반 앱용 REST API를 만들고 있습니다.FOSRestBundle 및 FOSUserBundle을 사용하는 RESTful 인증을 사용하는 Symfony2 앱

로그인하는 동안 로그인 양식이 AJAX를 통해 내 API의 url /rest/login으로 제출됩니다. 로그인이 성공적인 경우 실패하면

  • , 그것은 내가 API 및 응용 프로그램 자체에 대한 방화벽을 분리했지만이 401

를 반환, 204

  • 를 반환, 그들은 같은 컨텍스트를 공유하는 사용자가 API에 대해 인증 할 때도 앱에 대해 인증되었음을 의미해야합니다. 그래서 서버가 반환 할 때, 204 페이지가 다시로드되고 그가 지금 로그인 있기 때문에 그것은 응용 프로그램에 사용자를 리디렉션합니다.

    나는 FOSUserBundle의 미리 만들어진 check_login 페이지를 사용하기 위해 노력하고있다 /rest/login을 지적했다.

    리디렉션이 항상 반환되기 때문에 작동하지 않습니다. 심포니에 대한 설명서를 읽었으며 사용자 정의 check_login 페이지를 만드는 방법을 찾을 수 없습니다. 내가 필요로하는 것은 이런 것입니다.

    실마리가 없습니다. 모든 문서에서 찾은 어떤 것도 나를 도왔습니다. 나는 올바른 방향으로 나를 가리켜 줄 제안에 대해 감사 할 것이다.


    편집 : 추가로 단순화하려면 무엇을 목표로하는지. 내 로그인이 보통 form_login과 똑같이 작동하도록하고 싶습니다. 응답 만 변경하고 싶습니다. 리디렉션 대신 성공 204, 실패 401을 원합니다.

  • 답변

    12

    간단한 해결책을 찾을 수있었습니다. 나는 단지 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 
    

    문제를 해결하고 복잡한 인증 공급자 그래서

    1

    나는 비슷한 상황이지만 SOAP 서비스를 통과했기 때문에 문제를 이해했습니다. 중간에 나는 보안 WSSE를 다시 검색 할 수 있고 Symfony2 이미 그것은 실제 토큰으로 작동하는 솔루션

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

    을 제공하고 당신은 FOSUserBundle의 사용자와 일치시킬 수 있습니다. 유일한 생각은 당신이 비교하고자하는 "암호"필드가 데이터베이스에서 (암호화 된) 것과 동일하다는 것입니다. 그런 다음이 용도로만 추가 필드를 생성하기로 결정했습니다.

    도움이되기를 바랍니다.

    인사말

    +0

    :-) 필요하지, 나는 완전히 새로운 사용자 지정 인증 공급자를 구현해야합니까?조금이라도 지나치다. 내가 바꾸어야 할 것은 전체 프로세스가 아닌 리다이렉트 대신 204/401을 보내는 청취자의 행동이다. –