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 FilterchainOf(List<Filter> filters)Creates aFilterwhich encapsulates the provided filters into a singleFilter.static FilterchainOf(Filter... filters)Creates aFilterwhich encapsulates the provided filters into a singleFilter.static FilterconditionalFilter(Filter delegate, BiPredicate<Context,Request> condition)Create a Filter decorator that only delegates to the decorated filter if the supplied predicate returns true.static FilternewAsyncSessionFilter(AsyncSessionManager sessionManager)Creates a sessionFilterthat will use the providedAsyncSessionManagerto manage the users session.static FilternewBearerTokenAuthFilter(SecretReference<GenericSecret> tokenReference)Creates an authenticationFilterthat puts a Bearer Authorization header in the request.static FilternewBearerTokenAuthFilterWithoutRetry(SecretReference<GenericSecret> tokenReference)Creates an authenticationFilterthat puts a Bearer Authorization header in the request.static CsrfFilter.BuildernewCsrfFilter(String cookieName)Creates a filter that protects against cross-site request forgery (CSRF) attacks when using cookies for authentication.static FilternewDefaultCsrfFilter(String cookieName)Creates a filter that protects against cross-site request forgery (CSRF) attacks when using cookies for authentication.static FilternewHttpBasicAuthenticationFilter(String username, SecretReference<GenericSecret> password)Creates an authenticationFilterthat put a Basic Authorization header in the request.static FilternewHttpBasicAuthenticationFilter(String username, SecretReference<GenericSecret> password, Charset charset)Creates an authenticationFilterthat put a Basic Authorization header in the request.static FilternewOptionsFilter(String... allowedMethods)Creates aFilterwhich handles HTTP OPTIONS method requests.static FilternewSessionFilter(SessionManager sessionManager)Deprecated.static FilternewUrlEncodedHttpBasicAuthFilter(String username, SecretReference<GenericSecret> password)Creates an authenticationFilterthat put a Basic Authorization header in the request.static FilterrequestCopyFilter()
-
-
-
Method Detail
-
newOptionsFilter
public static Filter newOptionsFilter(String... allowedMethods)
Creates aFilterwhich 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 sessionFilterthat will use the providedSessionManagerto manage the users session.- Parameters:
sessionManager- TheSessionManager.- Returns:
- A session
Filter. - See Also:
newAsyncSessionFilter(AsyncSessionManager)
-
newAsyncSessionFilter
public static Filter newAsyncSessionFilter(AsyncSessionManager sessionManager)
Creates a sessionFilterthat will use the providedAsyncSessionManagerto manage the users session.- Parameters:
sessionManager- TheAsyncSessionManager.- Returns:
- A session
Filter.
-
requestCopyFilter
public static Filter requestCopyFilter()
Creates aFilterwhich 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 authenticationFilterthat 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 authenticationFilterthat 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 authenticationFilterthat 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 authenticationFilterthat 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 thetokenReferenceand 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 authenticationFilterthat 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 aFilterwhich 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 aFilterwhich 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...)
-
-