springmvc项目导出excel时碰到cookie/flash.js劫持,初次请求返回结果如下:
<html>
<head>
<script language="javascript">setTimeout("location.replace(location.href.split(\"#\")[0])",2000);</script>
<script type="text/javascript" src="http://10.48.0.250:89/cookie/flash.js"></script>
<script language="javascript">setURL("10.48.0.250");supFlash("1985617644");</script>
</head>
<body>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="0" height="0" id="m" align="center"><param name="allowScriptAccess" value="always" />
<param name="movie" value="http://10.48.0.250:89/cookie/flashcookie.swf" />
<param name="quality" value="high" />
<param name="FlashVars" value="srv=10.48.0.250" />
<embed src="http://10.48.0.250:89/cookie/flashcookie.swf"FlashVars="srv=10.48.0.250" quality="high" width="0" height="0" name="m" align="center" allowScriptAccess="always" type="application/x-shockwave-flash"pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
</body></html>
初次请求被劫持返回这个页面之后又触发了两次请求,导致浏览器弹出两次文件下载窗口。猜想初次请求已经到了后端,然后上面这个html里的setTimeout脚本又请求了一次后端。所以这里改用iframe加载导出链接,然后屏蔽iframe的setTimeout方法的方式,避免html里的二次请求。代码如下:
var url = "/declare/exportExcel";
if ($('#iframeExcel').eq(0) && $('#iframeExcel').eq(0).attr('src')) {
$('#iframeExcel').attr('src', url);
} else {
var iframeExcel = "<iframe id='iframeExcel' src='" + url + "' width='0px' height='0px'></iframe>"
$("body").append(iframeExcel)
$('#iframeExcel')[0].contentWindow.setTimeout = function(){};
}