IT虾米网

web Filter过滤详解

xmjava 2019年06月21日 编程语言 208 0

1、添加过滤器类:

package com.bigdatalearning.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.HttpServletRequest; 
import javax.servlet.http.HttpSession; 
 
import com.bigdatalearning.entity.UserEntity; 
 
public class SiteFilter implements Filter { 
	public void destroy() { 
	} 
 
	public void doFilter(ServletRequest sreq, ServletResponse response, FilterChain filterChain) 
			throws IOException, ServletException { 
		UserEntity user_name = null; 
		HttpServletRequest request = (HttpServletRequest) sreq; 
		user_name = (UserEntity)request.getSession().getAttribute("user"); 
		HttpSession session = request.getSession(false); 
		if (user_name == null) { 
			request.getRequestDispatcher("/welcome.jsp").forward(request, response); 
		} else { 
			request.getRequestDispatcher(request.getServletPath()).forward(request, response); 
		} 
 
		if (request.getRequestURI().endsWith("download.do")) { 
 
		} 
	} 
 
	public void init(FilterConfig filterConfig) throws ServletException { 
	 
	} 
} 




2、web.xml配置文件(现在是过滤所有接口):

<filter> 
		<filter-name>SiteFilter</filter-name> 
		<filter-class>com.bigdatalearning.utils.SiteFilter</filter-class> 
	</filter> 
	<filter-mapping> 
		<filter-name>SiteFilter</filter-name> 
		<url-pattern>/*</url-pattern> 
	</filter-mapping>



3、web.xml文件中过滤不需要加项目名称,Filter类中要加:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 
  <display-name>Spring4 MVC Hibernate4 Application</display-name> 
  <filter> 
    <filter-name>springUtf8Encoding</filter-name> 
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
    <init-param> 
      <param-name>encoding</param-name> 
      <param-value>UTF-8</param-value> 
    </init-param> 
    <init-param> 
      <param-name>forceEncoding</param-name> 
      <param-value>true</param-value> 
    </init-param> 
  </filter> 
  <filter-mapping> 
    <filter-name>springUtf8Encoding</filter-name> 
    <url-pattern>/*</url-pattern> 
  </filter-mapping> 
  <session-config> 
    <session-timeout>30</session-timeout> 
  </session-config> 
  <listener> 
    <listener-class>com.bigdatalearning.utils.SessionListener</listener-class> 
  </listener> 
  <servlet> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
  </servlet> 
  <servlet-mapping> 
    <servlet-name>mvc-dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
  </servlet-mapping> 
  <welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
  </welcome-file-list> 
  <filter> 
    <filter-name>SiteFilter</filter-name> 
    <filter-class>com.bigdatalearning.utils.SiteFilter</filter-class> 
  </filter> 
  <!-- Controller 过滤器 --> 
  <filter-mapping> 
    <filter-name>SiteFilter</filter-name> 
    <url-pattern>/courseAction/*</url-pattern> 
  </filter-mapping> 
  <filter-mapping> 
    <filter-name>SiteFilter</filter-name> 
    <url-pattern>/managePageAction/*</url-pattern> 
  </filter-mapping> 
  <filter-mapping> 
    <filter-name>SiteFilter</filter-name> 
    <url-pattern>/userAction/*</url-pattern> 
  </filter-mapping> 
  <filter-mapping> 
    <filter-name>SiteFilter</filter-name> 
    <url-pattern>/homepageAction/*</url-pattern> 
  </filter-mapping> 
    <!-- Html 过滤器 --> 
  <filter-mapping> 
    <filter-name>SiteFilter</filter-name> 
    <url-pattern>/webMaster.html</url-pattern> 
  </filter-mapping> 
  <filter-mapping> 
    <filter-name>SiteFilter</filter-name> 
    <url-pattern>/courseMaster.html</url-pattern> 
  </filter-mapping> 
  <filter-mapping> 
    <filter-name>SiteFilter</filter-name> 
    <url-pattern>/teacherMaster.html</url-pattern> 
  </filter-mapping> 
  <filter-mapping> 
    <filter-name>SiteFilter</filter-name> 
    <url-pattern>/courseHourMaster.html</url-pattern> 
  </filter-mapping> 
    <filter-mapping> 
    <filter-name>SiteFilter</filter-name> 
    <url-pattern>/applicantMaster.html</url-pattern> 
  </filter-mapping> 
  <filter-mapping> 
    <filter-name>SiteFilter</filter-name> 
    <url-pattern>/blogEdit.html</url-pattern> 
  </filter-mapping> 
  <filter-mapping> 
    <filter-name>SiteFilter</filter-name> 
    <url-pattern>/liepinEdit.html</url-pattern> 
  </filter-mapping> 
</web-app>




4、有一些网站不需要过滤,可做静态块配置:

package com.bigdatalearning.utils; 
 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
 
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.HttpServletRequest; 
 
import com.bigdatalearning.entity.ManagerEntity; 
 
public class SiteFilter implements Filter { 
	 
	private static List<String> commonPages = new ArrayList<String>(); 
	 
	static { 
		commonPages.add("/bigdataLearning/"); 
		commonPages.add("/bigdataLearning/index.html"); 
		commonPages.add("/bigdataLearning/homepageAction/getAllHomePageInfo"); 
		commonPages.add("/bigdataLearning/homepageAction/getAboutus"); 
		commonPages.add("/bigdataLearning/homepageAction/getAllHire"); 
		commonPages.add("/bigdataLearning/homepageAction/getHireById"); 
		commonPages.add("/bigdataLearning/courseAction/queryAllCourse"); 
		commonPages.add("/bigdataLearning/courseAction/queryCourseById"); 
		commonPages.add("/bigdataLearning/userAction/queryTeachers"); 
		commonPages.add("/bigdataLearning/userAction/queryTeacherById"); 
		commonPages.add("/bigdataLearning/userAction/userRegisterRequest"); 
		commonPages.add("/bigdataLearning/userAction/userRegisterCheck"); 
		commonPages.add("/bigdataLearning/userAction/userLogin"); 
		commonPages.add("/bigdataLearning/userAction/loginCheck"); 
		commonPages.add("/bigdataLearning/managePageAction/getAllBlogs"); 
		commonPages.add("/bigdataLearning/managePageAction/getBlogById"); 
	} 
	 
	public void destroy() { 
	} 
 
	public void doFilter(ServletRequest sreq, ServletResponse response, FilterChain filterChain) 
			throws IOException, ServletException { 
		HttpServletRequest request = (HttpServletRequest) sreq; 
		String url = request.getRequestURI(); 
		if (commonPages.contains(url)) { 
			request.getRequestDispatcher(request.getServletPath()).forward(request, response); 
		} else { 
			//UserEntity user_name = null; 
			ManagerEntity manager_name = null; 
			//user_name = (UserEntity)request.getSession().getAttribute("user"); 
			manager_name = (ManagerEntity)request.getSession().getAttribute("manager"); 
			if (manager_name == null) { 
				request.getRequestDispatcher("/welcome.jsp").forward(request, response); 
			} else { 
				request.getRequestDispatcher(request.getServletPath()).forward(request, response); 
			}			 
		} 
	} 
 
	public void init(FilterConfig filterConfig) throws ServletException { 
	 
	} 
} 


发布评论

分享到:

IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

springmvc 静态资源访问详解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。