liveBook · Manning

Path expression

As explained in Spring Security in Action, Second Edition

A path expression is a request-path pattern syntax for selecting endpoint requests based on their URL structure.

A path expression selects endpoint requests by URL structure, allowing one authorization rule to cover related paths without enumerating every endpoint. section 8.2

Common path-pattern forms include:

  • `/a` matches only `/a`; `/a/*` matches one pathname such as `/a/b`, but not `/a/b/c`. section 8.2
  • `/a/**` matches multiple pathnames, including `/a`, `/a/b`, and `/a/b/c`; it can also appear mid-path, as in `/a/**/c`. section 8.2
  • `/a/{param}` matches a path variable, while `/a/{param:regex}` restricts that variable to values matching a regular expression. section 8.2
  • Prefer path expressions for readable rules; use a regex matcher when requirements cannot be expressed clearly with path syntax. section 8.3

Protecting a path family

Configure `requestMatchers("/a/b/**").authenticated().anyRequest().permitAll()`. `/a` remains accessible without authentication, while `/a/b` and `/a/b/c` require authentication. A path variable can be constrained similarly: `requestMatchers("/product/{code:^[0-9]*$}").permitAll()` permits numeric product codes and denies other requests. section 8.2

Table 8.1 Common expressions used for path matching with MVC matchers (view table figure)

Expression

Description

/a

Only path /a.

/a/*

The * operator replaces one pathname. In this case, it matches /a/b or /a/c, but not /a/b/c.

/a/**

The ** operator replaces multiple pathnames. In this case, /a, /a/b, and /a/b/c are a match for this expression.

/a/{param}

This expression applies to the path /a with a given path parameter.

/a/{param:regex}

This expression applies to the path /a with a given path parameter only when the value of the parameter matches the given regular expression.

Browse the compact reference table for exact, single-segment, multi-segment, variable, and regex-constrained path patterns. section 8.2

How Spring Security in Action, Second Edition covers it

How deep it goes
Defines exact, wildcard, variable, and constrained-variable patterns, then defers unusually specific matching to regular expressions.
Examples
Uses `/a/b/**`, `/a/**/c`, numeric product-code variables, and video or email routes requiring regex matching.
What it assumes
Assumes basic Spring and Java usage; requires regular-expression familiarity only when path syntax is insufficient.
What you can do after
Apply it via Spring Security requestMatchers to authorize endpoint families and constrain path-variable values.
How it connects
Connects endpoint authorization with request matchers, path variables, HTTP methods, and regular expressions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Security in Action, Second Edition. The section links point to the parts of the book it draws on.