The pre-Logon page is automatically displayed after logon.

Source: Internet
Author: User

Today, we want to implement this function. On the internet, Referer is used for implementation. However, Referer also has several problems:
1. When you open a new window and enter the URL directly, the browser does not seem to send a referer header.
2. It is not generated when it is opened through functions such as JS window. Open.
3 Referer sending or not is configurable in the browser (of course, it is estimated that not many people will set it)

Here I will use struts2 to describe my implementation method:

Package com. snil. commons. interceptors;

Import java. util. enumeration;
Import java. util. List;
Import java. util. Map;

Import javax. annotation. resource;
Import javax. servlet. http. Cookie;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import javax. servlet. http. httpsession;

Import org. aopalliance. Intercept. Invocation;
Import org. Apache. commons. Lang. stringutils;
Import org. Apache. log4j. Logger;
Import org. Apache. struts2.servletactioncontext;
Import org. Apache. struts2.strutsstatics;
Import org. hibernate. criterion. expression;
Import org. springframework. Context. annotation. scope;
Import org. springframework. stereotype. Controller;

Import com. opensymphony. xwork2.actioncontext;
Import com. opensymphony. xwork2.actioninvocation;
Import com. opensymphony. xwork2.interceptor. abstractinterceptor;
Import com. Snail Il. commons. basedao. ibasedao;
Import com. snil. component. Beans. Huiyuan;
@ Scope ("prototype ")
@ Controller ("logininter ")
Public class logininter extends actinterceptor
{
Private Final Static logger = logger. getlogger (loginter. Class );
Public final static string session_key = "user ";
Public final static string cookie_key = "12cd ";
Public final static string goto_url_key = "going_to ";
@ Resource (name = "basedao ")
Protected ibasedao basedao;
@ Override
Public String intercept (actioninvocation Invocation) throws exception
{
Httpservletrequest request = servletactioncontext. getrequest ();

Httpservletresponse response = servletactioncontext. getresponse ();
Httpsession session = request. getsession ();
// Determine whether the user is logged on through the session. If no logon is found, the cookie is read.
If (session! = NULL & session. getattribute (session_key )! = NULL)
{
Return invocation. Invoke ();

}
Cookie [] cookies = request. getcookies ();
If (cookies! = NULL & cookies. length! = 0)
{
For (Cookie: cookies)
{
If (cookie_key.equals (cookie. getname ()))
{
String value = cookie. getvalue ();
If (stringutils. isnotblank (value ))
{
List <Huiyuan> Huiyuans = basedao. findentityproperty (Huiyuan. Class, expression. eq ("yonghuming", value ));
If (Huiyuans. Size () = 0)
{
Cookie. setpath ("/");
Cookie. setvalue (null );

Cookie. setmaxage (0 );
Response. addcookie (cookie );
Return "Index ";
}
If (Huiyuans. Get (0 )! = NULL)
{

Session. setattribute (session_key, Huiyuans. Get (0 ));
Logger.info ("read COOKIE: continue ");
Return invocation. Invoke ();
}

}
}
}
}
// The key point is to set the original request URL.
Settogoingurl (request, session, invocation );
Return "Index ";
}
Private void settogoingurl (httpservletrequest request, httpsession session, actioninvocation Invocation)
{
// Use Referer directly if it is not empty. If it is null, we will obtain the namespace, action name, and request parameters respectively.
// Create a new URL and save it in the session
String url = request. getheader ("Referer ");
Logger.info ("URL to be switched:" + request. getheader ("Referer "));
If (url = NULL | URL. Equals (""))
{
Url = "";
String Path = request. getcontextpath ();
String actionname = invocation. getproxy (). getactionname ();
String namespace = invocation. getproxy (). getnamespace ();
If (stringutils. isnotempty (namespace ))
{
Url = URL + path + namespace;

}
If (stringutils. isnotempty (actionname ))
{
Url = URL + "/" + actionname + ". Action" + "? ";
}

Map <string, string []> zzmap = request. getparametermap ();
If (zzmap! = NULL)
{
For (string S: zzmap. keyset ())
{
String [] value = zzmap. Get (s );
For (string VAL: value)
{
Url = URL + S + "=" + Val + "&";
}

}
}
Logger.info ("complete URL:" + URL );
}

Session. setattribute (goto_url_key, URL );


}
Public void setbasedao (ibasedao basedao)
{
This. basedao = basedao;
}

}

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.