001/* 002 * Copyright 2012-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.web.servlet; 018 019import javax.servlet.Filter; 020import javax.servlet.ServletContext; 021 022import org.springframework.util.Assert; 023 024/** 025 * A {@link ServletContextInitializer} to register {@link Filter}s in a Servlet 3.0+ 026 * container. Similar to the {@link ServletContext#addFilter(String, Filter) registration} 027 * features provided by {@link ServletContext} but with a Spring Bean friendly design. 028 * <p> 029 * The {@link #setFilter(Filter) Filter} must be specified before calling 030 * {@link #onStartup(ServletContext)}. Registrations can be associated with 031 * {@link #setUrlPatterns URL patterns} and/or servlets (either by {@link #setServletNames 032 * name} or via a {@link #setServletRegistrationBeans ServletRegistrationBean}s. When no 033 * URL pattern or servlets are specified the filter will be associated to '/*'. The filter 034 * name will be deduced if not specified. 035 * 036 * @param <T> the type of {@link Filter} to register 037 * @author Phillip Webb 038 * @since 1.4.0 039 * @see ServletContextInitializer 040 * @see ServletContext#addFilter(String, Filter) 041 * @see DelegatingFilterProxyRegistrationBean 042 */ 043public class FilterRegistrationBean<T extends Filter> 044 extends AbstractFilterRegistrationBean<T> { 045 046 /** 047 * Filters that wrap the servlet request should be ordered less than or equal to this. 048 * @deprecated since 2.1.0 in favor of 049 * {@code OrderedFilter.REQUEST_WRAPPER_FILTER_MAX_ORDER} 050 */ 051 @Deprecated 052 public static final int REQUEST_WRAPPER_FILTER_MAX_ORDER = AbstractFilterRegistrationBean.REQUEST_WRAPPER_FILTER_MAX_ORDER; 053 054 private T filter; 055 056 /** 057 * Create a new {@link FilterRegistrationBean} instance. 058 */ 059 public FilterRegistrationBean() { 060 } 061 062 /** 063 * Create a new {@link FilterRegistrationBean} instance to be registered with the 064 * specified {@link ServletRegistrationBean}s. 065 * @param filter the filter to register 066 * @param servletRegistrationBeans associate {@link ServletRegistrationBean}s 067 */ 068 public FilterRegistrationBean(T filter, 069 ServletRegistrationBean<?>... servletRegistrationBeans) { 070 super(servletRegistrationBeans); 071 Assert.notNull(filter, "Filter must not be null"); 072 this.filter = filter; 073 } 074 075 @Override 076 public T getFilter() { 077 return this.filter; 078 } 079 080 /** 081 * Set the filter to be registered. 082 * @param filter the filter 083 */ 084 public void setFilter(T filter) { 085 Assert.notNull(filter, "Filter must not be null"); 086 this.filter = filter; 087 } 088 089}