Search in sources :

Example 1 with Dynamic

use of jakarta.servlet.FilterRegistration.Dynamic in project spring-security by spring-projects.

the class AbstractSecurityWebApplicationInitializer method registerFilter.

/**
 * Registers the provided filter using the {@link #isAsyncSecuritySupported()} and
 * {@link #getSecurityDispatcherTypes()}.
 * @param servletContext
 * @param insertBeforeOtherFilters should this Filter be inserted before or after
 * other {@link Filter}
 * @param filterName
 * @param filter
 */
private void registerFilter(ServletContext servletContext, boolean insertBeforeOtherFilters, String filterName, Filter filter) {
    Dynamic registration = servletContext.addFilter(filterName, filter);
    Assert.state(registration != null, () -> "Duplicate Filter registration for '" + filterName + "'. Check to ensure the Filter is only configured once.");
    registration.setAsyncSupported(isAsyncSecuritySupported());
    EnumSet<DispatcherType> dispatcherTypes = getSecurityDispatcherTypes();
    registration.addMappingForUrlPatterns(dispatcherTypes, !insertBeforeOtherFilters, "/*");
}
Also used : Dynamic(jakarta.servlet.FilterRegistration.Dynamic) DispatcherType(jakarta.servlet.DispatcherType)

Example 2 with Dynamic

use of jakarta.servlet.FilterRegistration.Dynamic in project spring-framework by spring-projects.

the class AbstractDispatcherServletInitializer method registerServletFilter.

/**
 * Add the given filter to the ServletContext and map it to the
 * {@code DispatcherServlet} as follows:
 * <ul>
 * <li>a default filter name is chosen based on its concrete type
 * <li>the {@code asyncSupported} flag is set depending on the
 * return value of {@link #isAsyncSupported() asyncSupported}
 * <li>a filter mapping is created with dispatcher types {@code REQUEST},
 * {@code FORWARD}, {@code INCLUDE}, and conditionally {@code ASYNC} depending
 * on the return value of {@link #isAsyncSupported() asyncSupported}
 * </ul>
 * <p>If the above defaults are not suitable or insufficient, override this
 * method and register filters directly with the {@code ServletContext}.
 * @param servletContext the servlet context to register filters with
 * @param filter the filter to be registered
 * @return the filter registration
 */
protected FilterRegistration.Dynamic registerServletFilter(ServletContext servletContext, Filter filter) {
    String filterName = Conventions.getVariableName(filter);
    Dynamic registration = servletContext.addFilter(filterName, filter);
    if (registration == null) {
        int counter = 0;
        while (registration == null) {
            if (counter == 100) {
                throw new IllegalStateException("Failed to register filter with name '" + filterName + "'. " + "Check if there is another filter registered under the same name.");
            }
            registration = servletContext.addFilter(filterName + "#" + counter, filter);
            counter++;
        }
    }
    registration.setAsyncSupported(isAsyncSupported());
    registration.addMappingForServletNames(getDispatcherTypes(), false, getServletName());
    return registration;
}
Also used : Dynamic(jakarta.servlet.FilterRegistration.Dynamic)

Aggregations

Dynamic (jakarta.servlet.FilterRegistration.Dynamic)2 DispatcherType (jakarta.servlet.DispatcherType)1