Search in sources :

Example 21 with HandlerInterceptor

use of org.springframework.web.servlet.HandlerInterceptor in project grails-core by grails.

the class WebUtils method lookupHandlerInterceptors.

/**
 * Looks up all of the HandlerInterceptor instances registered for the application
 *
 * @param servletContext The ServletContext instance
 * @return An array of HandlerInterceptor instances
 */
public static HandlerInterceptor[] lookupHandlerInterceptors(ServletContext servletContext) {
    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    final Collection<HandlerInterceptor> allHandlerInterceptors = new ArrayList<HandlerInterceptor>();
    WebRequestInterceptor[] webRequestInterceptors = lookupWebRequestInterceptors(servletContext);
    for (WebRequestInterceptor webRequestInterceptor : webRequestInterceptors) {
        allHandlerInterceptors.add(new WebRequestHandlerInterceptorAdapter(webRequestInterceptor));
    }
    final Collection<HandlerInterceptor> handlerInterceptors = wac.getBeansOfType(HandlerInterceptor.class).values();
    allHandlerInterceptors.addAll(handlerInterceptors);
    return allHandlerInterceptors.toArray(new HandlerInterceptor[allHandlerInterceptors.size()]);
}
Also used : HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) WebRequestInterceptor(org.springframework.web.context.request.WebRequestInterceptor) ArrayList(java.util.ArrayList) WebRequestHandlerInterceptorAdapter(org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 22 with HandlerInterceptor

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

the class AbstractHandlerMapping method getCorsHandlerExecutionChain.

/**
	 * Update the HandlerExecutionChain for CORS-related handling.
	 * <p>For pre-flight requests, the default implementation replaces the selected
	 * handler with a simple HttpRequestHandler that invokes the configured
	 * {@link #setCorsProcessor}.
	 * <p>For actual requests, the default implementation inserts a
	 * HandlerInterceptor that makes CORS-related checks and adds CORS headers.
	 * @param request the current request
	 * @param chain the handler chain
	 * @param config the applicable CORS configuration, possibly {@code null}
	 * @since 4.2
	 */
protected HandlerExecutionChain getCorsHandlerExecutionChain(HttpServletRequest request, HandlerExecutionChain chain, CorsConfiguration config) {
    if (CorsUtils.isPreFlightRequest(request)) {
        HandlerInterceptor[] interceptors = chain.getInterceptors();
        chain = new HandlerExecutionChain(new PreFlightHandler(config), interceptors);
    } else {
        chain.addInterceptor(new CorsInterceptor(config));
    }
    return chain;
}
Also used : HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain)

Example 23 with HandlerInterceptor

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

the class AbstractHandlerMapping method getMappedInterceptors.

/**
	 * Return all configured {@link MappedInterceptor}s as an array.
	 * @return the array of {@link MappedInterceptor}s, or {@code null} if none
	 */
protected final MappedInterceptor[] getMappedInterceptors() {
    List<MappedInterceptor> mappedInterceptors = new ArrayList<>();
    for (HandlerInterceptor interceptor : this.adaptedInterceptors) {
        if (interceptor instanceof MappedInterceptor) {
            mappedInterceptors.add((MappedInterceptor) interceptor);
        }
    }
    int count = mappedInterceptors.size();
    return (count > 0 ? mappedInterceptors.toArray(new MappedInterceptor[count]) : null);
}
Also used : HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) ArrayList(java.util.ArrayList)

Example 24 with HandlerInterceptor

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

the class AbstractHandlerMapping method getHandlerExecutionChain.

/**
	 * Build a {@link HandlerExecutionChain} for the given handler, including
	 * applicable interceptors.
	 * <p>The default implementation builds a standard {@link HandlerExecutionChain}
	 * with the given handler, the handler mapping's common interceptors, and any
	 * {@link MappedInterceptor}s matching to the current request URL. Interceptors
	 * are added in the order they were registered. Subclasses may override this
	 * in order to extend/rearrange the list of interceptors.
	 * <p><b>NOTE:</b> The passed-in handler object may be a raw handler or a
	 * pre-built {@link HandlerExecutionChain}. This method should handle those
	 * two cases explicitly, either building a new {@link HandlerExecutionChain}
	 * or extending the existing chain.
	 * <p>For simply adding an interceptor in a custom subclass, consider calling
	 * {@code super.getHandlerExecutionChain(handler, request)} and invoking
	 * {@link HandlerExecutionChain#addInterceptor} on the returned chain object.
	 * @param handler the resolved handler instance (never {@code null})
	 * @param request current HTTP request
	 * @return the HandlerExecutionChain (never {@code null})
	 * @see #getAdaptedInterceptors()
	 */
protected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request) {
    HandlerExecutionChain chain = (handler instanceof HandlerExecutionChain ? (HandlerExecutionChain) handler : new HandlerExecutionChain(handler));
    String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
    for (HandlerInterceptor interceptor : this.adaptedInterceptors) {
        if (interceptor instanceof MappedInterceptor) {
            MappedInterceptor mappedInterceptor = (MappedInterceptor) interceptor;
            if (mappedInterceptor.matches(lookupPath, this.pathMatcher)) {
                chain.addInterceptor(mappedInterceptor.getInterceptor());
            }
        } else {
            chain.addInterceptor(interceptor);
        }
    }
    return chain;
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor)

Example 25 with HandlerInterceptor

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

the class CorsAbstractHandlerMappingTests method getCorsConfiguration.

private CorsConfiguration getCorsConfiguration(HandlerExecutionChain chain, boolean isPreFlightRequest) {
    if (isPreFlightRequest) {
        Object handler = chain.getHandler();
        assertTrue(handler.getClass().getSimpleName().equals("PreFlightHandler"));
        DirectFieldAccessor accessor = new DirectFieldAccessor(handler);
        return (CorsConfiguration) accessor.getPropertyValue("config");
    } else {
        HandlerInterceptor[] interceptors = chain.getInterceptors();
        if (interceptors != null) {
            for (HandlerInterceptor interceptor : interceptors) {
                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)

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