HTTP Authorization
EMQX supports authorization based on HTTP applications. In this scenario, users need to set up an external HTTP application as a data source, and EMQX will make requests to the HTTP service and determine the authorization result based on the data returned by the HTTP API, thereby implementing complex authorization logic.
Note
HTTP authorization is not supported in EMQX Serverless deployments.
How HTTP Authorization Works
The authorization process resembles an HTTP API call, where EMQX, acting as the request client, needs to construct and send a request to the HTTP service according to the "API" requirements. The HTTP service must return results according to the "client's" requirements:
- The response
content-typemust beapplication/json. - The authorization result is indicated by
resultin the body, with possible valuesallow,deny. - If the returned HTTP status code is
204, the authorization result is considered to allow publishing or subscribing. - A
200response without a validresultdoes not provide a valid authorization decision. Check the service response and verify subsequent handling as described in Authorization Order. - Status codes other than
200and204do not provide a valid authorization decision. For network errors, timeouts, or unexpected responses, see the failure handling guidance in Authorization Order.
Response example:
HTTP/1.1 200 OK
Headers: Content-Type: application/json
...
Body:
{
"result": "allow" | "deny"
}Configure HTTP Authorization
In the deployment, click Access Control -> Authorization -> Extended Authorization, select HTTP Authorization, and click Configure.
For identity authorization, EMQX Cloud will use the current client information to fill in and initiate the authorization query request configured by the user, querying the client's authorization data on the HTTP server side.
You can complete the related configuration according to the following instructions:
Method: Choose the HTTP request method, options:
get,post.TIP
The
POSTmethod is recommended. When using theGETmethod, some sensitive client information might be exposed through HTTP server logs. Additionally, for untrusted environments, use HTTPS.URL: Enter the URL address of the HTTP service.
- The URL address must start either with
http://orhttps://. - Before EMQX 6.3, the URL hostname cannot contain placeholders. EMQX 6.3 and later support dynamic hostname resolution.
- You can use the following placeholders in the URL path:
${clientid}${username}${peerhost}${cert_subject}${cert_common_name}
- The URL address must start either with
Hostname Resolution: Select Static (default) to use a fixed hostname and a persistent connection pool, or Dynamic to resolve the hostname for each request. A templated URL hostname requires Dynamic. Available in EMQX 6.3 or later; see Dynamic Hostname Resolution.
Allowed Hosts: Required for templated hostnames, for example,
*.auth.example.com. Separate multiple entries with commas. Available in dynamic resolution mode in EMQX 6.3 or later.Precondition (Optional): Enter a Variform expression of 1 to 256 characters to control whether EMQX invokes this authorizer for the current publish or subscribe request. The authorizer is invoked only when the expression evaluates to the string value
'true'; otherwise, it is skipped and EMQX continues with the next enabled authorizer in the chain. This field is available for deployments running EMQX 6.3 or later. For supported client attributes, request information, and examples, see Authorizer Preconditions.Headers (optional): Configuration for HTTP request headers. Multiple headers can be added.
OAuth2 Client Credentials (optional): Enable OAuth2 Client Credentials if the external HTTP authorization service requires EMQX to authenticate before sending authorization requests. This option is available only for deployments running EMQX v6.1.4 or later. For details, see Configure OAuth2 Client Credentials.
Connection Configurations: Configure concurrent connections, connection timeout waiting time, maximum HTTP request count, and request timeout time in this section.
Enable TLS: Configure whether to enable TLS.
Connection Pool size (optional): An integer specifying the concurrent connection count from EMQX nodes to the external HTTP Server; default value:
8.Connection Timeout (optional): Enter the connection timeout duration, with units available: hours, minutes, seconds, milliseconds.
HTTP Pipelining (optional): A positive integer specifying the maximum number of HTTP requests that can be sent without waiting for responses; default value:
100.Request Timeout (optional): Enter the connection timeout duration, with units available: hours, minutes, seconds, milliseconds.
Body: The request template.
POSTrequests are sent in JSON format in the request body.GETrequests are encoded as query parameters in the URL. Mapping keys and values can use placeholders.
TIP
- If the current deployment is a Dedicated Flex edition, a VPC Peering Connection needs to be created, and the server address should be the internal network address.
- If the current deployment is a BYOC edition, a VPC Peering Connection needs to be created in your public cloud console, please refer to the Creating BYOC Deployment - VPC Peering Connection Configuration section. The server address should be the internal network address.
- If you encounter an Init resource failure! please check whether the server address is correct and whether the security group is open.
HTTP Request and Response
When the client initiates a subscribing or publishing operation, the HTTP Authorizer constructs and sends a request based on the configured request template. You need to implement authorization logic in the request template and make sure that the checking results are returned in the required format.
Request
The request can use JSON format, with the following placeholders in the URL and request body:
${clientid}: The client ID${username}: The username used by the client on login${peerhost}: The source IP address of the client (supported for compatibility with older templates).${peername}: Available in EMQX 6.3 and later. Includes the source IP and port, such as192.168.0.1:51544. With Proxy Protocol, it uses the address reported by the proxy. Use this placeholder in new templates that need both IP and port; IPv6 addresses are not enclosed in extra brackets.${proto_name}: The protocol name used by the client, e.g.MQTT,CoAP${mountpoint}: The mount point of the gateway listener (topic prefix)${action}: The action being requested, e.g.publish,subscribe${topic}: The topic (or topic filter) to be published or subscribed in the current request${qos}: The QoS of the message to be published or subscribed in the current request${retain}: Whether the message to be published in the current request is a retained message
Response
After checking, the authorization service needs to return a response in the following format:
- Response content-type must be application/json.
- If the HTTP Status Code is 200, the authorization result is granted by the HTTP Body. It depends on the value of the result field:
- allow: Allow Publish or Subscribe.
- deny: Deny Publish or Subscribe.
- If the HTTP Status Code is 204, it means that this Publish or Subscribe request is allowed.
- Status codes other than
200and204do not provide a valid authorization decision. For network errors, timeouts, or unexpected responses, see the failure handling guidance in Authorization Order. Example response:
HTTP/1.1 200 OK
Headers: Content-Type: application/json
...
Body:
{
"result": "allow" | "deny"
}TIP
It is recommended to use the POST method. When using the GET method, some sensitive information may be exposed through HTTP server logs.
For untrusted environments, HTTPS should be used.
Configure OAuth2 Client Credentials
For deployments running EMQX v6.1.4 or later, HTTP authorization supports OAuth2 Client Credentials. When this option is enabled, EMQX obtains, caches, and automatically refreshes an access token from the configured token endpoint. When EMQX sends a request to the external HTTP authorization service, it adds the token to the request as the Authorization: Bearer <access_token> header.
Enable OAuth2 Client Credentials, and configure the following options:
| Field | Description |
|---|---|
| Token Endpoint | Required. OAuth2 authorization server endpoint used to request an access token. The URL must use HTTP or HTTPS and must not contain user information. |
| Client ID | Required. OAuth2 client ID used to request an access token. |
| Client Secret | Required. OAuth2 client secret used to request an access token. |
| Scope | Optional OAuth2 scope requested for the access token. |
| Token Request Timeout | Timeout for the HTTP request to the token endpoint. The default is 5 seconds. |
| Token Endpoint TLS | Enables TLS for the token endpoint. This setting is independent of Enable TLS, which controls the connection to the external HTTP authorization service. |
EMQX sends a POST request with the application/x-www-form-urlencoded content type to the token endpoint. The request body contains grant_type, client_id, client_secret, and the optional scope. The token endpoint must return a 200 response with a JSON body that contains an access_token. It can also return token_type and expires_in. If present, token_type must be Bearer, and expires_in must be a positive integer.
Important Notice
- Do not configure an
Authorizationheader for HTTP authorization when OAuth2 is enabled. EMQX rejects the configuration because it conflicts with the automatically generated Bearer authorization header. - The token endpoint must accept the client ID and client secret as form fields in the request body. Authenticating to the token endpoint with an HTTP Basic
Authorizationheader is not supported.
Dynamic Hostname Resolution
To use different HTTP service hosts for different tenants, such as https://${client_attrs.tenant}.auth.example.com/authz:
- Set Hostname Resolution to Dynamic.
- Enter the service address with the client attribute placeholder in URL.
- Set Allowed Hosts to the permitted range, such as
*.auth.example.com. - Save the configuration, then use a client with the corresponding attribute to trigger an authorization check. Verify that EMQX sends the request to the expected HTTP service.
Each allowlist entry must be an exact hostname or a wildcard pattern starting with *.. Do not include a scheme, port, path, or spaces. A wildcard matches subdomains only: *.auth.example.com does not match auth.example.com. Hostnames are limited to 253 characters and individual labels to 63 characters. The allowlist constrains rendered template hosts; it is not applied to a literal hostname.
The URL must use HTTP or HTTPS and a literal port. Dynamic mode cannot be enabled together with OAuth2 Client Credentials. In dynamic mode, a Connection Pool Size of 0 disables connection reuse; a positive value limits retained idle connections. All rendered hosts share the pool. HTTP pipelining and the maximum inactive connection time do not apply in dynamic mode.