如何仅为注册的映射(端点)添加spring拦截器

时间:2018-08-14 08:41:58

标签: java spring spring-rest

我遇到与HandlerInterceptorAdapter有关的问题。

拦截器:

@Component
public class RequestInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler)
        throws Exception {
        //some logic
        return true;
    }

}

WebMvcConfigurer

@Configuration
public class FsWebMvcConfigurer implements WebMvcConfigurer {

    private static final String API_PATTERN = "/**/api/**/";

    private final RequestInterceptor requestInterceptor;

    @Autowired
    public FsWebMvcConfigurer(final RequestInterceptor requestInterceptor) {
        this.requestInterceptor = requestInterceptor;
    }

    @Override
    public void addInterceptors(final InterceptorRegistry registry) {
        registry.addInterceptor(requestInterceptor).addPathPatterns(API_PATTERN);
    }
}

它能按预期工作-模式为/**/api/**/的每个请求都将被拦截。

不幸的是,它也适用于未注册的端点-我不希望这种行为。如何指定已注册的拦截器仅适用于已注册的映射?

注意:我不想由我自己确切指定每个现有端点(例如,将所有端点添加为模式)

0 个答案:

没有答案