Fork me on GitHub

Matchers: (v6.5)

1) Definition

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 your own matchers):

3) CORS

The CorsMatcher adds CORS headers on the response (Access-Control-Allow-Origin, Access-Control-Expose-Headers, Access-Control-Max-Age, Access-Control-Allow-Credentials, Access-Control-Allow-Methods, Access-Control-Allow-Headers) and then returns true.

By default, the allowAjaxRequests keyword maps to a preconfigured CorsMatcher with:

4) securityHeaders

The securityHeaders matchers are part of the default matchers in the DefaultMatchingChecker (via the securityheaders keyword).

They are equivalent to: xssprotection,noframe,hsts,nosniff,nocache.

5) Other matchers

Most pac4j implementations use the pac4j logics and matchers and thus the DefaultMatchingChecker component. In that case, the following matchers are automatically available via the following short keywords:

These short names are defined as constants in DefaultMatchers. You can override them with your own matchers using the same names.