目录
1、apisix的hmac认证Authorization头信息
lua%E7%94%9F%E6%88%90%E6%BA%90%E7%A0%81-toc" style="margin-left:0px;">2、signature的lua生成源码
java%E7%94%9F%E6%88%90%E7%AD%BE%E8%AF%81%E7%9A%84%E7%AE%80%E5%8D%95%E7%A4%BA%E4%BE%8B-toc" style="margin-left:0px;">3、java生成签证的简单示例
4、postman调用如下
apisix的hmac-auth认证,介绍可以看官方文档
hmac-auth | Apache APISIX® -- Cloud-Native API Gateway
照着官方文档,发现生成的签证根本调不通,所以研究了一下hmac-auth的生成源码,下面记录一下apisix的hmac需要怎么传参
1、apisix的hmac认证Authorization头信息
1.1hmac认证Authorization内容:
Signature signature="{具体签名}", keyId="{hmac的key}", algorithm="{hmac的算法}", headers="{需要生成签证的请求头}"
生成结果如下:
Signature signature="PpSL9rq26QtITXh33NhUpBJkNiskkkEDKKzFYqKKIC4=", keyId="a0f4cafbe86a43d5bcfdb7e97f32215b", algorithm="hmac-sha256", headers="date"
1.2参数介绍
algorithm:hmac支持的算法。具体可以看源码或者文档,只有几种,下面都是用的默认的算法HmacSHA256
headers:是说生成签名需要用的头信息,header里面的顺序也影响签名,具体签名生成见下文。下面介绍几个可能常用的header头。下面只是常用参数,还有一些别的head参数。验证了一下,不支持request-body这种不规范的
headers="date @request-target"
date: GMT格式日期。 一般是搭配签证过期窗口使用的
@request-target:是一个特殊的字段,通常用于 HTTP 请求的签名生成。它表示请求的目标 URI(即请求的路径和查询参数),而不是请求头中的其他字段,可以理解成具有含义的常量,经常会用它判断逻辑
lua%E7%94%9F%E6%88%90%E6%BA%90%E7%A0%81">2、signature的lua生成源码
hmac-auth.lua文件,在验证的时候加了一下core.log.warn打印日志,获取一些中间结果
local function generate_signature(ctx, secret_key, params)
local uri = ctx.var.request_uri
local request_method = core.request.get_method()
if uri == "" then
uri = "/"
end
local signing_string_items = {
params.keyId,
}
if params.headers then
for _, h in ipairs(params.headers) do
local canonical_header = core.request.header(ctx, h)
if not canonical_header then
if h == "@request-target" then
local request_target = request_method .. " " .. uri
core.table.insert(signing_string_items, request_target)
core.log.warn("canonical_header name:", core.json.delay_encode(h))
core.log.warn("canonical_header value: ",
core.json.delay_encode(request_target))
end
else
core.table.insert(signing_string_items,
h .. ": " .. canonical_header)
core.log.warn("canonical_header name1:", core.json.delay_encode(h))
core.log.warn("canonical_header value1: ",
core.json.delay_encode(canonical_header))
end
end
end
local signing_string = core.table.concat(signing_string_items, "\n") .. "\n"
core.log.warn("======signing_string签证:", signing_string)
return hmac_funcs[params.algorithm](secret_key, signing_string)
end
可以看出,如果请求头有@request-target,生成签证会用到uri和request_method,否则是不会用到的
(1)如果头没有@request-target,例如headers="request-line date request-body"
如果是get请求,那生成签证,就只需要keyId+"\n"+"date: "+具体GMT时间+"\n"
(2) 如果头有@request-target,例如headers="request-line date request-body @request-target"
如果是get请求,那生成签证,就要keyId+"\n"+"date: "+具体GMT时间+"\n"+request_method+" "+uri
java%E7%94%9F%E6%88%90%E7%AD%BE%E8%AF%81%E7%9A%84%E7%AE%80%E5%8D%95%E7%A4%BA%E4%BE%8B">3、java生成签证的简单示例
@Slf4j public class HmacUtils {/**** 获取签证信息* @return String 返回值,message的生成规则和顺序都不是固定的,是根据header变化的* @Author: shisha* @Date: 2024/12/25 10:22**/public static String getSignature(String key,String hmacSecret) throws Exception {String dateStr = getCurrentGmt();//key和时间 "a0f4cafbe86a43d5bcfdb7e97f32215b\ndate: Tue, 24 Dec 2024 12:27:55 GMT\n";// headers="date"String message = key +"\n"+"date: "+dateStr+"\n";//如果headers="date @request-target"//TODO message的生成规则和顺序都不是固定的,是根据header变化的message+="GET /dapi/v1.0/ssApi/getDataForGet?query=%7B%22pageIndex%22%3A1%2C%22pageSize%22%3A10%2C" +"%22apiTestToken%22%3A%2202024121716460709700000101001349%22%2C%22dz%22%3A%22%2Fdcpyo" +"%2FPRODUCTScxfw_4090%22%2C%22list%22%3A%5B%7B%22bm%22%3A%22PRODUCT_NAME%22%2C%22jtz%22%3A" +"%22RTX5060%22%7D%5D%2C%22apiId%22%3A%2202024112711241051600000101660374%22%7D&t=1734425245421\n" ;//获取签证return generateHmacSha256Base64(hmacSecret, message);}/**** Hmac生成签证的base64工具* @param message* @return String 返回值* @Author: shisha* @Date: 2024/12/25 9:49**/public static String generateHmacSha256Base64(String hmacSecret, String message) throws Exception {// 创建 HMAC-SHA256 实例Mac sha256_HMAC = Mac.getInstance("HmacSHA256");SecretKeySpec secret_key = new SecretKeySpec(hmacSecret.getBytes("UTF-8"), "HmacSHA256");sha256_HMAC.init(secret_key);// 计算 HMAC 值byte[] hmacBytes = sha256_HMAC.doFinal(message.getBytes("UTF-8"));// 进行 Base64 编码return Base64.getEncoder().encodeToString(hmacBytes);}public static String getCurrentGmt() {Date date = new Date();// Wed, 25 Dec 2024 01:50:59 GMTString form = "E, dd MMM yyyy HH:mm:ss";Locale locale = Locale.ENGLISH;SimpleDateFormat sdf = new SimpleDateFormat(form, locale);sdf.setTimeZone(TimeZone.getTimeZone("GMT"));String gmtTime = sdf.format(date) + " GMT";// 输出GMT时间字符串log.info(gmtTime);return gmtTime;}public static void main(String[] args) throws Exception {String key = "2e06116d05d6399c90c8a441942fb113";String hmacSecret = "005B192C781AEB9F641BC71DC6C63A43245E7ABBD";String signature = getSignature(key,hmacSecret);log.info(signature);//调用postman的请求参数示例,Authorization格式如下:// Signature signature="PpSL9rq26QtITXh33NhUpBJkNiskkkEDKKzFYqKKIC4=", keyId="a0f4cafbe86a43d5bcfdb7e97f32215b", algorithm="hmac-sha256", headers="request-line date request-body"}