Java拦截器(Interceptor)

2020-07-25  本文已影响0人  黑曼巴yk

前言

过滤器(Filter)关注的是web请求,拦截器(Interceptor)可以理解为“拒你所想拒”。拦截器关注的是方法调用。比如拦截敏感词汇。

使用

HandlerInterceptorAdapter

public class HandlerInterceptorAdapter implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.sendError(403);
        return false;
    }
}

注册

MyInterceptorConfig

@Configuration
public class MyInterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HandlerInterceptorAdapter()).addPathPatterns("/**");
    }
}
上一篇下一篇

猜你喜欢

热点阅读