Class ServletFilterRegistration

java.lang.Object
org.apache.juneau.microservice.ServletFilterRegistration

public final class ServletFilterRegistration extends Object
A servlet filter, with the URL patterns, dispatcher types and order it is mounted at, that the server components (JettyServerComponent, TomcatServerComponent) mount before the server starts.

Contribute one as a @Bean from a @Configuration class. It is the container-neutral counterpart of Spring Boot's FilterRegistrationBean, so the same registration works on Jetty and Tomcat.

Example:

@Configuration public class MyAppConfig { @Bean(name="boundary") public ServletFilterRegistration boundary() { return ServletFilterRegistration.create(new MyBoundaryFilter()) .urlPatterns("/console/*") .dispatcherTypes(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.ASYNC, DispatcherType.ERROR) .order(ServletFilterRegistration.HIGHEST_PRECEDENCE) .build(); } }

Order:

Filters run outermost first, by ascending order. A bean-supplied AuthFilterChain is mounted automatically at AUTH_ORDER, so a registration at the default DEFAULT_ORDER runs after authentication. Two registrations at the same order run in ascending bean-name order, and the auto-mounted authentication filter runs ahead of a registration at its own order; give registrations that must keep a relative order distinct values rather than relying on names.

The order is only ever ServletFilterRegistration.Builder.order(int). @Order and @Bean(priority) on the factory method do not affect it.

Running a filter before authentication is a property of a narrow URL pattern, not of the order alone. A boundary on "/console/*" rejects a bad request before authentication ever sees it; the same filter on "/*" at HIGHEST_PRECEDENCE would also intercept authentication redirects and challenges.

Names:

More than one registration in an application needs a distinct bean name, because a @Bean of one type with the same (default empty) name is a duplicate. Distinct filters must be distinct instances: wrapping one filter instance in two registrations fails at startup, because the container would otherwise initialize and destroy it twice.

Replacing the authentication mount:

An application that needs the AuthFilterChain on other dispatcher types, or at another position, registers it itself: wrap the injected AuthFilterChain bean instance in a registration. That registration then replaces the automatic mount. It must be the injected instance; a new instance is a second, separate filter.

See Also:
Since:
10.0.0
  • Field Details

  • Method Details

    • create

      public static ServletFilterRegistration.Builder create(jakarta.servlet.Filter filter)
      Creates a builder for a registration of a filter.
      Parameters:
      filter - The filter instance. Cannot be null.
      Returns:
      A new builder.
      Throws:
      IllegalArgumentException - If the filter is null.
    • collect

      Collects the registrations a server component mounts, outermost first.

      Takes every ServletFilterRegistration bean, plus an internal registration for the AuthFilterChain bean when there is one and no registration wraps that same instance (see the class description). Sorts by order, then the automatic authentication mount first, then bean name.

      Intended for the server components; applications contribute registrations as beans.

      Parameters:
      store - The microservice bean store. Cannot be null.
      Returns:
      The registrations in mount order. Never null.
      Throws:
      IllegalStateException - If one filter instance is registered more than once, or two registrations have the same explicit name.
    • getFilter

      public jakarta.servlet.Filter getFilter()
      Returns the filter.
      Returns:
      The filter instance. Never null.
    • getUrlPatterns

      Returns the URL patterns the filter is mapped to.
      Returns:
      An unmodifiable list, in declaration order and without duplicates.
    • getDispatcherTypes

      public Set<jakarta.servlet.DispatcherType> getDispatcherTypes()
      Returns the dispatcher types the filter is mapped to.
      Returns:
      An unmodifiable set. Never empty.
    • getOrder

      public int getOrder()
      Returns the order: lower values run first.
      Returns:
      The order.
    • isAsyncSupported

      public boolean isAsyncSupported()
      Returns whether the filter supports asynchronous request processing.
      Returns:
      true if it does.
    • getName

      public Optional<String> getName()
      Returns the explicit container filter name.
      Returns:
      The name, or empty when the component generates one.