1. Web. XML configuration
1<filter>2<description> Filter whether login </description>3<filter-name>encodingFilter</filter-name>4<filter-class>com.stxx.manager.filter.encodingfilter</filter-class>5<init-param>6<description> Configure white list </description>7<param-name>noCheck</param-name>8<param-value>9/login.jsp,Ten/register.jsp One</param-value> A</init-param> -<init-param> -<description> filter not successfully turned to address </description> the<param-name>redirectPath</param-name> -<param-value>/login.jsp</param-value> -</init-param> -</filter> +<filter-mapping> -<filter-name>encodingFilter</filter-name> +<url-pattern>*.jsp</url-pattern> A</filter-mapping>
2. Create filters
1 import java.io.IOException;2 import java.util.ArrayList;3 import java.util.List;4 import Javax.servlet.Filter;5 import Javax.servlet.FilterChain;6 import Javax.servlet.FilterConfig;7 import javax.servlet.ServletException;8 import javax.servlet.ServletRequest;9 import Javax.servlet.ServletResponse;Ten import javax.servlet.http.HttpServletRequest; One import Javax.servlet.http.HttpServletResponse; A import org.apache.commons.lang.StringUtils; - import Org.apache.log4j.Logger; - the /** - * Login Filter configuration white list, blacklist - * @author zxf - * + */ - Public classEncodingfilter implements Filter { + ALogger log = Logger.getlogger (encodingfilter.class); at - Private StaticFinal String No_check ="NoCheck"; - Private StaticFinal String Redirect_path ="Redirectpath"; - PrivateList<string> nochecklist =NewArraylist<string>(); - PrivateString Redirectpath ="/login.jsp"; - in Public voidinit (filterconfig init) throws Servletexception { -Log.info ("Initialize filter ...."); toString nochecks =Init.getinitparameter (no_check); + if(Stringutils.isnotblank (nochecks)) { - if(Stringutils.indexof (Nochecks,",")!=-1){ the for(String No:noChecks.split (",")){ * Nochecklist.add (Stringutils.trimtoempty (No)); $ }Panax Notoginseng}Else{ - Nochecklist.add (nochecks); the } + } AString Path =Init.getinitparameter (redirect_path); the if(Stringutils.isnotblank (path)) { +Redirectpath =path; - } $ } $ - Privateboolean check (String path) { - if(Nochecklist = =NULL|| Nochecklist.size () <=0) the return false; - for(String s:nochecklist) {Wuyi if(Path.indexof (s) >-1) { the return true; - } Wu } - return false; About } $ - Public voidDoFilter (ServletRequest arg0, Servletresponse arg1, - Filterchain arg2) throws IOException, servletexception { -HttpServletRequest request =(HttpServletRequest) arg0; AHttpServletResponse response =(HttpServletResponse) arg1; +Log.info ("Filtered by filter ...."); theString ContextPath =Request.getcontextpath (); - if("/". Equals (ContextPath)) {contextpath="";} $ if(Check (Request.getrequesturi ())) { theLog.info ("White list"); the Arg2.dofilter (request, response); the}Else{ theLog.info ("blacklist"); -Response.sendredirect (Response.encodeurl (contextpath+redirectpath)); in } the } the About Public voiddestroy () { theLog.info ("destroy filter ...."); the } the +}
Servlet Filter Configuration white list, blacklist