

consumer: Enter the ID of the consumer that requires application access control. If it is left blank, the IP address access control applies to all consumers.hide credentials: whether to hide credentials from the upstream service.anonymous: optional string (user uuid) used as the "anonymous" user if authentication fails. If it is empty (default), the request fails, identity authentication fails, and a 4xx response is returned. Note that this value should refer to the consumer ID field rather than the custom_id field in Cloud Native API Gateway.clock skew: clock skew seconds to prevent the replay attack.validate request body: whether body verification is enabled.enforce headers: list of HTTP headers that should participate in HMAC signature calculation.algorithms: list of HMAC digest algorithms the user wants to support. Allowed values are hmac-sha1, hmac-sha256, hmac-sha384, and hmac-sha512. By default, all these values are supported.

username: user (application) name. Either this field or custom_id should be specified.custom_id: custom identifier used to map the user to another database. Either this field or username should be specified.Tags: tag.

username: username used in HMAC signature verification.secret: secret value. If it is not specified, a secret is generated automatically by default.
Authorization: hmac username="username", algorithm="hmac-sha1", headers="x-date digest", signature="Base64(HMAC-SHA1(signing_str, secret))"username: username of the credential.algorithm: digital signature algorithm used to create signatures.headers: list of HTTP header names used to sign requests, which are separated by spaces.signature: base64-encoded digital signature generated by the client.Digest: SHA-256=base64(sha256(<body>))# -*- coding: utf-8 -*-import base64import datetimeimport hashlibimport hmacimport jsonimport requestsfrom urllib.parse import urlparse, urlencode#usernameUsername = 'xxx'#secretSecret = 'xxxx'# Access the address.Url = 'http://test.com/'HTTPMethod = 'POST' # methodAccept = 'application/json'ContentType = 'application/json'urlInfo = urlparse(Url)Host = urlInfo.hostnamePath = urlInfo.pathDigest = ''GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'xDate = datetime.datetime.utcnow().strftime(GMT_FORMAT)body_json = ''# Modify the body content.if HTTPMethod == 'POST' :# Enter the actual request body.body = { "arg1": "a", "arg2": "Chinese" }body_json = json.dumps(body)body_digest = hashlib.sha256(body_json.encode()).digest()Digest = "SHA-256=" + base64.b64encode(body_digest).decode()# Obtain the signature string.signing_str = 'x-date: %s\\ndigest: %s' % (xDate, Digest)# Calculate the signature.sign = hmac.new(Secret.encode(), msg=signing_str.encode(), digestmod=hashlib.sha1).digest()sign = base64.b64encode(sign).decode()auth = "hmac username=\\"" + Username + "\\", algorithm=\\"hmac-sha1\\", headers=\\"x-date digest\\", signature=\\""sign = auth + sign + "\\""// Send the request.headers = {'Host': Host,'Accept': Accept,'Content-Type': ContentType,'x-date': xDate,'Authorization': sign,'Digest': Digest}if HTTPMethod == 'GET' :ret = requests.get(Url, headers=headers)if HTTPMethod == 'POST' :ret = requests.post(Url, headers=headers, data=(body_json))print(ret.headers)print(ret.text)
package org.example;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.client.methods.HttpGet;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import javax.crypto.Mac;import javax.crypto.spec.SecretKeySpec;import java.net.URI;import java.nio.charset.StandardCharsets;import java.text.SimpleDateFormat;import java.util.Base64;import java.util.Date;import java.util.Locale;import java.util.TimeZone;public class Main {public static void main(String[] args) throws Exception {String username = "xxxx";String secret = "xxxx";String url = "http://www.test.com/";String httpMethod = "POST";String accept = "application/json";String contentType = "application/json";URI uri = new URI(url);String host = uri.getHost();String digest = "";SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);format.setTimeZone(TimeZone.getTimeZone("GMT"));String xDate = format.format(new Date());String body = "";if (httpMethod.equals("POST")) {body = "arg1=a&arg2=Chinese";}byte[] bodyDigest = java.security.MessageDigest.getInstance("SHA-256").digest(body.getBytes(StandardCharsets.UTF_8));digest = "SHA-256=" + Base64.getEncoder().encodeToString(bodyDigest);System.out.println(digest);String signingStr = "x-date: " + xDate + "\\ndigest: " + digest;Mac mac = Mac.getInstance("HmacSHA1");mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA1"));byte[] signBytes = mac.doFinal(signingStr.getBytes(StandardCharsets.UTF_8));String sign = Base64.getEncoder().encodeToString(signBytes);String auth = "hmac username=\\"" + username + "\\", algorithm=\\"hmac-sha1\\", headers=\\"x-date digest\\", signature=\\"";sign = auth + sign + "\\"";HttpResponse response = null;CloseableHttpClient httpClient = HttpClients.createDefault();if (httpMethod.equals("POST")) {HttpPost httpPost = new HttpPost(url);httpPost.setHeader("Host", host);httpPost.setHeader("Accept", accept);httpPost.setHeader("Content-Type", contentType);httpPost.setHeader("x-date", xDate);httpPost.setHeader("Authorization", sign);if (!digest.isEmpty()) {httpPost.setHeader("Digest", digest);}StringEntity entity = new StringEntity("arg1=a&arg2=Chinese", "UTF-8");httpPost.setEntity(entity);response = httpClient.execute(httpPost);} (httpMethod.equals("GET")) {HttpGet httpGet = new HttpGet(url);httpGet.setHeader("Host", host);httpGet.setHeader("Accept", accept);httpGet.setHeader("Content-Type", contentType);httpGet.setHeader("x-date", xDate);httpGet.setHeader("Authorization", sign);if (!digest.isEmpty()) {httpGet.setHeader("Digest", digest);}response = httpClient.execute(httpGet);}HttpEntity responseEntity = response.getEntity();System.out.println(EntityUtils.toString(responseEntity));}}
フィードバック