Using filter filter to implement login authorization

Source: Internet
Author: User

1. First write a permission filtering filter class to implement the filter interface
Import Javax.servlet.Filter;
Import Javax.servlet.FilterConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;
Import Javax.servlet.FilterChain;
Import java.io.IOException;
Import Javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpSession;
Import Javax.servlet.http.HttpServletResponse;

public class Rightfilter
Implements Filter ... {
public void init (Filterconfig filterconfig) throws Servletexception ... {
}

public void Dofilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, servletexception ... {
HttpServletRequest req = (httpservletrequest) request;
HttpServletResponse res = (httpservletresponse) response;

HttpSession session = Req.getsession (true);

User name information taken from the session
String username = (string) session.getattribute ("username");

If you don't get the user information, jump to the landing page
if (username = null | | "". Equals (username)) ... {
Jump to landing page
Res.sendredirect ("http://" +req.getheader ("Host") + "/login.jsp");
}
else ... {
Have logged in, continue this request
Chain.dofilter (Request,response);
}
}

public void Destroy () ... {
}
}

2. Then configure the JSP files that require login authorization in Web.xml:

     A. If a specific JSP file (such as a.jsp) requires login validation < Web-app >
   ....
   < filter >
     < filter-name > right </filter-name >
&nbs p;    < Filter-class > com.taihuatalk.taihua.common.RightFilter </filter-class >
   </filter >

   < filter-mapping >
     < Filter-name > Right </filter-name >
     < url-pattern >/a.jsp </url-pattern >
   </filter-mapping >
   ....
</Web-app >

    B. If it is a directory (such as A/directory) the entire directory of files need to login verification: < Web-app >
  
   < filter >
     < filter-name > right </filter-name >
&nbs p;    < Filter-class > com.taihuatalk.taihua.common.RightFilter </filter-class >
   </filter >

   < filter-mapping >
     < Filter-name > Right </filter-name >
     < url-pattern >/a/* </url-pattern &G T
   </filter-mapping >
   ....
</web-app >

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.