Search in sources :

Example 11 with HandlerInterceptor

use of org.springframework.web.servlet.HandlerInterceptor in project spring-framework by spring-projects.

the class CrossOriginTests method getCorsConfiguration.

@Nullable
private CorsConfiguration getCorsConfiguration(@Nullable HandlerExecutionChain chain, boolean isPreFlightRequest) {
    assertThat(chain).isNotNull();
    if (isPreFlightRequest) {
        Object handler = chain.getHandler();
        assertThat(handler.getClass().getSimpleName()).isEqualTo("PreFlightHandler");
        DirectFieldAccessor accessor = new DirectFieldAccessor(handler);
        return (CorsConfiguration) accessor.getPropertyValue("config");
    } else {
        for (HandlerInterceptor interceptor : chain.getInterceptorList()) {
            if (interceptor.getClass().getSimpleName().equals("CorsInterceptor")) {
                DirectFieldAccessor accessor = new DirectFieldAccessor(interceptor);
                return (CorsConfiguration) accessor.getPropertyValue("config");
            }
        }
    }
    return null;
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Nullable(org.springframework.lang.Nullable)

Example 12 with HandlerInterceptor

use of org.springframework.web.servlet.HandlerInterceptor in project spring-framework by spring-projects.

the class RequestMappingInfoHandlerMappingTests method getHandlerMappedInterceptors.

@Test
void getHandlerMappedInterceptors() throws Exception {
    String path = "/foo";
    HandlerInterceptor interceptor = new HandlerInterceptor() {
    };
    MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] { path }, interceptor);
    TestRequestMappingInfoHandlerMapping mapping = new TestRequestMappingInfoHandlerMapping();
    mapping.registerHandler(new TestController());
    mapping.setInterceptors(mappedInterceptor);
    mapping.setApplicationContext(new StaticWebApplicationContext());
    HandlerExecutionChain chain = mapping.getHandler(new MockHttpServletRequest("GET", path));
    assertThat(chain).isNotNull();
    assertThat(chain.getInterceptorList().get(0)).isSameAs(interceptor);
    chain = mapping.getHandler(new MockHttpServletRequest("GET", "/invalid"));
    assertThat(chain).isNull();
}
Also used : MappedInterceptor(org.springframework.web.servlet.handler.MappedInterceptor) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 13 with HandlerInterceptor

use of org.springframework.web.servlet.HandlerInterceptor in project spring-framework by spring-projects.

the class MvcNamespaceTests method testResources.

