Chapter Index
OG

如何在Spring boot上配置WebRequestInterceptor

如何在Spring boot上配置WebRequestInterceptor

最近有用到WebRequestInterceptor的接口,它和HandleInterceptor所用的配置方法不同

HandleInterceptor的配置:

public class MyInterceptor implements HandleInterceptor {}

@Configuration
public class InterceptorConfig  implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor());
    }

}

WebRequestInterceptor配置:

public class MyWebRequestIntercepter implements WebRequestInterceptor {}

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(
                new WebRequestHandlerInterceptorAdapter(
                        new MyWebRequestIntercepter()));
    }

}

如上,你應該使用WebRequestHandlerInterceptorAdapter进行包装WebRequestInterceptor