package com.example.helloworld.filter;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Component
@WebFilter(urlPatterns = "/**/*")
public class UrlFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Filter created");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
String requestUri = req.getRequestURI();
System.out.println("Filter:"+requestUri);
chain.doFilter(request,response);
}
@Override
public void destroy() {
System.out.println("Filter destroyed");
}
}
- 添加@Component和@WebFilter注解
- 实现init、doFilter、destroy三个方法
- doFilter方法里需要使用chain.doFilter方法放行
拦截器实现:
https://blog.csdn.net/qq_42026590/article/details/108494624