public class CorsFilter extends OncePerRequestFilter
javax.servlet.Filter
that handles CORS preflight requests and intercepts CORS
simple and actual requests thanks to a CorsProcessor
implementation
(DefaultCorsProcessor
by default) in order to add the relevant CORS response
headers (like Access-Control-Allow-Origin
) using the provided
CorsConfigurationSource
(for example a CorsConfigurationMapping
instance.
This filter could be used in conjunction with DelegatingFilterProxy
in order
to help with its initialization.
Modifier and Type | Field and Description |
---|---|
private CorsProcessor |
processor |
private CorsConfigurationSource |
source |
ALREADY_FILTERED_SUFFIX
logger
Constructor and Description |
---|
CorsFilter(CorsConfigurationSource source)
Constructor accepting a
CorsConfigurationSource , this source will be used
by the filter to find the CorsConfiguration to use for each incoming request. |
Modifier and Type | Method and Description |
---|---|
protected void |
doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain)
Same contract as for
doFilter , but guaranteed to be
just invoked once per request within a single request thread. |
void |
setCorsProcessor(CorsProcessor processor)
Configure a custom
CorsProcessor to use to apply the matched
CorsConfiguration for a request. |
doFilter, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter, shouldNotFilterAsyncDispatch, shouldNotFilterErrorDispatch
addRequiredProperty, afterPropertiesSet, destroy, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext
private CorsProcessor processor
private final CorsConfigurationSource source
public CorsFilter(CorsConfigurationSource source)
CorsConfigurationSource
, this source will be used
by the filter to find the CorsConfiguration
to use for each incoming request.CorsConfigurationMapping
public void setCorsProcessor(CorsProcessor processor)
CorsProcessor
to use to apply the matched
CorsConfiguration
for a request.
By default DefaultCorsProcessor
is used.
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, java.io.IOException
OncePerRequestFilter
doFilter
, but guaranteed to be
just invoked once per request within a single request thread.
See OncePerRequestFilter.shouldNotFilterAsyncDispatch()
for details.
Provides HttpServletRequest and HttpServletResponse arguments instead of the default ServletRequest and ServletResponse ones.
doFilterInternal
in class OncePerRequestFilter
ServletException
java.io.IOException