Matchers: (v4.0)
1) Definition
pac4j provides a security model and engine (specific behaviours). The “security filter” is in charge of protecting url, requesting authentication and optionally authorization.
In some cases, you may want to bypass this “security filter” and this can be done using the matchers parameter which is generally a list of matcher names. A matcher is generally defined in the security configuration.
The matchers can also be used to always apply some logic on the URLs, like adding some security headers.
2) Available matchers
A matcher can be defined by implementing the Matcher interface. It has only one method: boolean matches(WebContext context) to say if the “security filter” must be applied.
A few matchers are available (but you can of course develop yours):
-
the
PathMatcherallows you to exclude some paths from the security checks -
the
HeaderMatcherallows you to check if a given header isnullor matches a regular expression -
the
HttpMethodMatcherallows you to check if the method of the HTTP request is one of the expected defined methods.
3) Default matchers
In the DefaultMatchingChecker class (used by the DefaultSecurityLogic class), the following keywords are automatically available for the following matchers:
- the
get,post,putanddeletekeywords for the related configurations of theHttpMethodMatcher(if they do not already exist) - the
hstskeyword for theStrictTransportSecurityMatcher - the
nosniffkeyword for theXContentTypeOptionsMatcher - the
noframekeyword for theXFrameOptionsMatcher - the
xssprotectionkeyword for theXSSProtectionMatcher - the
nocachekeyword for theCacheControlMatcher - the
securityheaderskeyword as a shortcut forhsts,nosniff,noframe,xssprotection,nocache - the
csrfTokenkeyword for theCsrfTokenGeneratorMatcher - the
allowAjaxRequestskeyword for a default configuration of theCorsMatcherwith theAccess-Control-Allow-Originheader set to*. - the
nonekeyword for no matchers at all.
DefaultMatchingChecker applies the securityHeaders,csrfToken configuration.