session过期跳转到登陆页面并跳出iframe框架的两个方法

原文章地址:http://1008610086.blog.51cto.com/4995677/1538464


最近在做拦截器,判断用户登录后操作超时,失去权限然后要重新登录,但是用的iframe,返回的登陆页总是在框架中显示,我百度了下,总是只有其中一个方法,现在分享下两种解决方法,希望对你们有帮助:


方法一:

一般使用filter过滤用户是否登录,如果用户没有登陆则转向登陆页面,这时候可以使用response.sendRedirect()。
但当在页面上使用了iframe后,发现被重定向的只是父页面中的iframe区域,登陆页面内容显示在该区域中。说明在过滤器中发送重定向请求时,是在iframe页面发送的。错误的代码如下:

  HttpServletRequest req = (HttpServletRequest) request;  
Visitor visitor = (Visitor) req.getSession().getAttribute("visitor");  
if (visitor == null)  
{  
     ((HttpServletResponse) response).sendRedirect("/smpc/login/login.jsp");  
}  
chain.doFilter(request, response);

  因为response.sendRedirect()没有target属性,但html页面和js中有,于是,当判断出用户没有访问权限时,我们可以在jsp中使用js来转向到真正的登录页面。在filter类的doFilter方法中添加如下代码:(跳转的路径通过request.getRequestURI()获取)


User user = (User) req.getSession().getAttribute("session_user");  
if (user == null)  
{  
    java.io.PrintWriter out = response.getWriter();  
    out.println("<html>");  
    out.println("<script>");  
    out.println("window.open ('/smpc/login/login.jsp','_top')");  
    out.println("</script>");  
    out.println("</html>");  
    return;
}  


方法二:
在你想控制跳转的页面,比如login.jsp中的<head>与</head>之间加入以下代码:
<script language="JavaScript"> 
if (window != top) 
top.location.href = location.href; 
</script>
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是前后端代码实现的思路: 后端代码: 1. 在BaseController中添加一个OnActionExecuting方法,用于判断用户是否登录,如果未登录,则跳转登录面。 ```csharp public class BaseController : Controller { protected override void OnActionExecuting(ActionExecutingContext filterContext) { HttpSessionStateBase session = filterContext.HttpContext.Session; if (session["UserId"] == null) { filterContext.Result = new RedirectResult("~/Account/Login"); } base.OnActionExecuting(filterContext); } } ``` 2. 在需要登录验证的Controller中,继承BaseController即可。 ```csharp public class HomeController : BaseController { public ActionResult Index() { return View(); } } ``` 前端代码: 1. 在前端面中,使用`setInterval()`函数定时向后端发送一个心跳请求,以保持会话的活跃状态。 ```javascript setInterval(function () { $.ajax({ url: "/HeartBeat", type: "get", dataType: "json", success: function (data) { //处理返回的数据 }, error: function () { //处理请求失败的情况 } }); }, 300000); //5分钟发送一次请求 ``` 2. 在前端面中,使用`window.addEventListener()`函数监听所有的ajax请求,如果返回的状态码为401,则跳转登录面。 ```javascript window.addEventListener('load', function () { $(document).ajaxComplete(function (event, xhr, options) { if (xhr.status == 401) { window.location.href = '/Account/Login'; } }); }); ``` 3. 在前端面中,如果面中嵌套了iframe,则在iframe加载完成后,判断iframe中的面是否需要登录验证,如果需要,则向iframe中发送一个心跳请求。 ```javascript function checkIframeLogin() { var iframe = document.getElementById('myIframe'); if (iframe) { var iframeDoc = iframe.contentDocument || iframe.contentWindow.document; var iframeUrl = iframeDoc.location.href; if (iframeUrl.indexOf('NeedLogin') != -1) { setInterval(function () { iframe.contentWindow.postMessage('HeartBeat', '*'); }, 300000); //5分钟发送一次请求 } } } ``` 在iframe中的面中,添加一个监听事件,如果收到了来自父面的心跳请求,则返回一个状态码,用于前端面中的跳转。 ```javascript window.addEventListener('message', function (event) { if (event.data == 'HeartBeat') { $.ajax({ url: '/HeartBeat', type: 'get', dataType: 'json', success: function (data) { //处理返回的数据 }, error: function () { parent.window.location.href = '/Account/Login'; } }); } }); ``` 上述代码中,`parent.window.location.href = '/Account/Login';`表示在父面中跳转登录面。 希望以上代码可以帮助到您,如有任何疑问,请随时提出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值