Angular 2: Get Authorization header

前端 未结 3 1659
春和景丽
春和景丽 2020-12-16 02:45

in my App (Angular 2 / Ionic 2) I implemented my own login/authentication. Basically it works like this: On login, username and password is being validated by the PHP backen

相关标签:
3条回答
  • 2020-12-16 02:49

    Just wanted to post the answer as it might help others: The solution as to set

    Access-Control-Expose-Headers: Authorization
    

    Then - frontend can read the Authorization header as well.

    0 讨论(0)
  • 2020-12-16 03:04

    This answer helped me : link

    It shows how to add it in the backend and how use it in the frontend

    Java backend:

    public void methodJava(HttpServletResponse response){
    ...
    response.addHeader("access-control-expose-headers", "Authorization");
    }
    

    And access the header on angular like this:

    return this.http
        .get(<your url here for your backend>)
        .map(res => console.log("cookie: " + res.headers.get("Authorization") )
    }
    
    0 讨论(0)
  • 2020-12-16 03:16

    That's not related to Angular. The problem is CORS limits headers by default and you do not see "Authorization" header when call CORS requests. So, adjust server to let it send Authorization header

    Access-Control-Allow-Headers must be provided in response of OPTIONS request (pre-flight).

    Access-Control-Expose-Headers must be provided in response of POST/GET request.

    Access-Control-Expose-Headers: Authorization

    0 讨论(0)
提交回复
热议问题