Class Cors.Builder

java.lang.Object
com.jsgalactic.axiom.security.Cors.Builder
Enclosing class:
Cors

public static final class Cors.Builder extends Object
Configures Cors. Not thread-safe; build once at startup.
  • Method Details

    • allowOrigin

      public Cors.Builder allowOrigin(String origin)
      Allows one origin. Call again for more.
      Parameters:
      origin - exact serialized origin: http or https, a host name or IP literal and an optional port, with no path, wildcard or user information
      Returns:
      this builder
      Throws:
      IllegalArgumentException - for anything else, including * (use anyOrigin()) and null
    • anyOrigin

      public Cors.Builder anyOrigin()
      Allows every origin with Access-Control-Allow-Origin: *, for public APIs that never receive credentials. Cannot be combined with allowCredentials().
      Returns:
      this builder
    • allowMethods

      public Cors.Builder allowMethods(String... methods)
      Sets the methods a preflight may request, replacing the default of GET and HEAD. A method is still only granted if the route allows it.
      Parameters:
      methods - HTTP method tokens, upper case, never *
      Returns:
      this builder
      Throws:
      IllegalArgumentException - for a wildcard, an invalid token or an empty list
    • allowHeaders

      public Cors.Builder allowHeaders(String... headers)
      Sets the request headers a preflight may ask for (none by default). Matching is case-insensitive. Headers the Fetch standard always permits need not be listed.
      Parameters:
      headers - header name tokens, never *
      Returns:
      this builder
    • exposeHeaders

      public Cors.Builder exposeHeaders(String... headers)
      Makes response headers readable by the page's script.
      Parameters:
      headers - header name tokens, never *
      Returns:
      this builder
    • allowCredentials

      public Cors.Builder allowCredentials()
      Lets browsers send credentials (cookies, Authorization) with cross-origin requests; incompatible with anyOrigin().
      Returns:
      this builder
    • maxAge

      public Cors.Builder maxAge(Duration maxAge)
      Sets how long a browser may cache a preflight answer.
      Parameters:
      maxAge - zero (no header) to 24 hours, whole seconds; 10 minutes by default
      Returns:
      this builder
    • build

      public Cors build()
      Builds the middleware.
      Returns:
      the immutable middleware
      Throws:
      IllegalStateException - without an origin, or for anyOrigin() combined with origins or credentials