过滤器:
package com.sparrow.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @Author: 诉衷情の麻雀
* @Description: TODO
* @DateTime: 2023/7/18 21:02
**/
public class LoginCheckServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取用户名和密码
String username = req.getParameter("username");
String password = req.getParameter("password");
if("123456".equals(password)){
req.getSession().setAttribute("username",username);
req.getRequestDispatcher("/manage/admin.jsp").forward(req, resp);
}else {
req.getRequestDispatcher("/login.jsp").forward(req, resp);
}
System.out.println("LoginCheckServlet被调用了..");
}
}
public class ManageFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
//当fTomcat创建filter后就会调用该方法,进行初始化
System.out.println("ManageFilter init被调用...");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//每次调用该filter doFilter方法就会被调用
System.out.println("ManageFilter doFilter被调用...");
//在调用过滤器前,request对象已经被创建并封装
//所以我们这里可以通过ServletRequest获取很多信息,比如访问url,session
//比如访问的参数...就可以做事务管理 数据获取 日志管理等
HttpSession session = ((HttpServletRequest) servletRequest).getSession();
if (session.getAttribute("username") != null) {
filterChain.doFilter(servletRequest, servletResponse);
}else { //说明没有登录过..回到登录页面
servletRequest.getRequestDispatcher("/login.jsp").forward(servletRequest, servletResponse);
}
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
System.out.println("ManageFilter被销毁了");
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>管理后台登录title>
head>
<body>
<h1>管理后台登录h1>
<form action="<%=request.getContextPath()%>/loginCheckServlet" method="post">
用户名: <input type="text" name="username"/><br/><br/>
密 码: <input type="password" name="password"><br/><br/>
<input type="submit" value="用户登录"/>
form>
body>
html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>后台管理title>
<base href="<%=request.getContextPath()%>/manage/"/>
head>
<body>
<h1>后台管理h1>
<a href="#">用户列表a>||<a href="#">添加用户a>||<a href="#">删除用户a>
<hr/>
<img src="3.png " height="300"/>
body>
html>
<filter>
<filter-name>ManageFilterfilter-name>
<filter-class>com.sparrow.filter.ManageFilterfilter-class>
filter>
<filter-mapping>
<filter-name>ManageFilterfilter-name>
<url-pattern>/manage/*url-pattern>
filter-mapping>
<servlet>
<servlet-name>LoginCheckServletservlet-name>
<servlet-class>com.sparrow.servlet.LoginCheckServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>LoginCheckServletservlet-name>
<url-pattern>/loginCheckServleturl-pattern>
servlet-mapping>
url-pattern
:Filter的拦截路径,即浏览器在请求什么位置的资源时,过滤器会进行拦截过滤/a.jsp
对应的请求地址http://ip[域名]:port/工程路径/a.jsp会拦截/manage/*
对应的请求地址http://ip[域名]:port/工程路径/manage/xx,即web工程manage目录下所有资源会拦截*.jsp
后缀名可变,比如*.action *.do 等等对应的请求地址http://ip[域名]:port/工程路径/xx.jsp,后缀名名为.jsp请求会拦截ServletRequest
、ServletResponse
、FilterChain
对象,并通过doFilter传入public class SparrowFilterConfig implements Filter {
private String ip; //从配置文件中获取ip
@Override
public void init(FilterConfig filterConfig) throws ServletException {
//通过filterConfig获取相关的参数
String filterName = filterConfig.getFilterName();
ip = filterConfig.getInitParameter("ip");
ServletContext servletContext = filterConfig.getServletContext();
Enumeration<String> parameterNames = filterConfig.getInitParameterNames();
while (parameterNames.hasMoreElements()) {
System.out.println("名字=" + parameterNames.nextElement());
}
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//通过forbidden ip来控制
//先获取到访问ip
String remoteAddr = servletRequest.getRemoteAddr();
if (remoteAddr.contains(ip)) {
System.out.println("封杀该IP");
servletRequest.getRequestDispatcher("/login.jsp").forward(servletRequest, servletResponse);
}
}
@Override
public void destroy() {
Filter.super.destroy();
}
}
public class AFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("AFilter~~~" + Thread.currentThread().getId());
System.out.println("AFilter doFilter的前置代码");
System.out.println("执行AFilter ");
filterChain.doFilter(servletRequest, servletResponse);
System.out.println("AFilter doFilter的后置代码");
}
}
public class BFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("BFilter~~~" + Thread.currentThread().getId());
System.out.println("BFilter doFilter的前置代码");
System.out.println("执行BFilter ");
filterChain.doFilter(servletRequest, servletResponse);
System.out.println("BFilter doFilter的后置代码");
}
}
<filter>
<filter-name>AFilterfilter-name>
<filter-class>com.sparrow.filter.AFilterfilter-class>
filter>
<filter-mapping>
<filter-name>AFilterfilter-name>
<url-pattern>/admin/*url-pattern>
filter-mapping>
<filter>
<filter-name>BFilterfilter-name>
<filter-class>com.sparrow.filter.BFilterfilter-class>
filter>
<filter-mapping>
<filter-name>BFilterfilter-name>
<url-pattern>/admin/*url-pattern>
filter-mapping>