简体   繁体   English

使用Angular 2时如何读取HTTP协议标头?

[英]How to read HTTP protocol headers when using Angular 2?

My Angular 2 application is behind a reverse proxy that add HTTP headers like: 我的Angular 2应用程序位于反向代理后面,该反向代理添加HTTP标头,例如:

Auth-User: somebody 
Auth-Groups: something, something, ...

How can I read these values from my Angular 2 application? 如何从Angular 2应用程序中读取这些值? Note that I am not: 请注意,我不是:

  • making a new http call, 拨打新的http电话,
  • doing a username/password login, add something to sessionStorage that I can use in a CanActivate 进行用户名/密码登录,向sessionStorage添加一些可以在CanActivate中使用的东西

Basically I need to support a SSO scheme based on trusted HTTP headers from the very first time a browser hits my server. 基本上,从浏览器第一次访问我的服务器时,我就需要支持基于受信任HTTP标头的SSO方案。

Any ideas? 有任何想法吗?

Lars 拉尔斯

I realize that it cannot be done directly. 我意识到这无法直接完成。 Angular is client oriented and heavily sandboxed by the browser, so it is only the server that is able to access the necessary headers. Angular是面向客户端的,并被浏览器大量沙箱化,因此只有服务器才能访问必要的标头。 What I have come up with is: 我想出的是:

  • get some user settings service on the server. 在服务器上获得一些用户设置服务。 Includes the user and groups information the response. 包括用户和组信息的响应。 Service calls to the server cannot of course trust the client's view on who is the current user and group membership. 当然,对服务器的服务调用不能信任客户端关于谁是当前用户和组成员身份的视图。 The is the one I chose 这是我选择的
  • Use Universal Angular, which is server side oriented. 使用面向服务器端的Universal Angular。 In my case it didn't fit the culture of the company. 就我而言,这不适合公司的文化。

So instead of doing a basic authentication scheme like most examples/tutorials use, the solution is just getting the headers values wrapped in a service call. 因此,解决方案不是像大多数示例/教程一样使用基本的身份验证方案,而是仅将标头值包装在服务调用中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM