通过路径模式排除 Spring Request HandlerInterceptor

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34970179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:56:10  来源:igfitidea点击:

Exclude Spring Request HandlerInterceptor by Path-Pattern

springinterceptor

提问by Ron Zoosk

I know we can map different url to different interceptor, or we can map multiple url to single interceptor too. I am just curious to know if we also have exclude option. for example if I have 50 url mapping in application and except 1 mapping I want to call interceptor for all so rather than writing configuration for 49 mapping can I just mention * and one exclude to the 50th url?

我知道我们可以将不同的 url 映射到不同的拦截器,或者我们也可以将多个 url 映射到单个拦截器。我只是想知道我们是否也有排除选项。例如,如果我在应用程序中有 50 个 url 映射,除了 1 个映射,我想为所有对象调用拦截器,而不是为 49 个映射编写配置,我可以只提到 * 和第 50 个 url 的一个排除吗?

回答by fateddy

HandlerInterceptors can be applied or excluded to (multiple) specific url's or url-patterns.

HandlerInterceptors 可以应用于或排除(多个)特定的 url 或 url-patterns。

See the MVC Interceptor Configuration.

请参阅MVC 拦截器配置

Here are the examples from the documentation

以下是文档中的示例

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LocaleInterceptor());
        registry.addInterceptor(new ThemeInterceptor()).addPathPatterns("/**").excludePathPatterns("/admin/**");

        // multiple urls (same is possible for `exludePathPatterns`)
        registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/secure/*", "/admin/**", "/profile/**");
    }
}

or using XML config

或使用 XML 配置

<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
    <mvc:interceptor>
        <mvc:mapping path="/**"/>
        <mvc:exclude-mapping path="/admin/**"/>
        <bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"/>
    </mvc:interceptor>
    <mvc:interceptor>
        <!-- intercept multiple urls -->
        <mvc:mapping path="/secure/*"/>
        <mvc:mapping path="/admin/**"/>
        <mvc:mapping path="/profile/**"/>
        <bean class="org.example.SecurityInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>

回答by Rajesh Somasundaram

I think in spring-boot 2.0 version, this have changed a lot right now. Below is the implementation you can easily add and configure the path pattern.

我认为在 spring-boot 2.0 版本中,这现在已经改变了很多。下面是您可以轻松添加和配置路径模式的实现。

@Component
public class ServiceRequestAppConfig implements WebMvcConfigurer {

    @Autowired
    ServiceRequestInterceptor sri;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String pathPattern = "/admin/**";
        registry.addInterceptor(sri).excludePathPatterns(pathPattern);
    }

}


@Component
public class ServiceRequestInterceptor extends HandlerInterceptorAdapter {

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

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex){
         // Some useful techniques
    }

}