Package org.forgerock.http.filter
Class Filters
- java.lang.Object
-
- org.forgerock.http.filter.Filters
-
public final class Filters extends Object
Utility methods for creating common types of filters.
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static Filter
chainOf(List<Filter> filters)
Creates aFilter
which encapsulates the provided filters into a singleFilter
.static Filter
chainOf(Filter... filters)
Creates aFilter
which encapsulates the provided filters into a singleFilter
.static Filter
conditionalFilter(Filter delegate, BiPredicate<Context,Request> condition)
Create a Filter decorator that only delegates to the decorated filter if the supplied predicate returns true.static Filter
newAsyncSessionFilter(AsyncSessionManager sessionManager)
Creates a sessionFilter
that will use the providedAsyncSessionManager
to manage the users session.static Filter
newBearerTokenAuthFilter(SecretReference<GenericSecret> tokenReference)
Creates an authenticationFilter
that puts a Bearer Authorization header in the request.static Filter
newBearerTokenAuthFilterWithoutRetry(SecretReference<GenericSecret> tokenReference)
Creates an authenticationFilter
that puts a Bearer Authorization header in the request.static CsrfFilter.Builder
newCsrfFilter(String cookieName)
Creates a filter that protects against cross-site request forgery (CSRF) attacks when using cookies for authentication.static Filter
newDefaultCsrfFilter(String cookieName)
Creates a filter that protects against cross-site request forgery (CSRF) attacks when using cookies for authentication.static Filter
newHttpBasicAuthenticationFilter(String username, SecretReference<GenericSecret> password)
Creates an authenticationFilter
that put a Basic Authorization header in the request.static Filter
newHttpBasicAuthenticationFilter(String username, SecretReference<GenericSecret> password, Charset charset)
Creates an authenticationFilter
that put a Basic Authorization header in the request.static Filter
newOptionsFilter(String... allowedMethods)
Creates aFilter
which handles HTTP OPTIONS method requests.static Filter
newSessionFilter(SessionManager sessionManager)
Deprecated.static Filter
newUrlEncodedHttpBasicAuthFilter(String username, SecretReference<GenericSecret> password)
Creates an authenticationFilter
that put a Basic Authorization header in the request.static Filter
requestCopyFilter()
-
-
-
Method Detail
-
newOptionsFilter
public static Filter newOptionsFilter(String... allowedMethods)
Creates aFilter
which handles HTTP OPTIONS method requests.- Parameters:
allowedMethods
- The allowed HTTP methods of the endpoint.- Returns:
- A
Filter
.
-
newSessionFilter
@Deprecated public static Filter newSessionFilter(SessionManager sessionManager)
Deprecated.Creates a sessionFilter
that will use the providedSessionManager
to manage the users session.- Parameters:
sessionManager
- TheSessionManager
.- Returns:
- A session
Filter
. - See Also:
newAsyncSessionFilter(AsyncSessionManager)
-
newAsyncSessionFilter
public static Filter newAsyncSessionFilter(AsyncSessionManager sessionManager)
Creates a sessionFilter
that will use the providedAsyncSessionManager
to manage the users session.- Parameters:
sessionManager
- TheAsyncSessionManager
.- Returns:
- A session
Filter
.
-
requestCopyFilter
public static Filter requestCopyFilter()
Creates aFilter
which creates a defensive copy of theRequest
- on which the chain of execution will be based on. At the end of the chain of execution, the request copy will be closed.- Returns:
- A
RequestCopyFilter
.
-
conditionalFilter
public static Filter conditionalFilter(Filter delegate, BiPredicate<Context,Request> condition)
Create a Filter decorator that only delegates to the decorated filter if the supplied predicate returns true.- Parameters:
delegate
- Filter to delegate tocondition
- predicate to evaluate- Returns:
- the decorated Filter
-
newHttpBasicAuthenticationFilter
public static Filter newHttpBasicAuthenticationFilter(String username, SecretReference<GenericSecret> password)
Creates an authenticationFilter
that put a Basic Authorization header in the request. It encodes the credentials usingStandardCharsets.UTF_8
.
-
newHttpBasicAuthenticationFilter
public static Filter newHttpBasicAuthenticationFilter(String username, SecretReference<GenericSecret> password, Charset charset)
Creates an authenticationFilter
that put a Basic Authorization header in the request.- Parameters:
username
- the username to use for the credentials (must not benull
).password
- a reference to the password to use for the credentials (must not benull
).charset
- the charset to use for encoding credentials (must not benull
).- Returns:
- A basic auth
Filter
. - See Also:
- RFC-2617, RFC-7617
-
newUrlEncodedHttpBasicAuthFilter
public static Filter newUrlEncodedHttpBasicAuthFilter(String username, SecretReference<GenericSecret> password)
Creates an authenticationFilter
that put a Basic Authorization header in the request. The username and password are individually URL-encoded prior to being combined, as per OAuth 2 client secret authentication.
-
newBearerTokenAuthFilter
public static Filter newBearerTokenAuthFilter(SecretReference<GenericSecret> tokenReference)
Creates an authenticationFilter
that puts a Bearer Authorization header in the request. If an invalid_token error response is returned from the request, and the request is idempotent then a new bearer token will be requested from thetokenReference
and the request automatically retried.- Parameters:
tokenReference
- a reference to the bearer token (must not benull
).- Returns:
- A bearer auth
Filter
. - See Also:
- RFC-6750
-
newBearerTokenAuthFilterWithoutRetry
public static Filter newBearerTokenAuthFilterWithoutRetry(SecretReference<GenericSecret> tokenReference)
Creates an authenticationFilter
that puts a Bearer Authorization header in the request. If an invalid_token error response is returned from the request then the failure response is returned immediately without retrying the request with a fresh token.- Parameters:
tokenReference
- a reference to the bearer token (must not benull
).- Returns:
- A bearer auth
Filter
. - See Also:
- RFC-6750
-
newCsrfFilter
public static CsrfFilter.Builder newCsrfFilter(String cookieName)
Creates a filter that protects against cross-site request forgery (CSRF) attacks when using cookies for authentication. The filter requires that all requests using the cookie are accompanied by a custom header containing an anti-CSRF token. The anti-CSRF token is cryptographically bound to the cookie value.- Parameters:
cookieName
- the name of the cookie used for authentication.- Returns:
- a builder to configure the CSRF filter.
-
newDefaultCsrfFilter
public static Filter newDefaultCsrfFilter(String cookieName)
Creates a filter that protects against cross-site request forgery (CSRF) attacks when using cookies for authentication. The filter requires that all requests using the cookie are accompanied by a custom header containing an anti-CSRF token. The anti-CSRF token is cryptographically bound to the cookie value. This method constructs the CSRF filter with a default header name of "X-CSRF-Token". The safe HTTP methods (GET, HEAD, OPTIONS) will be excluded from the filter returned by this method.- Parameters:
cookieName
- the name of the cookie used for authentication.- Returns:
- a CSRF filter with default configuration options.
-
chainOf
public static Filter chainOf(Filter... filters)
Creates aFilter
which encapsulates the provided filters into a singleFilter
.- Parameters:
filters
- The list of filters to be invoked, in order.- Returns:
- A
Filter
. - See Also:
chainOf(List)
-
chainOf
public static Filter chainOf(List<Filter> filters)
Creates aFilter
which encapsulates the provided filters into a singleFilter
.- Parameters:
filters
- The list of filters to be invoked, in order.- Returns:
- A
Filter
. - See Also:
chainOf(Filter...)
-
-