PAC自动代理文件实现第三方系统(http协议)免登录跳转
1、发布第三方系统免登录跳转的中间页面,如:http:172.12.5.113:8990/xxx/index.html,代码如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <base target="_blank" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <link rel="icon" href="<%= BASE_URL %>favicon.ico"> <title>武汉化工区统一认证平台</title> <script> </script> <script type="text/javascript" src='./lib/jquery-1.12.4.min.js'></script> <style> #app { width: 100%; height: 100%; } </style> </head> <body> <noscript> <strong>We're sorry but v-cli-test doesn't work properly without JavaScript enabled. Please enable it to continue.</strong> </noscript> <div id="app"> </div> <!-- built files will be auto injected --> <script type="text/javascript"> $(function () { var sysInfo = {}; if (location.search && location.search !== '?null') { var search = decodeURIComponent(location.search).replace('?sysInfo=', ''); sysInfo = search && JSON.parse(search); localStorage.setItem('sysInfo', search); history.pushState(null, sysInfo.applicationName, location.href.split('?')[0]) } else { sysInfo = localStorage.getItem('sysInfo') && JSON.parse(localStorage.getItem('sysInfo')); } var curWindow = window; curWindow.document.querySelector('title').innerText = sysInfo.applicationName; var newWindow = window.open('_blank'); // 先打开页面 newWindow.location = sysInfo.loginUrl; // 后更改页面地址 // var newWindow = window.open(sysInfo.loginUrl); if (!newWindow || !newWindow.document) { alert('请允许浏览器弹窗后重试!'); return; } var n = 0; var intervalP = setInterval(function () { n += 1; var $body = $(newWindow.document.body); if ($body.length > 0) { clearInterval(intervalP); initLogin(); var intervalC = setInterval(function () { n += 1; initLogin(); if (n > 50) { clearInterval(intervalC); // 关闭该弹框 curWindow.close(); } }, 50); } }, 50); function initLogin() { var $body = $(newWindow.document.body); if ($body.length > 0) { var nameInput = $body.find(sysInfo.nameInputValue); if (nameInput.length > 0) { clearInterval(interval); nameInput.val(''); changeInputData(nameInput[0], sysInfo.userName) var pwdInput = $body.find(sysInfo.pwdInputValue); pwdInput.val(''); changeInputData(pwdInput[0], sysInfo.pwd); var loginBtn = $body.find(sysInfo.btnInputValue); setTimeout(function() { loginBtn.click(); // 关闭该弹框 curWindow.close(); }, 200); } } } // 触发input change事件 function changeInputData(el, value) { let copySetValue = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set; copySetValue.call(el, value) let e = new Event('input', { bubbles: true }) el.dispatchEvent(e) } }); </script> </body> </html>
1、新建PAC文件,设置中间跳转页面的url代理,内容如下(中间跳转页面访问路径为:172.12.5.113:8990/xxx/index.html):
function FindProxyForURL(url, host) { if(shExpMatch(url, "*/xxx/*")){ return "PROXY 172.12.5.113:8990"; } }
2、pac文件放置到服务器上,如:访问地址为:http://172.17.5.113:8990/pac/proxy.pac
3、配置window系统的代理,如:win10配置如下:
注意: 更改代理后,需要保存再关闭后打开‘使用设置脚本’开关
4、更改index页面的ip和端口号,如:172.12.4.10:8800/xxx/index.html,则访问地址依然是172.12.5.113:8990/xxx/index.html页面,到这里就实现了第三方系统跳转的跨域问题