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

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


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中配置拦截器

<filter>
		<filter-name>loginFilter</filter-name>
//这里的filterclass是你创建的fitler类
		<filter-class>com.fiss.controller.LoginFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>loginFilter</filter-name>
//定义规则
		<url-pattern>*.jsp</url-pattern>
	</filter-mapping>

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



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值