Interceptors

    在 Java 配置中,你可以注册拦截器来应用于 传入的请求,如下例所示:

    1. @Configuration
    2. @EnableWebMvc
    3. public class WebConfig implements WebMvcConfigurer {
    4. @Override
    5. public void addInterceptors(InterceptorRegistry registry) {
    6. registry.addInterceptor(new LocaleChangeInterceptor());
    7. registry.addInterceptor(new ThemeChangeInterceptor()).addPathPatterns("/**").excludePathPatterns("/admin/**");
    8. registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/secure/*");
    9. }
    10. }

    对应的 XML 如下

    1. <mvc:interceptors>
    2. <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
    3. <mvc:interceptor>
    4. <mvc:mapping path="/**"/>
    5. <mvc:exclude-mapping path="/admin/**"/>
    6. <bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"/>
    7. </mvc:interceptor>
    8. <mvc:interceptor>
    9. <mvc:mapping path="/secure/*"/>
    10. <bean class="org.example.SecurityInterceptor"/>
    11. </mvc:interceptor>
    12. </mvc:interceptors>