Nelmio Cors,Api如何设置带有令牌标头的angular2请求

Nelmio Cors,Api如何设置带有令牌标头的angular2请求

问题描述:

我正在开发一个项目,该项目使用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 

在你的客户的角度,当你在你签署必须得到某种形式的访问令牌。 因此,你的头应该包含这样的内容:

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