2

我想訪問由symfony2和FOSRestBundle提供的REST web服務。我使用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配置。陽明:

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狀態碼?

任何幫助:-)

+0

你可以添加你的security.yml嗎? – 2013-05-06 10:41:05

+0

security.yml added :-) – Nicki 2013-05-06 14:22:53

回答

0

理解,通過改變你的config.yml

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

使用Restbundle驗證異常處理程序請試試這個。 這是爲我工作。你必須從你的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 password5,t0.email AS email6,t0.is_active AS is_active7,t0.created_at AS created_at8 FROM user t0 WHERE t0.email =? LIMIT 1 [「[email protected]」] [] [2013-05-06] security.INFO:用戶「[email protected]」的身份驗證請求失敗:憑據錯誤[] [] ' – Nicki 2013-05-06 21:12:20

+1

不應該FOSRestBundle處理這個失敗的身份驗證請求併發送正確的HTTP狀態碼? – Nicki 2013-05-06 21:13:50