Spring MVC Filter-Login Filter

Source: Internet
Author: User

The following code is the code that inherits the Onceperrequestfilter implementation login filtering:

/** * *  @author  geloin *  @date  2012-4-10  afternoon 2:37:38 */package  com.test.spring.filter;import java.io.IOException;import java.io.PrintWriter;import  javax.servlet.filterchain;import javax.servlet.servletexception;import  javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;import  org.springframework.web.filter.onceperrequestfilter;/** *  Login Filter  *  *  @author  geloin *  @date  2012-4-10  pm 2:37:38 */public class sessionfilter  extends onceperrequestfilter {/* *  (non-javadoc)  *  *  @see  *  org.springframework.web.filter.onceperrequestfilter#dofilterinternal ( *  javax.servlet.http.httpservletrequest, * javax.servlet.http.httpservletresponse,  Javax.servlet.FilterChain)  */@Overrideprotected  void dofilterinternal (httpservletrequest&Nbsp;request,httpservletresponse response, filterchain filterchain) throws ServletException , ioexception {//  non-filtered uristring[] notfilter = new string[] {  " Login.html ", " index.html " };//  requested Uristring uri = request.getrequesturi ();// Filter if  (uri.indexof ("background")  != -1)  {//  Filter Boolean if background is included in  uri  doFilter = true;for  (String s : notfilter)  {if  (Uri.indexof (s) &NBSP;!=&NBSP;-1)  {//  do not filter dofilter = false;break if the URI contains a URI that is not filtered;}} if  (doFilter)  {//  perform filtering//  get the login entity object obj = request.getsession () from the session. GetAttribute ("Logineduser");if  (null == obj)  {//  if the login entity does not exist in session, the popup prompts to log back in//   Set the character set of request and response to prevent garbled request.setcharacterencoding ("UTF-8"); Response.setcharacterencoding ("UTF-8"); Printwriter out = response.getwriteR (); string loginpage =  "..."; Stringbuilder builder = new stringbuilder (); Builder.append ("<script type=\" text/ Javascript\ ">") builder.append ("Alert (' page expires, please login again! ‘);"); Builder.append ("window.top.location.href="); Builder.append (LoginPage); Builder.append ("';"); Builder.append ("</script>"); Out.print (builder.tostring ());}  else {//  if the login entity exists in the session, continue to Filterchain.dofilter (Request, response);}}  else {//  continue Filterchain.dofilter (request, response) If filtering is not performed;}}  else {//  if the URI does not contain background, continue filterchain.dofilter (Request, response);}}

After you finish writing the filter, you need to configure it in Web. xml:

<filter><filter-name>sessionFilter</filter-name><filter-class> Com.test.spring.filter.sessionfilter</filter-class></filter><filter-mapping><filter-name >sessionFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>


Spring MVC Filter-Login Filter

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.