简体   繁体   English

使用JavaScript打印网页的HTTP标头

[英]Printing the web page's HTTP Headers using JavaScript

So I have a webapp that is built on the nodejs framework. 所以我有一个基于nodejs框架的webapp。 At the moment I am using an NGINX Reverse Proxy that uses OIDC for authentication. 目前,我正在使用NGINX反向代理,该代理使用OIDC进行身份验证。 Once the user is authenticated it then forwards them on to the nodejs backend. 用户通过身份验证后,便会将其转发到nodejs后端。

I want to grab the users email address from the header which is passed from the reverse proxy to the backend and print it on my homepage so it will say Welcome XXX 我想从反向代理传递到后端的标头中获取用户的电子邮件地址,并将其打印在我的首页上,这样它将显示“欢迎XXX”

Now I can see that if i add the below in my app.js it will print the headers to the command line so i know it is being passed, but i just do not what javascript i need to achieve this 现在我可以看到,如果我在app.js中添加以下内容,它将在命令行中打印标题,因此我知道它已通过,但我不需要实现此目的所需的JavaScript

app.get('/home', function (req) {
  console.log(req.headers);
});

The example found here Accessing the web page's HTTP Headers in JavaScript does present me with a popup but it does not grab show the info i need which is 'x-user': 'fffffff@ffff.fff' 此处找到的示例使用JavaScript访问网页的HTTP标头确实向我展示了一个弹出窗口,但没有抓取显示我需要的信息,即“ x用户”:“ fffffff@ffff.fff”

I tried this code also but it is not picking it up for me 我也尝试过此代码,但它不是为我准备的

<script>
function getClientIP(req){
    return req.headers['x-user'];
}
</script>
<h3><b><script>getClientIP();</script></b></h3>

I have this printing to the console.log so i know for a fact x-user is present in the request. 我已经将此打印到console.log,所以我知道请求中存在x用户的事实。

Figured it out 弄清楚了

    <script>
    function parseHttpHeaders(httpHeaders) {
        return httpHeaders.split("\n")
         .map(x=>x.split(/: */,2))
         .filter(x=>x[0])
         .reduce((ac, x)=>{ac[x[0]] = x[1];return ac;}, {});
    }
    var req = new XMLHttpRequest();
    req.open('HEAD', document.location, false);
    req.send(null);
    var headers = parseHttpHeaders(req.getAllResponseHeaders());
    </script>

    <script>
    function here() {
     document.write(JSON.stringify(headers["x-user"]));
    }
    </script>


<h3><b><script>here();</script></b></h3>

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

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