Class UrlBasedCorsConfigurationSource
- All Implemented Interfaces:
CorsConfigurationSource
CorsConfigurationSource
that uses URL path patterns to select the
CorsConfiguration
for a request.
Pattern matching can be done with a PathMatcher
or with pre-parsed
PathPattern
s. The syntax is largely the same with the latter being more
tailored for web usage and more efficient. The choice depends on the presence of a
resolved
String lookupPath or a
parsed
RequestPath
with a fallback on PathMatcher
but the fallback can be disabled.
For more details, please see setAllowInitLookupPath(boolean)
.
- Since:
- 4.2
- Author:
- Sebastien Deleuze, Rossen Stoyanchev
- See Also:
-
Constructor Summary
ConstructorDescriptionDefault constructor withPathPatternParser.defaultInstance
.Constructor with aPathPatternParser
to parse patterns with. -
Method Summary
Modifier and TypeMethodDescriptiongetCorsConfiguration
(HttpServletRequest request) Return aCorsConfiguration
based on the incoming request.Return all configured CORS mappings.void
registerCorsConfiguration
(String pattern, CorsConfiguration config) Variant ofsetCorsConfigurations(Map)
to register one mapping at a time.void
setAllowInitLookupPath
(boolean allowInitLookupPath) When enabled, if there is neither aesolved
String lookupPath nor aparsed
RequestPath
then use theconfigured
UrlPathHelper
to resolve a String lookupPath.void
setAlwaysUseFullPath
(boolean alwaysUseFullPath) Deprecated.void
setCorsConfigurations
(Map<String, CorsConfiguration> corsConfigurations) Set the CORS configuration mappings.void
Deprecated.as of 5.3 in favor ofUrlPathHelper.PATH_ATTRIBUTE
.void
setPathMatcher
(PathMatcher pathMatcher) Configure aPathMatcher
to use for pattern matching.void
setRemoveSemicolonContent
(boolean removeSemicolonContent) Deprecated.as of 5.3 in favor of usingsetUrlPathHelper(UrlPathHelper)
, if at all.void
setUrlDecode
(boolean urlDecode) Deprecated.as of 5.3 in favor of usingsetUrlPathHelper(UrlPathHelper)
, if at all.void
setUrlPathHelper
(UrlPathHelper urlPathHelper) Configure theUrlPathHelper
to resolve the lookupPath.
-
Constructor Details
-
UrlBasedCorsConfigurationSource
public UrlBasedCorsConfigurationSource()Default constructor withPathPatternParser.defaultInstance
. -
UrlBasedCorsConfigurationSource
Constructor with aPathPatternParser
to parse patterns with.- Parameters:
parser
- the parser to use- Since:
- 5.3
-
-
Method Details
-
setAlwaysUseFullPath
Deprecated.as of 5.3 in favor of usingsetUrlPathHelper(UrlPathHelper)
, if at all. For further details, please seesetAllowInitLookupPath(boolean)
.Shortcut to thesame property
on the configuredUrlPathHelper
. -
setUrlDecode
Deprecated.as of 5.3 in favor of usingsetUrlPathHelper(UrlPathHelper)
, if at all. For further details, please seesetAllowInitLookupPath(boolean)
.Shortcut to thesame property
on the configuredUrlPathHelper
. -
setRemoveSemicolonContent
Deprecated.as of 5.3 in favor of usingsetUrlPathHelper(UrlPathHelper)
, if at all. For further details, please seesetAllowInitLookupPath(boolean)
.Shortcut to thesame property
on the configuredUrlPathHelper
. -
setUrlPathHelper
Configure theUrlPathHelper
to resolve the lookupPath. This may not be necessary if the lookupPath is expected to be pre-resolved or if parsedPathPatterns
are used instead. For further details on that, seesetAllowInitLookupPath(boolean)
.By default this is
UrlPathHelper.defaultInstance
. -
setAllowInitLookupPath
public void setAllowInitLookupPath(boolean allowInitLookupPath) When enabled, if there is neither aesolved
String lookupPath nor aparsed
RequestPath
then use theconfigured
UrlPathHelper
to resolve a String lookupPath. This in turn determines use of URL pattern matching withPathMatcher
or with parsedPathPattern
s.In Spring MVC, either a resolved String lookupPath or a parsed
RequestPath
is always available withinDispatcherServlet
processing. However, in a ServletFilter
such asCorsFilter
that may or may not be the case.By default this is set to
true
in which case lazy lookupPath initialization is allowed. Set this tofalse
when an application is using parsedPathPatterns
in which case theRequestPath
can be parsed earlier viaServletRequestPathFilter
.- Parameters:
allowInitLookupPath
- whether to disable lazy initialization and fail if not already resolved- Since:
- 5.3
-
setLookupPathAttributeName
Deprecated.as of 5.3 in favor ofUrlPathHelper.PATH_ATTRIBUTE
.Configure the name of the attribute that holds the lookupPath extracted viaUrlPathHelper.getLookupPathForRequest(HttpServletRequest)
.By default this is
UrlPathHelper.PATH_ATTRIBUTE
.- Parameters:
name
- the request attribute to check- Since:
- 5.2
-
setPathMatcher
Configure aPathMatcher
to use for pattern matching.This is an advanced property that should be used only when a customized
AntPathMatcher
or a custom PathMatcher is required.By default this is
AntPathMatcher
.Note: Setting
PathMatcher
enforces use of String pattern matching even when aparsed
RequestPath
is available. -
setCorsConfigurations
Set the CORS configuration mappings.For pattern syntax see
AntPathMatcher
andPathPattern
as well as class-level Javadoc for details on which one may in use. Generally the syntax is largely the same withPathPattern
more tailored for web usage.- Parameters:
corsConfigurations
- the mappings to use- See Also:
-
registerCorsConfiguration
Variant ofsetCorsConfigurations(Map)
to register one mapping at a time.- Parameters:
pattern
- the mapping patternconfig
- the CORS configuration to use for the pattern- See Also:
-
getCorsConfigurations
Return all configured CORS mappings. -
getCorsConfiguration
Description copied from interface:CorsConfigurationSource
Return aCorsConfiguration
based on the incoming request.- Specified by:
getCorsConfiguration
in interfaceCorsConfigurationSource
- Returns:
- the associated
CorsConfiguration
, ornull
if none
-
setUrlPathHelper(UrlPathHelper)
, if at all.