@Test
@SuppressWarnings("deprecation")
public void testResources() throws Exception {
    loadBeanDefinitions("mvc-config-resources.xml");
    HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
    assertThat(adapter).isNotNull();
    RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
    ContentNegotiationManager manager = mapping.getContentNegotiationManager();
    ResourceHttpRequestHandler handler = appContext.getBean(ResourceHttpRequestHandler.class);
    assertThat(handler).isNotNull();
    assertThat(handler.getContentNegotiationManager()).isSameAs(manager);
    SimpleUrlHandlerMapping resourceMapping = appContext.getBean(SimpleUrlHandlerMapping.class);
    assertThat(resourceMapping).isNotNull();
    assertThat(resourceMapping.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE - 1);
    BeanNameUrlHandlerMapping beanNameMapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
    assertThat(beanNameMapping).isNotNull();
    assertThat(beanNameMapping.getOrder()).isEqualTo(2);
    ResourceUrlProvider urlProvider = appContext.getBean(ResourceUrlProvider.class);
    assertThat(urlProvider).isNotNull();
    Map<String, MappedInterceptor> beans = appContext.getBeansOfType(MappedInterceptor.class);
    List<Class<?>> interceptors = beans.values().stream().map(mappedInterceptor -> mappedInterceptor.getInterceptor().getClass()).collect(Collectors.toList());
    assertThat(interceptors).contains(ConversionServiceExposingInterceptor.class, ResourceUrlProviderExposingInterceptor.class);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestURI("/resources/foo.css");
    request.setMethod("GET");
    HandlerExecutionChain chain = resourceMapping.getHandler(request);
    assertThat(chain).isNotNull();
    assertThat(chain.getHandler() instanceof ResourceHttpRequestHandler).isTrue();
    MockHttpServletResponse response = new MockHttpServletResponse();
    for (HandlerInterceptor interceptor : chain.getInterceptorList()) {
        interceptor.preHandle(request, response, chain.getHandler());
    }
    ModelAndView mv = adapter.handle(request, response, chain.getHandler());
    assertThat((Object) mv).isNull();
}
Also used : MappedInterceptor(org.springframework.web.servlet.handler.MappedInterceptor) BeforeEach(org.junit.jupiter.api.BeforeEach) LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) RequestParam(org.springframework.web.bind.annotation.RequestParam) Validator(org.springframework.validation.Validator) ResourceUrlProvider(org.springframework.web.servlet.resource.ResourceUrlProvider) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) LocaleResolver(org.springframework.web.servlet.LocaleResolver) CacheControl(org.springframework.http.CacheControl) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) HandlerMethod(org.springframework.web.method.HandlerMethod) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) Map(java.util.Map) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) Resource(org.springframework.core.io.Resource) PathResourceResolver(org.springframework.web.servlet.resource.PathResourceResolver) ResourceTransformer(org.springframework.web.servlet.resource.ResourceTransformer) UrlBasedCorsConfigurationSource(org.springframework.web.cors.UrlBasedCorsConfigurationSource) CookieThemeResolver(org.springframework.web.servlet.theme.CookieThemeResolver) TypeMismatchException(org.springframework.beans.TypeMismatchException) StandardCharsets(java.nio.charset.StandardCharsets) NumberFormat(org.springframework.format.annotation.NumberFormat) CssLinkResourceTransformer(org.springframework.web.servlet.resource.CssLinkResourceTransformer) ResourceResolver(org.springframework.web.servlet.resource.ResourceResolver) LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) DeferredResultProcessingInterceptor(org.springframework.web.context.request.async.DeferredResultProcessingInterceptor) ScriptTemplateViewResolver(org.springframework.web.servlet.view.script.ScriptTemplateViewResolver) FlashMapManager(org.springframework.web.servlet.FlashMapManager) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) Ordered(org.springframework.core.Ordered) Errors(org.springframework.validation.Errors) ResourceUrlProviderExposingInterceptor(org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor) ThemeResolver(org.springframework.web.servlet.ThemeResolver) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) AbstractJackson2HttpMessageConverter(org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter) CookieLocaleResolver(org.springframework.web.servlet.i18n.CookieLocaleResolver) BindingResult(org.springframework.validation.BindingResult) FreeMarkerViewResolver(org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver) CachingResourceResolver(org.springframework.web.servlet.resource.CachingResourceResolver) CacheManager(org.springframework.cache.CacheManager) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) Nullable(org.springframework.lang.Nullable) ConversionService(org.springframework.core.convert.ConversionService) Validated(org.springframework.validation.annotation.Validated) ViewResolver(org.springframework.web.servlet.ViewResolver) PathMatcher(org.springframework.util.PathMatcher) ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) Target(java.lang.annotation.Target) WebJarsResourceResolver(org.springframework.web.servlet.resource.WebJarsResourceResolver) BeanNameViewResolver(org.springframework.web.servlet.view.BeanNameViewResolver) GroovyMarkupViewResolver(org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver) ThemeChangeInterceptor(org.springframework.web.servlet.theme.ThemeChangeInterceptor) FormattingConversionServiceFactoryBean(org.springframework.format.support.FormattingConversionServiceFactoryBean) UrlPathHelper(org.springframework.web.util.UrlPathHelper) MockRequestDispatcher(org.springframework.web.testfixture.servlet.MockRequestDispatcher) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) DefaultRequestToViewNameTranslator(org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator) RequestDispatcher(jakarta.servlet.RequestDispatcher) VersionResourceResolver(org.springframework.web.servlet.resource.VersionResourceResolver) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) InternalResourceView(org.springframework.web.servlet.view.InternalResourceView) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) DateTimeFormat(org.springframework.format.annotation.DateTimeFormat) SessionFlashMapManager(org.springframework.web.servlet.support.SessionFlashMapManager) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) RedirectView(org.springframework.web.servlet.view.RedirectView) Locale(java.util.Locale) BOOLEAN(org.assertj.core.api.InstanceOfAssertFactories.BOOLEAN) HandlerMappingIntrospector(org.springframework.web.servlet.handler.HandlerMappingIntrospector) EncodedResourceResolver(org.springframework.web.servlet.resource.EncodedResourceResolver) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Collection(java.util.Collection) MediaType(org.springframework.http.MediaType) WebApplicationContext(org.springframework.web.context.WebApplicationContext) ISO(org.springframework.format.annotation.DateTimeFormat.ISO) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) ModelAndView(org.springframework.web.servlet.ModelAndView) List(java.util.List) ConcurrentMapCache(org.springframework.cache.concurrent.ConcurrentMapCache) UserRoleAuthorizationInterceptor(org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) ConcurrentTaskExecutor(org.springframework.scheduling.concurrent.ConcurrentTaskExecutor) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) FreeMarkerConfigurer(org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer) CachingResourceTransformer(org.springframework.web.servlet.resource.CachingResourceTransformer) ContentVersionStrategy(org.springframework.web.servlet.resource.ContentVersionStrategy) GroovyMarkupConfigurer(org.springframework.web.servlet.view.groovy.GroovyMarkupConfigurer) MappedInterceptor(org.springframework.web.servlet.handler.MappedInterceptor) Cache(org.springframework.cache.Cache) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) NotNull(jakarta.validation.constraints.NotNull) Controller(org.springframework.stereotype.Controller) AbstractHandlerMapping(org.springframework.web.servlet.handler.AbstractHandlerMapping) Retention(java.lang.annotation.Retention) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) DefaultServletHttpRequestHandler(org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) RequestToViewNameTranslator(org.springframework.web.servlet.RequestToViewNameTranslator) Charset(java.nio.charset.Charset) CompositeUriComponentsContributor(org.springframework.web.method.support.CompositeUriComponentsContributor) ScriptTemplateConfigurer(org.springframework.web.servlet.view.script.ScriptTemplateConfigurer) SimpleControllerHandlerAdapter(org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter) HttpRequestHandlerAdapter(org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter) CallableProcessingInterceptor(org.springframework.web.context.request.async.CallableProcessingInterceptor) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) ParameterizableViewController(org.springframework.web.servlet.mvc.ParameterizableViewController) ElementType(java.lang.annotation.ElementType) TimeUnit(java.util.concurrent.TimeUnit) ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) BeanNameUrlHandlerMapping(org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) FixedVersionStrategy(org.springframework.web.servlet.resource.FixedVersionStrategy) Comparator(java.util.Comparator) Collections(java.util.Collections) RetentionPolicy(java.lang.annotation.RetentionPolicy) ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) HttpRequestHandlerAdapter(org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter) BeanNameUrlHandlerMapping(org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) ResourceUrlProvider(org.springframework.web.servlet.resource.ResourceUrlProvider) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 14 with HandlerInterceptor

