shiro 攔截器實現session到期攔截ajax請求的處理

來源:互聯網
上載者:User

攔截器代碼:

package com.xlqh.outlook.shirofilter;import java.io.IOException;import org.apache.shiro.web.filter.PathMatchingFilter;import com.xlqh.outlook.MyShiroConstants.MyShiroConstants;import com.xlqh.outlook.SessionManagement.SessionManagement;import com.xlqh.outlook.entity.yuuser;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;//自訂攔截器,Session到期AJAX處理public class SessionExpiredFilter extends PathMatchingFilter {    @Override       protected boolean onPreHandle(ServletRequest  request, ServletResponse response, Object mappedValue) throws Exception {          yuuser sys = (yuuser) SessionManagement.getSession(MyShiroConstants.Session_USER);             if(sys==null){            System.out.println("session到期");            if ("XMLHttpRequest".equalsIgnoreCase(((HttpServletRequest) request).getHeader("X-Requested-With"))) {                //ajax的sesson處理                //返回狀態代碼                 onLoginFail(response);                return false;                }            else{                //普通的處理,直接給到下一個攔截器                 return true;               }        }        return true;    }    //session到期給403狀態代碼    private void onLoginFail(ServletResponse response) throws IOException {        HttpServletResponse httpResponse = (HttpServletResponse) response;        httpResponse.setStatus(403);        httpResponse.getWriter().write("session逾時了");    }}

前台頁面全域需要處理的,基於jquery的ajax請求都會被攔截

全域的js片段:

var timeoutlogin="/login?code=22";    $.ajaxSetup({        contentType : "application/x-www-form-urlencoded;charset=utf-8",        cache : false,        complete : function(data, TS) {            //對返回的資料data做判斷            debugger;            //403是後台攔截器那兒定義的,            if (data.status == 403) {                //session到期的話,就location到一個頁面                location.href=timeoutlogin;                return;            }        }    });

ajax請求的js:

function test() {        var valusid = $("#select2").val();        $.post("yurole/selectuserrole", {"userid" : valusid}, function(data) {            $("#div2").html("");            debugger;            for ( var i = 0; i < data.length; i++) {                $("#div2").append("<p><a href='"+data[i].menuUlr+"' target='content' >"+ data[i].menuName + "<a/></p>");            }        });    }

shiro配置:
需要把session攔截器放在登入攔截器前面

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.