Class CorsFilter
java.lang.Object
io.micronaut.http.server.cors.CorsFilter
- All Implemented Interfaces:
Ordered, ConditionalFilter
Responsible for handling CORS requests and responses.
- Since:
- 1.0
- Author:
- James Kleeh, Graeme Rocher
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intprotected final HttpServerConfiguration.CorsConfigurationFields inherited from interface Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
Constructor Summary
ConstructorsConstructorDescriptionCorsFilter(HttpServerConfiguration.CorsConfiguration corsConfiguration, @Nullable StaticResourceResolver staticResourceResolver, Router router, @Nullable HttpHostResolver httpHostResolver) -
Method Summary
Modifier and TypeMethodDescriptionfinal @Nullable HttpResponse<?> filterPreFlightRequest(HttpRequest<?> request) final @Nullable HttpResponse<?> filterRequest(HttpRequest<?> request) final voidfilterResponse(HttpRequest<?> request, MutableHttpResponse<?> response) intgetOrder()booleanisEnabled(HttpRequest<?> request) The filter condition.protected voidsetAllowCredentials(CorsOriginConfiguration config, MutableHttpResponse<?> response) protected voidsetAllowHeaders(List<?> optionalAllowHeaders, MutableHttpResponse<?> response) protected voidsetAllowMethods(HttpMethod method, MutableHttpResponse<?> response) protected voidsetAllowPrivateNetwork(CorsOriginConfiguration config, MutableHttpResponse<?> response) Sets the HTTP Header "Access-Control-Allow-Private-Network" in the response totrue, if theCorsOriginConfiguration.isAllowPrivateNetwork()istrue.protected voidsetExposeHeaders(List<String> exposedHeaders, MutableHttpResponse<?> response) protected voidsetMaxAge(long maxAge, MutableHttpResponse<?> response) protected voidsetOrigin(@Nullable String origin, MutableHttpResponse<?> response) protected voidsetVary(MutableHttpResponse<?> response) protected booleanshouldDenyToPreventDriveByLocalhostAttack(CorsOriginConfiguration corsOriginConfiguration, HttpRequest<?> request) protected booleanshouldDenyToPreventDriveByLocalhostAttack(String origin, HttpRequest<?> request)
-
Field Details
-
CORS_FILTER_ORDER
public static final int CORS_FILTER_ORDER -
corsConfiguration
-
-
Constructor Details
-
CorsFilter
@Inject public CorsFilter(HttpServerConfiguration.CorsConfiguration corsConfiguration, @Nullable StaticResourceResolver staticResourceResolver, Router router, @Nullable HttpHostResolver httpHostResolver) - Parameters:
corsConfiguration- TheCorsOriginConfigurationinstancestaticResourceResolver- Static Resource Resolverrouter- RouterhttpHostResolver- HTTP Host resolver
-
-
Method Details
-
isEnabled
Description copied from interface:ConditionalFilterThe filter condition.- Specified by:
isEnabledin interfaceConditionalFilter- Parameters:
request- The request- Returns:
- true if the filter is enabled
-
filterPreFlightRequest
@PreMatching @RequestFilter @Internal public final @Nullable HttpResponse<?> filterPreFlightRequest(HttpRequest<?> request) -
filterRequest
@RequestFilter @Internal public final @Nullable HttpResponse<?> filterRequest(HttpRequest<?> request) -
filterResponse
@ResponseFilter @Internal public final void filterResponse(HttpRequest<?> request, MutableHttpResponse<?> response) -
shouldDenyToPreventDriveByLocalhostAttack
protected boolean shouldDenyToPreventDriveByLocalhostAttack(CorsOriginConfiguration corsOriginConfiguration, HttpRequest<?> request) - Parameters:
corsOriginConfiguration- CORS Origin configuration for request's HTTP Header origin.request- HTTP Request- Returns:
- true if the resolved host is localhost or 127.0.0.1 address and the CORS configuration has any for allowed origins.
-
shouldDenyToPreventDriveByLocalhostAttack
- Parameters:
origin- HTTP HeaderHttpHeaders.ORIGINvalue.request- HTTP Request- Returns:
- true if the resolved host is localhost or 127.0.0.1 and origin is not one of these then deny it.
-
getOrder
-
setAllowCredentials
- Parameters:
config- TheCorsOriginConfigurationinstanceresponse- TheMutableHttpResponseobject
-
setAllowPrivateNetwork
protected void setAllowPrivateNetwork(CorsOriginConfiguration config, MutableHttpResponse<?> response) Sets the HTTP Header "Access-Control-Allow-Private-Network" in the response totrue, if theCorsOriginConfiguration.isAllowPrivateNetwork()istrue.- Parameters:
config- TheCorsOriginConfigurationinstanceresponse- TheMutableHttpResponseobject
-
setExposeHeaders
- Parameters:
exposedHeaders- A list of the exposed headersresponse- TheMutableHttpResponseobject
-
setVary
- Parameters:
response- TheMutableHttpResponseobject
-
setOrigin
- Parameters:
origin- The originresponse- TheMutableHttpResponseobject
-
setAllowMethods
- Parameters:
method- TheHttpMethodobjectresponse- TheMutableHttpResponseobject
-
setAllowHeaders
- Parameters:
optionalAllowHeaders- A list with optional allow headersresponse- TheMutableHttpResponseobject
-
setMaxAge
- Parameters:
maxAge- The max ageresponse- TheMutableHttpResponseobject
-