nginx反向代理解决跨域获取localStorage里的token

背景

仅供学习研究使用,请勿用于制作钓鱼、攻击等技术
想要自动拿到一个网站的token鉴权,进行后续资源获取的操作。
目标网站的token是存在localStorage里的,loacalStorage里的资源是根据主机域名来区分的,不能跨域获取。所以采用nginx反向代理让目标网站和本地服务在同一个域里。
在这里插入图片描述

nginx反向代理原理

借助ngxin的porxy_pass配置,将nginx服务作为中转站,使目标网站和本地网站融合成同一个访问地址。从而让浏览器以为两个网站在同一个域里

安装nginx

安装步骤,这里不过多介绍了,具体查看这里nginx安装

配置nginx反向代理

  1. 修改nginx的配置文件(根据自己的安装路径找到default.conf)
vim nginx/conf/conf.d/default.conf
  1. 增加反向代理porxy_pass配置
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
        proxy_pass https://download.taoyuzhou.net; #配置目标网站ip地址或域名
    }

    location ~ /yan {
        proxy_pass http://192.168.85.1; #配置本地服务地址
    }
    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
  1. 重启nginx

访问nginx地址

我这里已经反向代理成功了
在这里插入图片描述

修改自己的本地服务

通过ifarme嵌入代理后的目标网站地址,登录后就可以拿到token了。

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
</head>
<body>

<!-- <iframe width="100%" height="800" name="myifarme" id="myifarme" seamless="seamless" src="https://download.taoyuzhou.net/#/pages/my/login/index"></iframe> -->
<iframe width="100%" height="800" name="myifarme" id="myifarme" seamless="seamless" src="http://192.168.85.132"></iframe>


</body>
<script type="text/javascript">
function getToken(){
	console.log(localStorage.token)
}
getToken()
</script>
</html>

访问自己的服务

通过iframe嵌入代理后的目标网站地址,登录后就可以拿到localStorage里的token了。
在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值