redirect user to access page after login

Source: Internet
Author: User

Code Download: Login_limit

1. Demand Scenario

1) share to other platform link users click to jump to the corresponding platform login page, login and redirect to the sharing page

2) The user stays on a page for a long time without action to the session expires, refresh or click on another link to redirect to the sign-in page, sign in and ask to redirect to the page you want to visit

2. Demand Analysis

Given that you want to redirect to a page after logging in, you need the system to store the current link when you jump to the login page to redirect after login. The idea is already very clear, because we use the interceptor to determine whether the user is logged in, so we just need to save the current link in the Interceptor method, then login redirect. The specific code is as follows:

Since we have previously written a multi-user login limit , we just need to make a slight modification on the basis of this blog post (just modify the login blocker), and the modified code is as follows (bold code is modified code)

Note: It is important to note that asynchronous requests are filtered (asynchronous requests do not jump to the page, causing the page to display the requested data directly after the login)

 PackageCom.utils.interceptor;ImportCom.entity.User;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.handler.HandlerInterceptorAdapter;Importjava.util.Date;ImportJava.util.Map;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjavax.servlet.http.HttpSession;/*** Login Blocker 1, no login jump login page 2, drop the current link to save, redirect to the login Page 3, the user was kicked off after giving the prompt message*/ Public classLogininterceptorextendsHandlerinterceptoradapter {@Override Public BooleanPrehandle (httpservletrequest request, httpservletresponse response, Object handler) throwsException {HttpSession session=request.getsession (); User User= (User) session.getattribute ("Now_user"); if(User = =NULL) {            //user drop line, save current link and redirect to login page//String s = Request.getheader ("X-requested-with");            if  (Request.getheader ("x-requested-with") = = null ) {//  non-Ajax (asynchronous) request, save current access link  String que Ryurl = request.getquerystring () = = null ? "": ("?" + request.getquerystring ()); //  get parameter  String Requesturl = Request.getservletpath () + Queryurl; // httprequest.getservletpath (), gets the link  if  (Session.getattribute ("redirect_link") = = null  ) {Session.setattribute ( "Redirect_link"  , Requesturl); }} Response.sendredirect (Request.getcontextpath ()+ "/other/tologin"); return false; }        //Multi-User login limit judgment, and give the prompt information        BooleanIsLogin =false; if(User! =NULL) {Map<string, string> Loginusermap = (map<string, string>) Session.getservletcontext (). GetAttribute (" Loginusermap "); String sessionId=Session.getid ();  for(String key:loginUserMap.keySet ()) {//user has logged in at another place                if(Key.equals (User.getusername ()) &&!Loginusermap.containsvalue (sessionId)) {IsLogin=true;  Break; }            }        }        if(islogin) {Map<string, string> loginouttime = (map<string, string>) Session.getservletcontext (). GetAttribute (" Loginouttime "); Session.setattribute ("Mess", "User:" + user.getusername () + "," + Loginouttime.get (User.getusername ()) + "Logged in elsewhere!");            Loginouttime.remove (User.getusername ()); Session.getservletcontext (). SetAttribute ("Loginusermap", Loginouttime); Response.sendredirect (Request.getcontextpath ()+ "/other/tologin"); return false; }        return Super. Prehandle (Request, response, handler); } @Override Public voidPosthandle (httpservletrequest request, httpservletresponse response, Object handler, Modelandview Modelandview)throwsException {Super. Posthandle (Request, response, Handler, Modelandview); } @Override Public voidaftercompletion (httpservletrequest request, httpservletresponse response, Object Handl Er, Exception ex)throwsException {Super. Aftercompletion (Request, response, Handler, ex); }}

This is the perfect solution to the problem of redirecting users to a page after they log in.

redirect user to access page after login

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.