--- title: API Security description: Secure the MockServer control plane with mTLS client certificate authentication, JWT bearer tokens, or both, plus network and CORS hardening tips. layout: page pageOrder: 2 section: 'Security' subsection: true sitemap: priority: 0.7 changefreq: 'monthly' lastmod: 2026-05-30T08:00:00+00:00 ---

Multiple techniques can be used to lock down MockServer deployments, as follows:

Proxy Authentication: When MockServer is used as an HTTP proxy, it supports HTTP Basic proxy authentication (RFC 7235) on the data plane via three configuration properties: proxyAuthenticationUsername, proxyAuthenticationPassword, and proxyAuthenticationRealm. When these are configured, MockServer responds with 407 Proxy Authentication Required and a Proxy-Authenticate: Basic realm="…" header to any CONNECT or forwarded request that does not include valid Proxy-Authorization credentials. The authentication mechanisms described below secure the control plane (expectation management, verification, retrieval, etc.), which is separate from this data-plane proxy authentication.

Common confusion: The MockServerClient.withProxyConfiguration() method configures how the client connects to MockServer through an upstream proxy—it does not add authentication to MockServer itself. See the client documentation for details.

 

Control Plane Authentication

Authentication can be enabled for all control plane requests (i.e. create expectations, clear, reset, verify, retrieve, stop, etc) using either mTLS, JWT or both.

If both mTLS and JWT are enabled mTLS will be validated first.

Control plane authentication settings are read live, not frozen at startup. The CA chain, JWK source, issuer and audience used to authenticate control plane requests are re-read from the current configuration on every request, so enabling, disabling or re-pointing control plane authentication on a running MockServer takes effect immediately — whether you change it with a system property, a Configuration setter, or PUT /mockserver/configuration.

This is what makes it possible to lock down an instance that is already running. It also means the trust anchor you set at startup is not permanent: anything that can change MockServer's configuration can change who is trusted. Two things follow. First, if you run MockServer embedded, set the CA chain on the Configuration instance you start the server with rather than through the global static properties, so unrelated code in the same JVM (including other tests) cannot move it. Second, if the control plane is reachable by anyone you would not trust to change its own trust anchor, keep control plane authentication enabled — PUT /mockserver/configuration is itself a control plane request and is refused when authentication is enabled and not satisfied.

 

Control Plane mTLS Authentication

When mTLS authentication is enabled all control plane requests need to be received over a mTLS connection where the client's X509 certificates can be validated using the controlPlaneTLSMutualAuthenticationCAChain

{% include_subpage _includes/control_plane_authentication_mtls_configuration.html %}  

Control Plane JWT Authentication

When JWT authentication is enabled all control plane requests need and JWT via a authorization header which is validated using the controlPlaneJWTAuthenticationJWKSource

{% include_subpage _includes/control_plane_authentication_jwt_configuration.html %}  

Control Plane OIDC Authentication

When OIDC authentication is enabled all control plane requests need a Bearer access token via an authorization header issued by an external OpenID Connect identity provider. The token signature is verified against the provider's JWK set (configured directly via controlPlaneOidcJwksUri or discovered from controlPlaneOidcIssuer), and its issuer, audience, expiry and required scopes are checked. The verified subject is recorded as the principal in the control plane audit log.

{% include_subpage _includes/control_plane_authentication_oidc_configuration.html %}  

Control Plane Authorization

Once a principal has been authenticated, you can layer a coarse role-based authorization check on top. Authorization maps the verified principal's OIDC scopes or groups to one of three hierarchical roles and enforces them on every control-plane operation.

Prerequisite: Authorization requires a verified principal, so OIDC authentication must be configured. Without authentication there is no principal and authorization has nothing to enforce.

Important limitations:

 

Roles

The three roles form a strict hierarchy. A principal granted a higher role satisfies every requirement at or below it:

RolePermitsExamples
readRead-only control-plane operationsretrieve, verify, verifySequence, explainUnmatched, debugMismatch
mutateEverything read permits, plus all mutating operationsexpectation, clear, reset, configuration, chaosExperiment, LLM endpoints
adminEverything mutate permits (reserved for future fine-grained admin operations)Currently identical to mutate
 

Configuration

Enable authorization with controlPlaneAuthorizationEnabled (default false) and map scope/group values to roles with controlPlaneScopeMapping:

PropertyEnvironment VariableDefaultDescription
mockserver.controlPlaneAuthorizationEnabled MOCKSERVER_CONTROL_PLANE_AUTHORIZATION_ENABLED false Enable coarse role-based authorization of all control-plane requests. Requires a verified principal (OIDC authentication must be configured).
mockserver.controlPlaneScopeMapping MOCKSERVER_CONTROL_PLANE_SCOPE_MAPPING empty Comma-separated scope=role pairs mapping a principal's OIDC scope or group value to a role (read, mutate, or admin). Example: platform-admins=admin,qa-team=mutate,viewers=read

Example — system property configuration:

-Dmockserver.controlPlaneAuthorizationEnabled=true
-Dmockserver.controlPlaneScopeMapping=platform-admins=admin,qa-team=mutate,viewers=read

Example — Java programmatic configuration:

ConfigurationProperties.controlPlaneAuthorizationEnabled(true);
ConfigurationProperties.controlPlaneScopeMapping(Map.of(
    "platform-admins", ControlPlaneRole.ADMIN,
    "qa-team",         ControlPlaneRole.MUTATE,
    "viewers",         ControlPlaneRole.READ
));
 

Behaviour

 

Configuration Hardening

For the most security-focused deployment, set the configuration properties below to the values shown. Each can be set as a Java system property (-Dmockserver.<name>=<value>), an environment variable (MOCKSERVER_<NAME>), or in a properties file — see Configuration Properties for the exact syntax and full description of each.

 

Network exposure

 

Securing the metrics scrape endpoint

The Prometheus scrape endpoint GET /mockserver/metrics is served without control-plane authentication by design: Prometheus and OpenTelemetry scrapers cannot attach a control-plane client certificate or bearer token to a scrape, so requiring one would prevent metrics from being collected at all. (In contrast, the JSON metrics snapshot PUT /mockserver/retrieve?type=METRICS goes through HttpState.handle and is gated by control-plane authentication like every other retrieve operation.)

Because the endpoint is open, be aware of what its labels can reveal to anyone with network reach:

To secure it, choose whichever fits your deployment (they combine):

 

Forwarding & proxying

 

TLS protocols

 

Response template execution

If you use response templates, note that templates cannot reach Java classes by default, so this hardening is already in place and nothing needs configuring (templates you do not use carry no risk):

 

Request parsing limits

Bound the size of inbound request lines, headers, and chunks so a single client cannot exhaust memory with an oversized request:

The body-size limits mockserver.maxRequestBodySize and mockserver.maxResponseBodySize provide complementary bounds on payload size.

 

Control plane & CORS

See Also