Package org.springframework.web.filter
Class CorsFilter
- java.lang.Object
- org.springframework.web.filter.GenericFilterBean
- org.springframework.web.filter.OncePerRequestFilter
- org.springframework.web.filter.CorsFilter
- All Implemented Interfaces:
Filter
,Aware
,BeanNameAware
,DisposableBean
,InitializingBean
,EnvironmentAware
,EnvironmentCapable
,ServletContextAware
public class CorsFilter extends OncePerRequestFilter
Filter
that handles CORS preflight requests and intercepts CORS simple and actual requests thanks to aCorsProcessor
implementation (DefaultCorsProcessor
by default) in order to add the relevant CORS response headers (likeAccess-Control-Allow-Origin
) using the providedCorsConfigurationSource
(for example anUrlBasedCorsConfigurationSource
instance.This is an alternative to Spring MVC Java config and XML namespace CORS configuration, useful for applications depending only on spring-web (not on spring-webmvc) or for security constraints requiring CORS checks to be performed at
Filter
level.This filter could be used in conjunction with
DelegatingFilterProxy
in order to help with its initialization.- Since:
- 4.2
- Author:
- Sebastien Deleuze
- See Also:
- CORS W3C recommendation
Field Summary
Fields inherited from class org.springframework.web.filter.OncePerRequestFilter
ALREADY_FILTERED_SUFFIX
Fields inherited from class org.springframework.web.filter.GenericFilterBean
logger
Constructor Summary
Constructors Constructor Description CorsFilter(CorsConfigurationSource configSource)
Constructor accepting aCorsConfigurationSource
used by the filter to find theCorsConfiguration
to use for each incoming request.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
Same contract as fordoFilter
, but guaranteed to be just invoked once per request within a single request thread.void
setCorsProcessor(CorsProcessor processor)
Configure a customCorsProcessor
to use to apply the matchedCorsConfiguration
for a request.Methods inherited from class org.springframework.web.filter.OncePerRequestFilter
doFilter, doFilterNestedErrorDispatch, getAlreadyFilteredAttributeName, isAsyncDispatch, isAsyncStarted, shouldNotFilter, shouldNotFilterAsyncDispatch, shouldNotFilterErrorDispatch
Methods inherited from class org.springframework.web.filter.GenericFilterBean
addRequiredProperty, afterPropertiesSet, createEnvironment, destroy, getEnvironment, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setEnvironment, setServletContext
Constructor Detail
CorsFilter
public CorsFilter(CorsConfigurationSource configSource)
Constructor accepting aCorsConfigurationSource
used by the filter to find theCorsConfiguration
to use for each incoming request.- See Also:
UrlBasedCorsConfigurationSource
Method Detail
setCorsProcessor
public void setCorsProcessor(CorsProcessor processor)
Configure a customCorsProcessor
to use to apply the matchedCorsConfiguration
for a request.By default
DefaultCorsProcessor
is used.
doFilterInternal
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException
Description copied from class:OncePerRequestFilter
Same contract as fordoFilter
, but guaranteed to be just invoked once per request within a single request thread. SeeOncePerRequestFilter.shouldNotFilterAsyncDispatch()
for details.Provides HttpServletRequest and HttpServletResponse arguments instead of the default ServletRequest and ServletResponse ones.
- Specified by:
doFilterInternal
in classOncePerRequestFilter
- Throws:
ServletException
IOException