Spring+session+interceptor+ajax (Interceptor) Login and exit

Source: Internet
Author: User

Method one uses the httpsession that the servlet comes with

Note the point: httpsession should be used as a parameter of the method

 //log in   
public boolean customerlogin (HttpSession    httpSession) { Httpsession.setattribute ( " customer " }
// Exit Public String customerout (HttpSession HttpSession) {  httpsession.removeattribute (         "Customer"); return"Login";     }

Method Two: Use Spring's @sessionattributes ("Logincustomer")

 / /log in   
@Controller @SessionAttributes ( "Logincustomer" public class Customercontroller { public Jsonobject Customerlogin (@RequestBody jsonobject JSON, Modelmap model,httpservletresponse response) { Model.addattribute ( "Logincustomer"
// exit @RequestMapping (value = "Customerout")    public  String customerout ( Sessionstatus sessionstatus) {        sessionstatus.setcomplete (); // only useful for @sessionattributes ("customer"), HttpSession useless         // use Sessionstatus.setcomplete (); Will clear all the sessions,        return "Login";    }

Interceptors (Interceptor)

Note: The interceptor is used in conjunction with Ajax to use this statement response.sendredirect (request. Getcontextpath () + "/login.jsp"); to pass the result to the front end, and then to the front end to achieve the jump

So to determine if the request is an AJAX request

 PackageCom.dessert.interceptor;ImportJava.io.OutputStream;ImportJava.io.PrintStream;ImportJavax.servlet.ServletOutputStream;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjavax.servlet.http.HttpSession;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.handler.HandlerInterceptorAdapter; Public classCommoninterceptorextendsHandlerinterceptoradapter {/** There are three methods in the Interceptor: * Prehandler: Before entering the handler method, the use of identity authentication, identity authorization, login verification, such as identity authentication, the user does not log in, interception is no longer executed, * return value is false, you can implement     intercept; otherwise, when true, the interception does not execute; Posthandler *: After entering the handler method, go back to Modelandview before executing, using the scene from the Modelandview parameter, for example, the common model data is passed here to the view, * can also be unified to specify the view of the display, etc. afterhandler: After executing the handler, this method is implemented, which is used for uniform exception handling, unified log processing, etc.*/@Override Public BooleanPrehandle (httpservletrequest request, httpservletresponse response, Object handler)throwsException {HttpSession session=request.getsession (); if(Session.getattribute ("Logincustomer")! =NULL) {//System.out.println (Session.getattribute ("Costomer"));                return true; }            //if it is an AJAX request, the request header will have X-requested-withString Requestwith = Request.getheader ("X-requested-with"); if(Requestwith! =NULL&& requestwith.equalsignorecase ("XMLHttpRequest")){                if(Session.getattribute ("customer") = =NULL) {                    return false; } Else if(Session.getattribute ("Logincustomer")! =NULL) {                    return true; }            } Else{response.sendredirect (Request. Getcontextpath ()+ "/login.jsp"); }            return false; } @Override Public voidPosthandle (httpservletrequest request, httpservletresponse response, Object handler, Modelandview Modeland View)throwsException {} @Override Public voidaftercompletion (httpservletrequest request, httpservletresponse response, Object handler, Exception ex) 
    throwsException {}}

Configuration of interceptors in the SPRINGMVC

<mvc:interceptors>        <Mvc:interceptor>            <!--The URL path is matched, and if not configured or/**, all controllers will be blocked -            <mvc:mappingPath="/**" />            <!--<mvc:exclude-mapping path= "/index.jsp"/> -            <mvc:exclude-mappingPath= "/*login" />            <mvc:exclude-mappingPath= "/forgotpwd" />            <mvc:exclude-mappingPath= "/customerregister" />            <mvc:exclude-mappingPath= "/vaildtel" />            <mvc:exclude-mappingPath= "/css/**" />            <mvc:exclude-mappingPath= "/js/**" />            <mvc:exclude-mappingPath= "/myutil/**" />            <mvc:exclude-mappingPath= "/images/**" />            <!--<mvc:exclude-mapping path= "/*.html"/> -            <Beanclass= "Com.dessert.interceptor.CommonInterceptor">            </Bean>        </Mvc:interceptor>        <!--when multiple interceptors are set, the Prehandle method is called sequentially and then the Posthandle and Aftercompletion methods of each interceptor are called in reverse order -</mvc:interceptors>

Spring+session+interceptor+ajax (Interceptor) Login and exit

Related Article

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.