2017-08-28 53 views
0

我正在開發一個項目,該項目使用symfony2作爲後端和前端應用程序在Angular2中構建的API。登錄並獲得令牌後,當我嘗試使用的令牌頭到我的後端這個問題happendNelmio Cors,Api如何設置帶有令牌標頭的angular2請求

發送GET請求

令牌未在TokenStorage

發現下面的錯誤截圖

internal server error

header response

BACKEND SETTING

#nelmioCorsBundle configuration IN CONFIG.YML 
nelmio_cors: 
defaults: 
    allow_credentials: true 
    allow_origin: '*' 
    allow_headers: ['accept', 'content-type', 'authorization', 'x-http-method-override'] 
    allow_methods: ['POST', 'PUT', 'PATCH', 'GET', 'DELETE'] 
    max_age: 3600 


paths: 

    '^/': 
     allow_origin: ['http://localhost:4201'] 
     allow_headers: ['Authorization', 'X-Requested-With', 'Content-Type', 'Accept', 'Origin', 'X-Custom-Auth'] 
     allow_methods: ['POST', 'PUT', 'GET', 'DELETE', 'OPTIONS'] 
     max_age: 3600 
     hosts: [] 
     origin_regex: false 
     hosts: ['^\.'] 

BACKEND SETTING SECURITY.YML

firewalls: 
     login: 
      pattern: ^/api/login 
      form_login: 
      provider: fos_userbundle 
      login_path: /api/login 
      check_path: /api/login_check 
      username_parameter: username 
      password_parameter: password 
      success_handler: lexik_jwt_authentication.handler.authentication_success 
      failure_handler: lexik_jwt_authentication.handler.authentication_failure 
      require_previous_session: false 
     logout:  true 
     anonymous: true 

    api: 
     pattern: ^/api 
     anonymous: false 
     provider: fos_userbundle 
     lexik_jwt: #par defaut check token in Authorization Header prefixer par Bearer 
      authorization_header: # check token in Authorization Header 
        enabled: true 
        prefix: Bearer 
        name: Authorization 
      cookie:    # check token in a cookie 
        enabled: false 
        name: BEARER 
      query_parameter:  # check token in query string parameter 
        enabled: true 
        name: bearer 
      throw_exceptions:  true  # When an authentication failure occurs, return a 401 response immediately 
      create_entry_point:  false  # When no authentication details are provided, create a default entry point that returns a 401 response 
      authentication_provider: lexik_jwt_authentication.security.authentication.provider 
      authentication_listener: lexik_jwt_authentication.security.authentication.listener 

回答

0

在你的客戶的角度,當你在你簽署必須得到某種形式的訪問令牌。 因此,你的頭應該包含這樣的內容:

Authorization: Bearer <Token>

+0

getListcommandes(idcommande:編號):可觀察 { 常量URL ='$ {this.postUrlCommandes}/$ {idcommande}'; let header = new Headers({'Authorization':'Bearer'+ mytoken}); headers.append('Content-Type','application/json'); let options = new RequestOptions({headers:headers}); return this.http.get(url,headers) .map((response:Response)=> { var result = response.json(); return result; }); } –

+0

我已經在函數角邊設置了標頭承載者 –

+0

奇怪的是,您設置了標頭,但它們沒有出現在請求標頭中(來自您上傳的圖片)。在getListCommandes' 2.;從 1'進口{HttpHeaders} '@角/普通/ HTTP': '頭=新HttpHeaders({ '授權': '承載' – Nick