use of org.springframework.web.servlet.HandlerInterceptor in project spring-framework by spring-projects.

the class WebMvcConfigurationSupportExtensionTests method handlerMappings.

@Test
public void handlerMappings() throws Exception {
    RequestMappingHandlerMapping rmHandlerMapping = this.config.requestMappingHandlerMapping(this.config.mvcContentNegotiationManager(), this.config.mvcConversionService(), this.config.mvcResourceUrlProvider());
    rmHandlerMapping.setApplicationContext(this.context);
    rmHandlerMapping.afterPropertiesSet();
    assertThat(rmHandlerMapping.getUrlPathHelper().getClass()).isEqualTo(TestPathHelper.class);
    assertThat(rmHandlerMapping.getPathMatcher().getClass()).isEqualTo(TestPathMatcher.class);
    HandlerExecutionChain chain = rmHandlerMapping.getHandler(new MockHttpServletRequest("GET", "/"));
    assertThat(chain).isNotNull();
    HandlerInterceptor[] interceptors = chain.getInterceptors();
    assertThat(interceptors).isNotNull();
    assertThat(interceptors.length).isEqualTo(4);
    assertThat(interceptors[0].getClass().getSimpleName()).isEqualTo("CorsInterceptor");
    assertThat(interceptors[1].getClass()).isEqualTo(LocaleChangeInterceptor.class);
    assertThat(interceptors[2].getClass()).isEqualTo(ConversionServiceExposingInterceptor.class);
    assertThat(interceptors[3].getClass()).isEqualTo(ResourceUrlProviderExposingInterceptor.class);
    Map<RequestMappingInfo, HandlerMethod> map = rmHandlerMapping.getHandlerMethods();
    assertThat(map.size()).isEqualTo(2);
    RequestMappingInfo info = map.entrySet().stream().filter(entry -> entry.getValue().getBeanType().equals(UserController.class)).findFirst().orElseThrow(() -> new AssertionError("UserController bean not found")).getKey();
    assertThat(info.getPatternValues()).isEqualTo(Collections.singleton("/api/user/{id}"));
    AbstractHandlerMapping handlerMapping = (AbstractHandlerMapping) this.config.viewControllerHandlerMapping(this.config.mvcConversionService(), this.config.mvcResourceUrlProvider());
    handlerMapping.setApplicationContext(this.context);
    assertThat(handlerMapping).isNotNull();
    assertThat(handlerMapping.getOrder()).isEqualTo(1);
    assertThat(handlerMapping.getUrlPathHelper().getClass()).isEqualTo(TestPathHelper.class);
    assertThat(handlerMapping.getPathMatcher().getClass()).isEqualTo(TestPathMatcher.class);
    chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/path"));
    assertThat(chain).isNotNull();
    assertThat(chain.getHandler()).isNotNull();
    chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/bad"));
    assertThat(chain).isNotNull();
    assertThat(chain.getHandler()).isNotNull();
    chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/old"));
    assertThat(chain).isNotNull();
    assertThat(chain.getHandler()).isNotNull();
    handlerMapping = (AbstractHandlerMapping) this.config.resourceHandlerMapping(this.config.mvcContentNegotiationManager(), this.config.mvcConversionService(), this.config.mvcResourceUrlProvider());
    handlerMapping.setApplicationContext(this.context);
    assertThat(handlerMapping).isNotNull();
    assertThat(handlerMapping.getOrder()).isEqualTo((Integer.MAX_VALUE - 1));
    assertThat(handlerMapping.getUrlPathHelper().getClass()).isEqualTo(TestPathHelper.class);
    assertThat(handlerMapping.getPathMatcher().getClass()).isEqualTo(TestPathMatcher.class);
    chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/resources/foo.gif"));
    assertThat(chain).isNotNull();
    assertThat(chain.getHandler()).isNotNull();
    interceptors = chain.getInterceptors();
    assertThat(interceptors.length).as(Arrays.toString(interceptors)).isEqualTo(5);
    assertThat(interceptors[0].getClass().getSimpleName()).isEqualTo("CorsInterceptor");
    // PathExposingHandlerInterceptor at interceptors[1]
    assertThat(interceptors[2].getClass()).isEqualTo(LocaleChangeInterceptor.class);
    assertThat(interceptors[3].getClass()).isEqualTo(ConversionServiceExposingInterceptor.class);
    assertThat(interceptors[4].getClass()).isEqualTo(ResourceUrlProviderExposingInterceptor.class);
    handlerMapping = (AbstractHandlerMapping) this.config.defaultServletHandlerMapping();
    handlerMapping.setApplicationContext(this.context);
    assertThat(handlerMapping).isNotNull();
    assertThat(handlerMapping.getOrder()).isEqualTo(Integer.MAX_VALUE);
    chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/anyPath"));
    assertThat(chain).isNotNull();
    assertThat(chain.getHandler()).isNotNull();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) DEFAULT_VIEW_INCLUSION(com.fasterxml.jackson.databind.MapperFeature.DEFAULT_VIEW_INCLUSION) Validator(org.springframework.validation.Validator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) APPLICATION_XML(org.springframework.http.MediaType.APPLICATION_XML) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) HandlerMethod(org.springframework.web.method.HandlerMethod) FAIL_ON_UNKNOWN_PROPERTIES(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) HandlerExceptionResolverComposite(org.springframework.web.servlet.handler.HandlerExceptionResolverComposite) BOOLEAN(org.assertj.core.api.InstanceOfAssertFactories.BOOLEAN) Map(java.util.Map) TestBean(org.springframework.beans.testfixture.beans.TestBean) AntPathMatcher(org.springframework.util.AntPathMatcher) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) FormatterRegistry(org.springframework.format.FormatterRegistry) HandlerMethodArgumentResolver(org.springframework.web.method.support.HandlerMethodArgumentResolver) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) RestController(org.springframework.web.bind.annotation.RestController) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.jupiter.api.Test) List(java.util.List) DefaultMessageCodesResolver(org.springframework.validation.DefaultMessageCodesResolver) LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) Principal(java.security.Principal) HandlerExceptionResolver(org.springframework.web.servlet.HandlerExceptionResolver) DeferredResultProcessingInterceptor(org.springframework.web.context.request.async.DeferredResultProcessingInterceptor) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ConcurrentTaskExecutor(org.springframework.scheduling.concurrent.ConcurrentTaskExecutor) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) ConversionServiceExposingInterceptor(org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor) ResponseStatusExceptionResolver(org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) HandlerMethodReturnValueHandler(org.springframework.web.method.support.HandlerMethodReturnValueHandler) Mockito.mock(org.mockito.Mockito.mock) Ordered(org.springframework.core.Ordered) SimpleMappingExceptionResolver(org.springframework.web.servlet.handler.SimpleMappingExceptionResolver) Errors(org.springframework.validation.Errors) ResourceUrlProviderExposingInterceptor(org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor) ModelAttributeMethodProcessor(org.springframework.web.method.annotation.ModelAttributeMethodProcessor) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Controller(org.springframework.stereotype.Controller) FileSystemResourceLoader(org.springframework.core.io.FileSystemResourceLoader) AbstractHandlerMapping(org.springframework.web.servlet.handler.AbstractHandlerMapping) RequestMappingInfo(org.springframework.web.servlet.mvc.method.RequestMappingInfo) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) GetMapping(org.springframework.web.bind.annotation.GetMapping) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) Nullable(org.springframework.lang.Nullable) MessageCodesResolver(org.springframework.validation.MessageCodesResolver) APPLICATION_JSON(org.springframework.http.MediaType.APPLICATION_JSON) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) CallableProcessingInterceptor(org.springframework.web.context.request.async.CallableProcessingInterceptor) ViewResolver(org.springframework.web.servlet.ViewResolver) ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) CorsConfiguration(org.springframework.web.cors.CorsConfiguration) ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) HttpStatus(org.springframework.http.HttpStatus) View(org.springframework.web.servlet.View) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) UrlPathHelper(org.springframework.web.util.UrlPathHelper) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) HandlerTypePredicate(org.springframework.web.method.HandlerTypePredicate) Collections(java.util.Collections) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) RequestMappingInfo(org.springframework.web.servlet.mvc.method.RequestMappingInfo) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) HandlerMethod(org.springframework.web.method.HandlerMethod) AbstractHandlerMapping(org.springframework.web.servlet.handler.AbstractHandlerMapping) Test(org.junit.jupiter.api.Test)

