一、Filter原理
package com.wzh.finalwork.Filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(urlPatterns ="/*" )
public class DemoFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
Filter.super.init(filterConfig);
System.out.println("初始化成功!");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("过滤启动!");
}
@Override
public void destroy() {
Filter.super.destroy();
System.out.println("销毁成功!");
}
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("过滤启动!");
filterChain.doFilter(servletRequest,servletResponse);//允许访问对应操作
}
二、过滤器验证
![](https://img-blog.csdnimg.cn/direct/6d504fb1d81e42aeb5ffce53e57c46aa.png)
@WebFilter(urlPatterns ="/loginnn" )//定义过滤器拦截请求,/*是所有请求
public class DemoFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
Filter.super.init(filterConfig);
System.out.println("初始化成功!");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("过滤启动!");
filterChain.doFilter(servletRequest,servletResponse);//允许访问对应操作
System.out.println("放行后!");
}
@Override
public void destroy() {
Filter.super.destroy();
System.out.println("销毁成功!");
}
}
package com.wzh.finalwork.Filter;
import com.alibaba.fastjson.JSONObject;
import com.wzh.finalwork.Pojo.Result;
import com.wzh.finalwork.Utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Slf4j
@WebFilter(urlPatterns = "/*")
public class LoginFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//获取请求路径
HttpServletRequest request=(HttpServletRequest) servletRequest;
HttpServletResponse response=(HttpServletResponse) servletResponse;
String requestURL = request.getRequestURL().toString();
log.info(requestURL);
//判断是不是登录路径
if(requestURL.contains("login")){
log.info("登录操作直接放行");
filterChain.doFilter(request,response);
return;//让代码不用继续执行
}
//获取请求头的token
String token = request.getHeader("token");
//判断token是否存在,不存在的处理过程
if(!StringUtils.hasLength(token)){
log.info("请求头为空");
Result r=Result.error("not_login");
//把r转化成json格式数据
String s = JSONObject.toJSONString(r);
response.getWriter().write(s);//响应json格式的字符串
return;
}
//校验jwt令牌是否有效
try {
JwtUtils.parseJWT(token);
}catch (Exception e){
e.printStackTrace();
log.info("解析失败!返回错误");
Result r=Result.error("not_login");
//把r转化成json格式数据
String s = JSONObject.toJSONString(r);
response.getWriter().write(s);//响应json格式的字符串
return;
}
//放行
log.info("成功放行!!!");
filterChain.doFilter(request, response);
}
}
三、Apifox的使用
![](https://img-blog.csdnimg.cn/direct/e440eaf824ca49aeacd7b11cf62f710d.png)
![](https://img-blog.csdnimg.cn/direct/3b3a4eecd59d4582a51c74351ee0c30a.png)