Search in sources :

Example 1 with HandlerInterceptor

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

the class CloudFoundryActuatorAutoConfiguration method getSecurityInterceptor.

private HandlerInterceptor getSecurityInterceptor(RestTemplateBuilder restTemplateBuilder, Environment environment) {
    CloudFoundrySecurityService cloudfoundrySecurityService = getCloudFoundrySecurityService(restTemplateBuilder, environment);
    TokenValidator tokenValidator = new TokenValidator(cloudfoundrySecurityService);
    HandlerInterceptor securityInterceptor = new CloudFoundrySecurityInterceptor(tokenValidator, cloudfoundrySecurityService, environment.getProperty("vcap.application.application_id"));
    return securityInterceptor;
}
Also used : HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor)

Example 2 with HandlerInterceptor

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

the class AbstractEndpointHandlerMappingTests method securityInterceptorShouldBePresentForNonCorsRequest.

@Test
public void securityInterceptorShouldBePresentForNonCorsRequest() throws Exception {
    HandlerInterceptor securityInterceptor = mock(HandlerInterceptor.class);
    TestActionEndpoint endpoint = new TestActionEndpoint(new TestEndpoint("a"));
    AbstractEndpointHandlerMapping<?> mapping = new TestEndpointHandlerMapping<>(Collections.singletonList(endpoint));
    mapping.setApplicationContext(this.context);
    mapping.setSecurityInterceptor(securityInterceptor);
    mapping.afterPropertiesSet();
    assertThat(mapping.getHandler(request("POST", "/a")).getInterceptors()).contains(securityInterceptor);
}
Also used : HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) Test(org.junit.Test)

Example 3 with HandlerInterceptor

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

the class SitePreferenceAutoConfigurationTests method sitePreferenceHandlerInterceptorRegistered.

@Test
public void sitePreferenceHandlerInterceptorRegistered() throws Exception {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(Config.class, WebMvcAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, SitePreferenceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    RequestMappingHandlerMapping mapping = this.context.getBean(RequestMappingHandlerMapping.class);
    HandlerInterceptor[] interceptors = mapping.getHandler(new MockHttpServletRequest()).getInterceptors();
    assertThat(interceptors).hasAtLeastOneElementOfType(SitePreferenceHandlerInterceptor.class);
}
Also used : SitePreferenceHandlerInterceptor(org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 4 with HandlerInterceptor

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

the class DeviceResolverAutoConfigurationTests method deviceResolverHandlerInterceptorRegistered.

@Test
public void deviceResolverHandlerInterceptorRegistered() throws Exception {
    this.context = new AnnotationConfigWebApplicationContext();
    this.context.setServletContext(new MockServletContext());
    this.context.register(Config.class);
    this.context.refresh();
    RequestMappingHandlerMapping mapping = this.context.getBean(RequestMappingHandlerMapping.class);
    HandlerInterceptor[] interceptors = mapping.getHandler(new MockHttpServletRequest()).getInterceptors();
    assertThat(interceptors).hasAtLeastOneElementOfType(DeviceResolverHandlerInterceptor.class);
}
Also used : DeviceResolverHandlerInterceptor(org.springframework.mobile.device.DeviceResolverHandlerInterceptor) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 5 with HandlerInterceptor

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

the class InterceptorRegistryTests method getInterceptorsForPath.

private List<HandlerInterceptor> getInterceptorsForPath(String lookupPath) {
    PathMatcher pathMatcher = new AntPathMatcher();
    List<HandlerInterceptor> result = new ArrayList<>();
    for (Object interceptor : this.registry.getInterceptors()) {
        if (interceptor instanceof MappedInterceptor) {
            MappedInterceptor mappedInterceptor = (MappedInterceptor) interceptor;
            if (mappedInterceptor.matches(lookupPath, pathMatcher)) {
                result.add(mappedInterceptor.getInterceptor());
            }
        } else if (interceptor instanceof HandlerInterceptor) {
            result.add((HandlerInterceptor) interceptor);
        } else {
            fail("Unexpected interceptor type: " + interceptor.getClass().getName());
        }
    }
    return result;
}
Also used : MappedInterceptor(org.springframework.web.servlet.handler.MappedInterceptor) PathMatcher(org.springframework.util.PathMatcher) AntPathMatcher(org.springframework.util.AntPathMatcher) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) ArrayList(java.util.ArrayList) AntPathMatcher(org.springframework.util.AntPathMatcher)

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