Example 15 with HandlerInterceptor

use of org.springframework.web.servlet.HandlerInterceptor in project spring-framework by spring-projects.

the class HandlerMappingTests method orderedInterceptors.

@PathPatternsParameterizedTest
void orderedInterceptors(TestHandlerMapping mapping, Function<String, MockHttpServletRequest> requestFactory) throws Exception {
    MappedInterceptor i1 = new MappedInterceptor(new String[] { "/**" }, mock(HandlerInterceptor.class));
    HandlerInterceptor i2 = mock(HandlerInterceptor.class);
    MappedInterceptor i3 = new MappedInterceptor(new String[] { "/**" }, mock(HandlerInterceptor.class));
    HandlerInterceptor i4 = mock(HandlerInterceptor.class);
    mapping.setInterceptors(i1, i2, i3, i4);
    mapping.setApplicationContext(new StaticWebApplicationContext());
    HandlerExecutionChain chain = mapping.getHandler(requestFactory.apply("/"));
    assertThat(chain).isNotNull();
    assertThat(chain.getInterceptorList()).contains(i1.getInterceptor(), i2, i3.getInterceptor(), i4);
}
Also used : HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext)

Aggregations

HandlerInterceptor (org.springframework.web.servlet.HandlerInterceptor)30 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)14 Test (org.junit.jupiter.api.Test)10 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)8 Test (org.junit.Test)6 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)6 CorsConfiguration (org.springframework.web.cors.CorsConfiguration)5 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)4 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)3 ArrayList (java.util.ArrayList)3 Nullable (org.springframework.lang.Nullable)3 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)3 MappedInterceptor (org.springframework.web.servlet.handler.MappedInterceptor)3 RequestMappingHandlerAdapter (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter)3 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2