2

symfony2 및 FOSRestBundle에서 제공하는 REST 웹 서비스에 액세스하려고합니다. HTTP 기본 인증을 사용합니다. 불행히도, 내가 어떻게 알아낼 수 없습니다, HTTP 코드 401 및 403 다시, 인증 물건 (올바른) 제공되지 않은 경우.symfony2/FOSRestBundle : HTTP 상태 코드 없음

이 내가 JQuery와/AJAX와 서비스를 호출하는 방법입니다

$.ajax({ 
       url: url + "/api/places.json", 
       type: 'GET', 
       data: {data}, 
       dataType: "json", 
       beforeSend: function(xhr) { 
        xhr.setRequestHeader('Authorization', auth); 
       } 
      }).done(function(data, textStatus, xhr) { 
       console.log('JSONAdapter.findPlacesByUser '+xhr.status); 
       callback(data); 
      }).fail(function(xhr, err) { 
       console.log('JSONAdapter.findPlacesByUser ERROR '+err+': '+xhr.status+' '+xhr.responseText); 
      }); 

어느 쪽도 '완료'도 내가 잘못 또는 없음 인증 데이터를 제공 할 때 '실패'기능이 실행됩니다. 심지어 (내가 위의 AJAX 호출을 경우와 같은 파일)이 물건

내가 어떤 오류 메시지 또는 상태 코드를 얻을 수 없습니다

$(document).ajaxError(function(event, jqxhr, settings, exception) { 
     console.log('ajaxError'); 
     if(jqxhr.status == 403) 
     { 
      console.log('Not authorized'); 
     } 
    }); 

이 symfonys 설정에서 내 FOSRestBundle 구성입니다. YML :

twig: 
debug:   %kernel.debug% 
strict_variables: %kernel.debug% 
exception_controller: 'FOS\RestBundle\Controller\ExceptionController::showAction' 

fos_rest: 
view: 
    view_response_listener: 'force' 
    failed_validation: HTTP_BAD_REQUEST 
    default_engine: php 
    formats: 
     json: true 
     xml: false 
     rss: false 
format_listener: 
    prefer_extension: true 
body_listener: 
    decoders: 
     json: fos_rest.decoder.json 
param_fetcher_listener: true 
allowed_methods_listener: true 
access_denied_listener: 
    json: true 
exception: 
    codes: 
     'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404 
     'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT 
     'Symfony\Component\Security\Core\Exception\AuthenticationException': 401 
     'Symfony\Component\Security\Core\Exception\AccessDeniedException': 403 

security.yml :

jms_security_extra: 
    secure_all_services: false 
    expressions: true 
security: 
    encoders: 
     XXXX\UserBundle\Entity\User: 
      algorithm: sha1 
      iterations: 1 
      encode_as_base64: false 
    providers: 
     api_users: 
      entity: { class: XXXXUserBundle:User, property: email } 
    firewalls: 
     rest_webservice: 
      pattern: ^/api 
      stateless: true 
      http_basic: 
       provider: api_users 
       realm: "XXXX API" 

내가 시도, 당신이 볼 추측 일부 물건 (예 : 마지막 두 줄). 하지만 지금 당황스럽고 디버깅을 시작할 위치를 모른다.

인증이 필요하거나 실패 할 때 HTTP 상태 코드를 수신하려면 어떻게해야합니까?

어떤 도움

은 config.yml

exception: 
     codes: 
      'acme\RestBundle\Exceptions\RestAuthenticationFailedException' : 403 

사용 Restbundle 인증 예외 핸들러를 변경하여 보시기 바랍니다

+0

security.yml을 추가 할 수 있습니까? –

+0

security.yml added :-) – Nicki

답변

0

:-) 감사하겠습니다. 이것은 나를 위해 일하고있다. 그리고 REST 컨트롤러에서 \acme\RestBundle\Exceptions\RestAuthenticationFailedException()을 던져야합니다.

+0

사용자/요청의 비인가 때문에 내 컨트롤러에 액세스하지 못했습니다. '[2013-05-06] security.INFO : "[email protected]"사용자를위한 기본 인증 권한 헤더가 있음 [2013-05-06] doctrine.DEBUG : SELECT t0.id AS id1, t0.username AS username2, t0.slug AS slug3, t0.salt AS salt4, t0.password AS 암호 5, t0.email AS email6, t0.is_active AS is_active7, t0.created_at AS created_at8 사용자 t0 WHERE t0.email =? LIMIT 1 [ "[email protected]"] [] [2013-05-06] security.INFO : "[email protected]"사용자의 인증 요청 실패 : 잘못된 자격 증명 [] [] – Nicki

+1

FOSRestBundle이 실패한 인증 요청을 처리하고 적절한 HTTP 상태 코드를 전송합니다. – Nicki