当前位置:网站首页>三种解决:在点击用户退出登录时,再按浏览器的后退按钮,仍然能够看到之前登录的页面。
三种解决:在点击用户退出登录时,再按浏览器的后退按钮,仍然能够看到之前登录的页面。
2022-07-26 03:19:00 【清风亦思雨】
关于这个问题,我尝试了很多方法。我的项目是springboot + theamleaf完成的,当时我的关闭功能是通过a标签进行后端请求,后端清除session,返回登录页面。从逻辑上来说,这是没毛病的,但是,偶然间发现,退出登录后,点击浏览器的后退按钮,仍可以回到登陆完成的页面,因为我使用的是Ajax前后端交互,因此,页面不需要刷新,仍然可以向后端请求数据。那么,问题就来了,我尝试的解决办法和最后的最佳解决办法。
1.禁止浏览器的后退键,代码如下:
<script>
history.go(1);
var counter =0;
if (window.history && window.history.pushState) {
$(window).on('popstate', function () {
window.location='login';
// self.location="orderinfo.html";//如查需要跳转页面就用它
});
}
window.history.pushState('#',null,'#');//在IE中必须得有这两行
window.history.forward(1);
</script>这个办法我最先尝试,发现多次快速后退,会失效,而且会影响地址栏的地址。不同浏览器可能会有其他问题。
2.听说是因为浏览器的缓存,那就禁止页面缓存,代码如下:
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">这是HTML,要知道,兼容性处理很麻烦,chrome浏览器还不支持,而且其他浏览器偶尔失效。并且只是清除缓存没什么用,对于有些问题有帮助,比如执行数据后。
PS:清除浏览器中的缓存,它和其它几句合起来用,就可以使你再次进入曾经访问过的页面时,ie浏览器必须从服务端下载最新的内容,达到刷新的效果。
3.既然是浏览器的缓存,那么就加上版本号,通过js实现文件添加版本号,代码如下:
<script type="text/javascript">
document.write("<script src='**.js?" + Math.random() + "'><\/script>");
</script>既然是浏览器缓存搞鬼,不刷新是没用的,因此这个办法也不行。(不管是浏览器的缓存还是浏览器的后退,他们本就无罪,这些都是防君子,不防小人。)
4.最终解决办法,Ajax向后台清空session,如果成功,则刷新本页,代码如下:
$('#user-exit').click(function () {
$.ajax({
url: "/loginOut",
type: "POST",
success: function (data) {
location.reload();
},
error: function () {
alert("Ajax请求出错!");
}
})
})Session是运行在服务器端的,JavaScript是运行在客户端的,JavaScript不能直接运行服务器端的代码。没有用到Ajax,可以用js,先将Session的值传递到前端某一元素中,然后使用js访问元素的值。
边栏推荐
- 爆肝出了4W字的Redis面试教程
- Canvas -- draw text -- modification of pie chart
- 多商户商城系统功能拆解15讲-平台端会员标签
- tf.truncated_ Normal() usage
- What's good for starting a business with 10000 yuan? Is we media OK?
- 班级里有一群学生考试结果出来了,考了语文和数学两门,请筛选出总分是第一的同学
- JSD-2204-酷鲨商城(管理商品模块)-Day02
- 【TensorFlow&PyTorch】图像数据增强API
- How to correctly calculate the CPU utilization of kubernetes container
- 2020 AF-RCNN: An anchor-free convolutional neural network for multi-categoriesagricultural pest det
猜你喜欢

QT notes - temporary floating window

Configuration and use of virtualservice, gateway and destinationrule of istio III

使用VRRP技术实现网关设备冗余,附详细配置实验

Offline data warehouse from 0 to 1 - phase I resource purchase configuration

图解LeetCode——5. 最长回文子串(难度:中等)

Opencv 在图像上进行标注(画框+写字)

LeetCode·83双周赛·6128.最好的扑克手牌·模拟

Leetcode · 83 biweekly match · 6128. best poker hand · simulation

Easyexcel sets row hiding to solve the problem of sethidden (true) invalidation

Use eventlog analyzer for log forensics analysis
随机推荐
Canvas - drawing pictures - dynamic drawing production
Use Anaconda to configure the tensorflow of GPU Version (less than 30 series graphics cards)
js中数组排序的方法有哪些
What's good for starting a business with 10000 yuan? Is we media OK?
ext4、ntfs、xfs、btrfs、zfs、f2fs和reiserFS性能对比
tensorflow中tf.Variable()函数的用法
Get twice the result with half the effort: learn the web performance test case design model
大厂面试都面试些啥,看了不亏(一)
Classic interview questions -- three characteristics of OOP language
Docker installs redis!!! (including detailed illustration of each step) actual combat
[tensorflow & pytorch] image data enhancement API
NFT因无意义而美丽
UE4 how to render statically? 5 steps to generate static rendering
【 Kotlin 中的类和对象实例】
经典面试问题——OOP语言的三大特征
Istio三之VirtualService、Gateway、DestinationRule配置使用
canvas——矩形的绘制——柱状图的制作
leetcode-202.快乐数
如何正确计算 Kubernetes 容器 CPU 使用率
How to correctly calculate the CPU utilization of kubernetes container