一、过滤器的作用和概述
1.1 简述
人—>检票员(filter)—> 电影院。
注意:配置多个filter的时候,要设置编号id,值越小,优先级越高,越先执行。
在3.0之后新增@WebFilter注解,当使用注解配置多个Filter时,用户无法控制其执行顺序,此时Filter过滤的顺序是按照Filter的类名来控制的,按自然排序的规则。
1.2 使用场景
场景:权限控制、用户登录(非前端后端分离场景)等,过滤非法登录
1.3 依赖支持
<!--spring boot的启动类 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
1.4 过程实现
1.启动类里面增加 @ServletComponentScan,进行扫描
2.新建一个Filter类,implements Filter,并实现对应的接口
3. @WebFilter 标记一个类为filter,被spring进行扫描
urlPatterns:拦截规则,支持正则
4.控制chain.doFilter的方法的调用,来实现是否通过放行还是不放行,
如果放行: FilterChain.dofilter(res,rep);
如果不放行:调转到指定页面web应用resp.sendRedirect("/index.html"); return
一、启动类增加注解
package com.kech.reggie;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.ComponentScan;
@Slf4j
@ServletComponentScan("com.kech.reggie.filter")
@SpringBootApplication
public class ReggieApplication {
public static void main(String[] args) {
SpringApplication.run(ReggieApplication.class, args);
log.info("项目启动成功");
}
}
二、创建Filter过滤器
package com.kech.reggie.filter;
import com.alibaba.fastjson.JSON;
import com.kech.reggie.utils.R;
import org.springframework.util.AntPathMatcher;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(urlPatterns = "/*", filterName = "LoginFilter")
public class LoginFilter implements Filter {
// 路径匹配器,支持通配符
public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
@Override
public void init(FilterConfig filterConfig) throws ServletException {
Filter.super.init(filterConfig);
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
//1.获取本次请求的URL
String requestURI = request.getRequestURI();
//定义不需要处理的请求路径
String[] urls = new String[]{
"/employee/login",
"/employee/logout",
"/backend/**",
"/front/**"
};
//2.判断本次请求是否需要处理
boolean check = check(urls, requestURI);
//3.如果不需要处理,则直接放行
if (check) {
filterChain.doFilter(request, response);
return;
}
//4.判断登录状态,如果已经登录,则直接放行
if (request.getSession().getAttribute("employee") != null) {
filterChain.doFilter(request, response);
return;
}
//5.如果未登录则返回未登录结果,通过输出流方式向客户端相应数据
response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
return;
}
@Override
public void destroy() {
Filter.super.destroy();
}
/**
* 路径匹配,检查请求是否放行
* @param requestURI
* @return
*/
public boolean check(String[] urls, String requestURI) {
for (String url : urls) {
boolean match = PATH_MATCHER.match(url, requestURI);
if (match) {
return true;
}
}
return false;
}
}