spring security 單一賬戶多地方登陸提醒, ajax 攔截器 Interceptor

來源:互聯網
上載者:User

標籤:提醒   equal   port   類型   ==   null   資料庫管理   mode   response   

spring-security.xml部分代碼:

    <http auto-config="false" >        <access-denied-handler ref="accessDeniedHandler" />        <!--         <form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?login_error=1" default-target-url="/" always-use-default-target="true" />        -->        <http-basic />        <custom-filter position="CONCURRENT_SESSION_FILTER" ref="concurrencyFilter" />        <custom-filter ref="loginFilter" before="FORM_LOGIN_FILTER" />        <!-- 替換預設的LogoutFilter <logout logout-success-url="/login.jsp" /> -->        <custom-filter ref="ylLogoutFilter" before="LOGOUT_FILTER" />        <custom-filter ref="logoutFilter" position="LOGOUT_FILTER" />        <custom-filter ref="carParkLogoutFilter" after="LOGOUT_FILTER" />        <!-- 增加一個自訂的customSecurityInterceptor,放在FILTER_SECURITY_INTERCEPTOR之前, 實現使用者、角色、許可權、資源的資料庫管理。 -->         <custom-filter ref="customSecurityInterceptor" before="FILTER_SECURITY_INTERCEPTOR" />        <remember-me />        <!-- 會話管理配置  -->        <session-management session-authentication-strategy-ref="sessionAuthenticationStrategy" invalid-session-url="/logon/commonSessionExpired.htm"/>    </http>    <beans:bean id="concurrencyFilter" class="org.springframework.security.web.session.ConcurrentSessionFilter">          <beans:property name="sessionRegistry" ref="sessionRegistry" />          <beans:property name="expiredUrl" value="/logon/commonSessionExpired.htm" />      </beans:bean>

攔截到期:spring-mvc.xml 這裡主要是為了彈視窗,不彈視窗就不用做下面了

    <mvc:interceptors>        <!-- 特定路徑下才攔截 -->        <mvc:interceptor>            <mvc:mapping path="/logon/commonSessionExpired.htm"/>            <bean class="com.jevon.frame.security.SessionInterceptor"/>         </mvc:interceptor>     </mvc:interceptors>

攔截器:SessionInterceptor, 這裡需要區分ajax請求,和普通請求,ajax返回需要轉換成JSON格式的,這裡ReturnResult使用Map代替就可以了

package com.jevon.frame.security;import java.io.PrintWriter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;import com.jevon.can.common.domain.ReturnResult;import net.sf.json.JSONObject;public class SessionInterceptor extends HandlerInterceptorAdapter {    @Override    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)            throws Exception {            request.setCharacterEncoding("UTF-8");            response.setCharacterEncoding("UTF-8");            response.setContentType("text/html;charset=UTF-8");            /**            String url = request.getServletPath();            if (url != null && url.contains("/logon/expiredUrl")) {                return Boolean.TRUE;            }**/            if (null == request.getSession().getAttribute("SES_CURRENTUSER")) {                if(isAjax(request)){                    PrintWriter out = response.getWriter();                    JSONObject jsonObject = JSONObject.fromObject(new ReturnResult("頁面到期,請重新登入!", "logout", false));                    out.print(jsonObject);                    out.close();                }else{                    PrintWriter out = response.getWriter();                    StringBuilder builder = new StringBuilder();                    builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");                    builder.append("window.top.logoutAlert(\"頁面到期,請重新登入!\");");//alert(\"頁面到期,請重新登入!\");");                    builder.append("</script>");                    out.print(builder.toString());                    out.close();                }                return false;            }         return true;    }    @Override    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,            ModelAndView modelAndView) throws Exception {        super.postHandle(request, response, handler, modelAndView);    }    @Override    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)            throws Exception {        // TODO Auto-generated method stub        super.afterCompletion(request, response, handler, ex);    }        //判斷是否ajax請求    public static boolean isAjax(HttpServletRequest request) {           return "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));      }  }

封裝的ajax.js返回:這裡使用的是logout類型

        loadComplete: function (data) {            if (data.success === false) {                if (data.msgType == ‘1‘) {                    window.parent.location = $.getProjectName() + ‘/logon/commonQuit.htm‘;                }                if (data.msgType == ‘2‘ && data.msg) {                    $.messager.alert("警告", data.msg, "warning");                }                if (data.msgType == ‘3‘ && data.msg) {                    $.messager.alert("錯誤", data.msg, "error");                }                if(data.msgType == ‘logout‘) {                    $.messager.alert("警告", data.msg, "info", function(r) {                        window.parent.location = $.getProjectName() + ‘/logon/expiredUrl.htm‘;                    });                }            }        }

最頂層jsp頁面:攔截器直接調用該方法,alert 就可以使用到樣式

<script>     function logoutAlert(msg) {        $.messager.alert("警告", msg, "info", function(r) {             window.location = $.getProjectName() + ‘/logon/expiredUrl.htm‘;        });    }</script>

 

 

spring security 單一賬戶多地方登陸提醒, ajax 攔截器 Interceptor

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.