Spring Boot (入门) 过滤器
程序员文章站
2022-04-28 16:44:51
...
过滤器(Filter)文件
MyFilter.java
/**filterName 给过滤器取一个外号
- urlPatterns 配置过滤器的拦截地址
- @author Maibenben
*/
@WebFilter(filterName=“firstFiler”,urlPatterns="/*")
public class MyFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
System.out.println("MyFilter-->init 初始化");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest req=(HttpServletRequest) request;
System.out.println("MyFilter-->init 过滤中。。"+request);
System.out.println("MyFilter-->init 过滤中。。");
String url=req.getRequestURI().toString();
//如果后面还有过滤器就表示执行下一个过滤器,如果没有,就表示过滤完成
chain.doFilter(request, response);
}
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("MyFilter-->init 被销毁");
}
}