2017-07-10 7 views
0
私のsymfonyのAPIのウィッヒリターンJSONからの角度で件のデータを取得しようとしているときに、私は現在、エラーが直面しています

:ブロックされたクロスオリジン・リクエストのSymfony/AngularJS

「クロスオリジン・リクエスト:同一生成元ポリシーを読んで許可していませんリモートリソースはhttp://localhost:8000/customersです(理由:CORSヘッダー 'Access-Control-Allow-Origin'がありません)。ここ

は完全な結果のスクリーンショットです:enter image description here

私が質問を知っているが尋ね複数回だったが、私は働いて答えを見つけることができませんでした。

しかし、それはない、私はそれが動作APIコントローラで$セッションを取得しようとしていないと私は必要なすべての件のデータを取得し、ここに私のAPIコントローラは次のとおりです。

/** 
* @Rest\View(statusCode=Response::HTTP_CREATED) 
* @Rest\Get("/customers") 
*/ 

/** 
* @Rest\View(statusCode=Response::HTTP_CREATED) 
* @Rest\Get("/index") 
*/ 
public function getIndexAction(Request $request) 
{ 
    $loginad = $this->getUser()->getUsername(); 

    $nom_ad = "******"; 
    $port_ad = ******; 
    $compte_ad = "*******"; 
    $password_ad = "******"; 
    //parcours de l'AD 
    // Connexion LDAP 
    $ldapconn = ldap_connect($nom_ad, $port_ad) 
    or die("Impossible de se connecter au serveur LDAP $nom_ad"); 
    if ($ldapconn){ 
     $ldapbind = ldap_bind($ldapconn, $compte_ad, $password_ad) 
     or die("Impossible de se binder au serveur LDAP $nom_ad"); 
     if($ldapbind){ 
      $employeeID = false; 
      $dn = "OU=CER35,DC=CeRNum,DC=dom"; 
      $filter="(samAccountName=$loginad)"; 
      $justtheseattributes = array("ou", "sn", "givenname", "mail", "employeeid", "samaccountname"); 
      $sr=ldap_search($ldapconn, $dn, $filter, $justtheseattributes); 
      $info = ldap_get_entries($ldapconn, $sr); 
      for ($i=0;$i<$info["count"];$i++) { 
       $employeeID = $info[$i]["employeeid"][0]; 
      } 
      if (!$employeeID) { 
       $dn = "OU=CER56,DC=CeRNum,DC=dom"; 
       $filter="(samAccountName=$loginad)"; 
       $justtheseattributes = array("ou", "sn", "givenname", "mail", "employeeid", "samaccountname"); 
       $sr=ldap_search($ldapconn, $dn, $filter, $justtheseattributes); 
       $info = ldap_get_entries($ldapconn, $sr); 
       for ($i=0;$i<$info["count"];$i++) { 
        $employeeID = $info[$i]["employeeid"][0]; 
       } 
      } 
     } 
    } 

    $agent = $this->get('doctrine') 
     ->getRepository('CERAgentBundle:Agent', 'agent') 
     ->findByCode($employeeID); 

    $session = new Session(); 
    $session->set('agent', $agent); 

    $formatted = [ 
     'civilite' => $agent[0]->getCivilite(), 
     'prenom' => $agent[0]->getPrenom(), 
     'nom' => $agent[0]->getNom() 
     ]; 

    return new JsonResponse($formatted); 
} 

ので、私が "localhost:8000/index"を呼び出すと、casサーバーの認証のためのバンドルもhttps urlを呼び出し、ユーザーはイントラネットの会社に自分自身を認証することができるので、最終的にlocalhost:8000/indexから結果を取得することができる。

角度コントローラ:

私は私のAPIのdosen'tが同じに設定だと思う

security: 
providers: 
    cas: 
     id: prayno.cas_user_provider 
role_hierarchy: 
     ROLE_ADMIN:  ROLE_USER 
     ROLE_SUPER_ADMIN: ROLE_ADMIN 

firewalls: 
    dev: 
     pattern: ^/(_(profiler|wdt)|css|images|js)/ 
     security: false 
    main: 
     anonymous: ~ 
     logout: ~ 
     guard: 
      authenticators: 
       - prayno.cas_authenticator 

access_control: 
    - { path: /index, roles: ROLE_USER} 

:10

nelmio_corsバンドルの設定:

nelmio_cors: 
    defaults: 
     allow_credentials: true 
     allow_origin: ['*'] 
     allow_headers: ['*'] 
     allow_methods: ['POST', 'PUT', 'GET', 'DELETE'] 
     max_age: 3600 
     hosts: [] 
     origin_regex: false 

CASバンドル構成:

p_rayno_cas_auth: 
server_login_url: https://extranet-authentification-******/cas-a3net/ 
server_validation_url: https://extranet-authentification-*****/cas-a3net/serviceValidate 
server_logout_url: https://extranet-authentification-****/cas-a3net/logout 

(security.ymlファイル)ヘッダーが角度doであるため、ブラウザーはフェッチを許可しません。

ヘッダーオプションをAngularコントローラーから直接設定することはできますが、これはAPIのものと一致する可能性がありますか?

ありがとうございました。

+0

角度からヘッダーを制御することはできません。 –

+0

私はセキュリティを無効にする(私の悪い、編集)しかし、とにかく、私はセキュリティを無効にしたくない、私は(可能であれば)右のヘッダを追加したい! – Valoo

答えて

0

あなたは常に$ customersの結果セットではなく、Symfonyからの応答のインスタンスを返すべきです。

これはどのように実行できるかの例です: return new JsonResponse($ customers、200、array( 'Access-Control-Allow-Origin' => '*'));

さらに詳しい情報がここにあります。

AJAX Cross-domain on symfony2

+0

既に試したことがありません。 – Valoo

+0

Chrome Dev Tools Networkタブからレスポンスヘッダーを貼り付けることはできますか? (私たちが興味を持っているリクエストのために)そのヘッダーはsymfonyから設定されていますか? – sw0rdfishhhhh

+0

「接続:キープアライブ のContent-Length:0 のContent-Type:text/htmlの;のcharset = UTF-8 日\t:火、2017年7月11日13時11分46秒GMT がキープアライブ:タイムアウト= 15 、最大= 100 場所: https://extranet-authentification-cerfrance.35-56.fr/cas-a3net/login?service=http%3A%2F%2Flocalhost%3A8000 %2Findex サーバー:Apacheの」 – Valoo

関連する問題