Class HerodotusPathPatternRequestMatcher.Builder

java.lang.Object
cn.herodotus.engine.oauth2.authorization.matcher.HerodotusPathPatternRequestMatcher.Builder
Enclosing class:
HerodotusPathPatternRequestMatcher

public static final class HerodotusPathPatternRequestMatcher.Builder extends Object
A builder for specifying various elements of a request for the purpose of creating a HerodotusPathPatternRequestMatcher.

To match a request URI like /app/servlet/my/resource/** where /app is the context path, you can do PathPatternRequestMatcher.withDefaults().matcher("/servlet/my/resource/**")

If you have many paths that have a common path prefix, you can use basePath to reduce repetition like so:

     PathPatternRequestMatcher.Builder mvc = withDefaults().basePath("/mvc");
     http
         .authorizeHttpRequests((authorize) -> authorize
              .requestMatchers(mvc.matcher("/user/**")).hasAuthority("user")
              .requestMatchers(mvc.matcher("/admin/**")).hasAuthority("admin")
         )
             ...
 
  • Constructor Details

    • Builder

      Builder()
    • Builder

      Builder(org.springframework.web.util.pattern.PathPatternParser parser)
    • Builder

      Builder(org.springframework.web.util.pattern.PathPatternParser parser, String basePath)
  • Method Details

    • basePath

      Match requests starting with this basePath.

      Prefixes should be of the form /my/prefix, starting with a slash, not ending in a slash, and not containing and wildcards

      Parameters:
      basePath - the path prefix
      Returns:
      the HerodotusPathPatternRequestMatcher.Builder for more configuration
    • matcher

      Match requests having this path pattern.

      When the HTTP method is null, then the matcher does not consider the HTTP method

      Path patterns always start with a slash and may contain placeholders. They can also be followed by /** to signify all URIs under a given path.

      These must be specified relative to any context path prefix. A basePath may be specified to reuse a common prefix, for example a servlet path.

      The following are valid patterns and their meaning

      • /path - match exactly and only `/path`
      • /path/** - match `/path` and any of its descendents
      • /path/{value}/** - match `/path/subdirectory` and any of its descendents, capturing the value of the subdirectory in RequestAuthorizationContext.getVariables()

      A more comprehensive list can be found at PathPattern.

      Parameters:
      path - the path pattern to match
      Returns:
      the HerodotusPathPatternRequestMatcher.Builder for more configuration
    • matcher

      public HerodotusPathPatternRequestMatcher matcher(@Nullable org.springframework.http.HttpMethod method, String path)
      Match requests having this HttpMethod and path pattern.

      When the HTTP method is null, then the matcher does not consider the HTTP method

      Path patterns always start with a slash and may contain placeholders. They can also be followed by /** to signify all URIs under a given path.

      These must be specified relative to any context path prefix. A basePath may be specified to reuse a common prefix, for example a servlet path.

      The following are valid patterns and their meaning

      • /path - match exactly and only `/path`
      • /path/** - match `/path` and any of its descendents
      • /path/{value}/** - match `/path/subdirectory` and any of its descendents, capturing the value of the subdirectory in RequestAuthorizationContext.getVariables()

      A more comprehensive list can be found at PathPattern.

      Parameters:
      method - the HttpMethod to match, may be null
      path - the path pattern to match
      Returns:
      the HerodotusPathPatternRequestMatcher.Builder for more configuration
    • matcher