最简单使用的配置登录拦截器(过滤器)的方法(SSH)

最简单使用的配置登录拦截器(过滤器)的方法,可集成于大部分框架中首先创建loginfilter类 继承httpservlet 实现Filter接口 注意是javax.servlet包下的filter


[java] view plain copy
print ?
  1. package com.fiss.controller;  
  2.   
  3. import java.io.IOException;  
  4. import java.lang.reflect.Method;  
  5.   
  6. import javax.servlet.Filter;  
  7. import javax.servlet.FilterChain;  
  8. import javax.servlet.FilterConfig;  
  9. import javax.servlet.RequestDispatcher;  
  10. import javax.servlet.ServletException;  
  11. import javax.servlet.ServletRequest;  
  12. import javax.servlet.ServletResponse;  
  13. import javax.servlet.http.HttpServlet;  
  14. import javax.servlet.http.HttpServletRequest;  
  15. import javax.servlet.http.HttpServletResponse;  
  16. import javax.servlet.http.HttpSession;  
  17.   
  18. import org.springframework.aop.MethodBeforeAdvice;  
  19.   
  20. import com.fiss.entity.Admin;  
  21.   
  22. public class filter extends HttpServlet implements Filter {  
  23.   
  24.     public void doFilter(ServletRequest req, ServletResponse resp,  
  25.             FilterChain chain) throws IOException, ServletException {  
  26.   
  27.         HttpServletRequest request = (HttpServletRequest) req;  
  28.         HttpServletResponse response = (HttpServletResponse) resp;  
  29.         HttpSession session = request.getSession(true);  
  30.   
  31.         String url = request.getRequestURI();  
  32.         Admin admin = (Admin) session.getAttribute("admin");  
  33.         if (admin == null && url.indexOf("login.jsp") == -1  
  34.                 && !url.equals("/fiss/") && !url.equals("/fiss/admin.do")) {  
  35.             String location = "/manager/login.jsp";  
  36.             request.getRequestDispatcher(location).forward(request, response);  
  37.   
  38.             System.out.println("成功拦截到外星人企图入侵网站后台   :  " + url);  
  39.             response.setHeader("Cache-Control""no-store");  
  40.             response.setDateHeader("Expires"0);  
  41.             response.setHeader("Pragma""no-cache");  
  42.         } else {  
  43.             chain.doFilter(request, response);  
  44.         }  
  45.     }  
  46.   
  47.     public void init(FilterConfig arg0) throws ServletException {  
  48.     }  
  49.   
  50. }  
package com.fiss.controller;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
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;

import org.springframework.aop.MethodBeforeAdvice;

import com.fiss.entity.Admin;

public class filter extends HttpServlet implements Filter {

	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		HttpSession session = request.getSession(true);

		String url = request.getRequestURI();
		Admin admin = (Admin) session.getAttribute("admin");
		if (admin == null && url.indexOf("login.jsp") == -1
				&& !url.equals("/fiss/") && !url.equals("/fiss/admin.do")) {
			String location = "/manager/login.jsp";
			request.getRequestDispatcher(location).forward(request, response);

			System.out.println("成功拦截到外星人企图入侵网站后台   :  " + url);
			response.setHeader("Cache-Control", "no-store");
			response.setDateHeader("Expires", 0);
			response.setHeader("Pragma", "no-cache");
		} else {
			chain.doFilter(request, response);
		}
	}

	public void init(FilterConfig arg0) throws ServletException {
	}

}




写好以后在web.xml中配置拦截器

[html] view plain copy
print ?
  1. <filter>  
  2.         <filter-name>loginFilterfilter-name>  
  3. //这里的filterclass是你创建的fitler类  
  4.         <filter-class>com.fiss.controller.LoginFilterfilter-class>  
  5.     filter>  
  6.     <filter-mapping>  
  7.         <filter-name>loginFilterfilter-name>  
  8. //定义规则  
  9.         <url-pattern>*.jspurl-pattern>  
  10.     filter-mapping>  

		loginFilter
//这里的filterclass是你创建的fitler类
		com.fiss.controller.LoginFilter
	
	
		loginFilter
//定义规则
		*.jsp
	

配置完成   启动服务器没有报错  尝试直接访问url页面  控制台成功输出拦截信息 OK

你可能感兴趣的:(spring)