过滤器Filter

10 篇文章 0 订阅
27 篇文章 0 订阅

注册filter(配置的过滤链)

@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean filterRegistrationBean (){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new FilterModel());
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean2 (){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new FilterModel2());
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }
}

 

实现接口

注意import

import javax.servlet.*;
import javax.servlet.FilterConfig;
public class FilterModel implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("init1");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("doFilter1");
        String name = servletRequest.getParameter("name");
        System.out.println("servletRequest = " + name);
        if(name.equals("qyc")){
            System.out.println("过滤1");
            filterChain.doFilter(servletRequest,servletResponse);
        }

    }

    @Override
    public void destroy() {
        System.out.println("destroy1");
    }
}

 

public class FilterModel2 implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("init2");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        
        System.out.println("doFilter2");
        System.out.println("httpServletRequest.getRequestURI() = " + httpServletRequest.getRequestURI());
        System.out.println("httpServletRequest.getRequestURL() = " + httpServletRequest.getRequestURL());
        System.out.println("httpServletRequest.getQueryString() = " + httpServletRequest.getQueryString());
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {
        System.out.println("destroy2");
    }
}

 

执行顺序   过滤器Filter   拦截器Interceptor  AOP

有关AOP和拦截器的内容请看上一篇博客

https://blog.csdn.net/qq_41835813/article/details/108501750   拦截器

https://blog.csdn.net/qq_41835813/article/details/108488261  AOP

init2
init1

日志

doFilter1   
servletRequest = qyc
过滤1
doFilter2
httpServletRequest.getRequestURI() = /login
httpServletRequest.getRequestURL() = http://localhost:8889/login
httpServletRequest.getPathInfo() = null
httpServletRequest.getQueryString() = name=qyc

username => qyc
controller  +  http://localhost:8889/login
around 环绕通知的目标方法名:test03
before 通过
URL : http://localhost:8889/login
HTTP_METHOD : GET
IP : 127.0.0.1
CLASS_METHOD : com.qyc.service.impl.TestServiceImpl.test03
ARGS : []

service

环绕通知结束   7
@doAfter
@doAfterReturning

postHandle
拦截之后

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:技术黑板 设计师:CSDN官方博客 返回首页
评论

打赏作者

Turing·

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值