Springmvc Framework is a very powerful Java web framework, now the most mainstream is the framework of spring MVC today we learn the springmvcd login blocker filter
First of all, SPRINGMVC has a unified portal Dispatcherservlet, and all requests are handled by Dispatcherservlet.
Dispatcherservlet is a front controller, configured in Web. XML, primarily to intercept the corresponding request and then send the request to the Controller for processing according to the corresponding rules
First configure the interception request in Web. xml
<!--filter login blocker--><filter> <!--intercept request for processing class-- <filter-name>securityservlet</ filter-name> <filter-class> org.andy.shop.utils.securityservlet</filter-class> </ filter> <filter-mapping> <filter-name>SecurityServlet</filter-name> < url-pattern>*.jsp</url-pattern> </filter-mapping> <filter-mapping> <!-- Interception format .do--> <filter-name>SecurityServlet</filter-name> <url-pattern>*.do</ Url-pattern>
Here we use the. Do intercept format, which means that all requests have a. Do request will be intercepted by us to do login processing, if the. do write "/", it will intercept all requests, will cause JS files, CSS files and other files can not be accessed
What we're writing here is inheriting the HttpServlet class implementation of the filter interface
Package Org.andy.shop.utils;import Java.io.ioexception;import Javax.servlet.filter;import Javax.servlet.filterchain;import Javax.servlet.filterconfig;import Javax.servlet.servletexception;import Javax.servlet.servletrequest;import Javax.servlet.servletresponse;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Javax.servlet.http.HttpSession; /** * Filter Login blocker * @author lyy * @date 2016-05-05 */public class Securityservlet extends HttpServlet implements filter{/ * * */private static final long serialversionuid = 1l;public void init (Filterconfig filterconfig) throws Servletexceptio n {}public void DoFilter (ServletRequest srequest, Servletresponse sresponse, Filterchain chain) throws IOException, servletexception {httpservletrequest request= (httpservletrequest) srequest; HttpServletResponse response = (httpservletresponse) sresponse; HttpSession session = Request.getsession (true); String Usercode = (string) request.getremoteuser ();//Login String User_role = (string) session.getattribute (constants.use RID);//Login role String Url=request.getrequesturi ();//Get Request path System.out.println (URL); if (User_role = = NULL | | "". Equals (User_role)) {//If the login ID is empty//determines that the obtained path is not empty and is not accessing the login page or performing a logon operation, jump if (url!=null &&!url.equ ALS ("") && (Url.contains ("userdetail.do")) {//If the request URL is not empty and the requested URL contains a userdetail.do request Response.sendre Direct (Request.getcontextpath () + "/login.do"); Return }} chain.dofilter (Srequest, sresponse);//Jump return;}}
When we visit localhost:8080/test/userdetai.do, if not logged in the verification will jump to the corresponding login page
Java SPRINGMVC Filter Login blocker