SPRINGMVC Interceptor Implementation: When the user accesses the site resources, monitoring session is expired

Source: Internet
Author: User

SPRINGMVC Interceptor Implementation: When the user accesses the site resources, listen to whether the session expired one, interceptor configuration
<mvc:interceptors>    <Mvc:interceptor>        <mvc:mappingPath="/**"/>        <mvc:exclude-mappingPath= "/user/login"/>    <!--do not intercept login requests -        <mvc:exclude-mappingPath= "/user/logout"/>    <!--do not intercept logoff requests -        <mvc:exclude-mappingPath= "*.jsp"/>        <mvc:exclude-mappingPath= "*.html"/>        <mvc:exclude-mappingPath= "*.js"/>        <mvc:exclude-mappingPath= "*.css"/>        <Beanclass= "Org.huaxin.interceptor.AccessInterceptor"></Bean>    </Mvc:interceptor></mvc:interceptors>
Second, the Interceptor Code
 Public BooleanPrehandle (httpservletrequest request, httpservletresponse response, Object obj)throwsException {System.out.println ("[Accessinterceptor]:p rehandle Execution"); HttpSession Session=request.getsession (); ServletContext Application=Session.getservletcontext (); if(Application.getattribute (Session.getid ()) = =NULL){//not logged inPrintWriter out =Response.getwriter (); StringBuffer SB=NewStringBuffer ("<script type=\" text/javascript\ "charset=\" utf-8\ ">"); Sb.append ("Alert (\" Your account has been squeezed out, or not logged in, or the page has expired, please re-login \ ")"); Sb.append ("Window.location.href= '/user/logout ';"); Sb.append ("</script>");            Out.print (Sb.tostring ());            Out.close (); return false; }Else{//already logged in            return true; }    }
Three Summary 1. Note that the interceptor used here is handlerinterceptor, and your interceptor needs to implement this interface 2. In your login handler, to save the session to application, it is convenient to SessionID to determine if there is SESSION3.SB   . Append ("window.location.href= '/user/logout ';"); This line of code is to say, perform the logout operation, in your/user/logout this handler inside must parse the page to the login page, easy to re-login

SPRINGMVC Interceptor Implementation: When the user accesses the site resources, monitoring session is expired

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.