public class UrlBasedCorsConfigurationSource extends Object implements 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)
.
PathPattern
,
AntPathMatcher
Constructor and Description |
---|
UrlBasedCorsConfigurationSource()
Default constructor with
PathPatternParser.defaultInstance . |
UrlBasedCorsConfigurationSource(PathPatternParser parser)
Constructor with a
PathPatternParser to parse patterns with. |
Modifier and Type | Method and Description |
---|---|
CorsConfiguration |
getCorsConfiguration(HttpServletRequest request)
Return a
CorsConfiguration based on the incoming request. |
Map<String,CorsConfiguration> |
getCorsConfigurations()
Return all configured CORS mappings.
|
void |
registerCorsConfiguration(String pattern,
CorsConfiguration config)
Variant of
setCorsConfigurations(Map) to register one mapping at a time. |
void |
setAllowInitLookupPath(boolean allowInitLookupPath)
When enabled, if there is neither a
esolved String lookupPath nor a
parsed RequestPath
then use the configured UrlPathHelper
to resolve a String lookupPath. |
void |
setAlwaysUseFullPath(boolean alwaysUseFullPath)
Deprecated.
as of 5.3 in favor of using
setUrlPathHelper(UrlPathHelper) , if at all. For further details,
please see setAllowInitLookupPath(boolean) . |
void |
setCorsConfigurations(Map<String,CorsConfiguration> corsConfigurations)
Set the CORS configuration mappings.
|
void |
setLookupPathAttributeName(String name)
Deprecated.
as of 5.3 in favor of
UrlPathHelper.PATH_ATTRIBUTE . |
void |
setPathMatcher(PathMatcher pathMatcher)
Configure a
PathMatcher to use for pattern matching. |
void |
setRemoveSemicolonContent(boolean removeSemicolonContent)
Deprecated.
as of 5.3 in favor of using
setUrlPathHelper(UrlPathHelper) , if at all. For further details,
please see setAllowInitLookupPath(boolean) . |
void |
setUrlDecode(boolean urlDecode)
Deprecated.
as of 5.3 in favor of using
setUrlPathHelper(UrlPathHelper) , if at all. For further details,
please see setAllowInitLookupPath(boolean) . |
void |
setUrlPathHelper(UrlPathHelper urlPathHelper)
Configure the
UrlPathHelper to resolve the lookupPath. |
public UrlBasedCorsConfigurationSource()
PathPatternParser.defaultInstance
.public UrlBasedCorsConfigurationSource(PathPatternParser parser)
PathPatternParser
to parse patterns with.parser
- the parser to use@Deprecated public void setAlwaysUseFullPath(boolean alwaysUseFullPath)
setUrlPathHelper(UrlPathHelper)
, if at all. For further details,
please see setAllowInitLookupPath(boolean)
.same property
on the configured UrlPathHelper
.@Deprecated public void setUrlDecode(boolean urlDecode)
setUrlPathHelper(UrlPathHelper)
, if at all. For further details,
please see setAllowInitLookupPath(boolean)
.same property
on the configured UrlPathHelper
.@Deprecated public void setRemoveSemicolonContent(boolean removeSemicolonContent)
setUrlPathHelper(UrlPathHelper)
, if at all. For further details,
please see setAllowInitLookupPath(boolean)
.same property
on the configured UrlPathHelper
.public void setUrlPathHelper(UrlPathHelper urlPathHelper)
UrlPathHelper
to resolve the lookupPath. This may
not be necessary if the lookupPath is expected to be pre-resolved or if
parsed PathPatterns
are used instead.
For further details on that, see setAllowInitLookupPath(boolean)
.
By default this is UrlPathHelper.defaultInstance
.
public void setAllowInitLookupPath(boolean allowInitLookupPath)
esolved
String lookupPath nor a
parsed
RequestPath
then use the configured
UrlPathHelper
to resolve a String lookupPath. This in turn determines use of URL
pattern matching with PathMatcher
or with parsed PathPattern
s.
In Spring MVC, either a resolved String lookupPath or a parsed
RequestPath
is always available within DispatcherServlet
processing. However in a Servlet Filter
such as CorsFilter
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 to false
when an
application is using parsed PathPatterns
in which case the
RequestPath
can be parsed earlier via
ServletRequestPathFilter
.
allowInitLookupPath
- whether to disable lazy initialization
and fail if not already resolved@Deprecated public void setLookupPathAttributeName(String name)
UrlPathHelper.PATH_ATTRIBUTE
.UrlPathHelper.getLookupPathForRequest(HttpServletRequest)
.
By default this is UrlPathHelper.PATH_ATTRIBUTE
.
name
- the request attribute to checkpublic void setPathMatcher(PathMatcher pathMatcher)
PathMatcher
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 a
parsed
RequestPath
is available.
public void setCorsConfigurations(@Nullable Map<String,CorsConfiguration> corsConfigurations)
For pattern syntax see AntPathMatcher
and PathPattern
as well as class-level Javadoc for details on which one may in use.
Generally the syntax is largely the same with PathPattern
more
tailored for web usage.
corsConfigurations
- the mappings to usePathPattern
,
AntPathMatcher
public void registerCorsConfiguration(String pattern, CorsConfiguration config)
setCorsConfigurations(Map)
to register one mapping at a time.pattern
- the mapping patternconfig
- the CORS configuration to use for the patternPathPattern
,
AntPathMatcher
public Map<String,CorsConfiguration> getCorsConfigurations()
@Nullable public CorsConfiguration getCorsConfiguration(HttpServletRequest request)
CorsConfigurationSource
CorsConfiguration
based on the incoming request.getCorsConfiguration
in interface CorsConfigurationSource
CorsConfiguration
, or null
if none