日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

為HTTP觸發(fā)器配置簽名認(rèn)證

函數(shù)計算支持為HTTP觸發(fā)器配置簽名認(rèn)證,當(dāng)請求消息到達(dá)函數(shù)計算網(wǎng)關(guān)后,網(wǎng)關(guān)會對開啟簽名認(rèn)證的HTTP觸發(fā)器上的請求進(jìn)行認(rèn)證,您的函數(shù)無需再次對請求簽名進(jìn)行認(rèn)證,只需關(guān)注業(yè)務(wù)邏輯即可。本文介紹如何通過控制臺為HTTP觸發(fā)器配置簽名認(rèn)證以及如何驗證通過簽名訪問HTTP觸發(fā)器。

開啟HTTP觸發(fā)器簽名認(rèn)證

本文介紹如何在函數(shù)已有的HTTP觸發(fā)器中開啟簽名認(rèn)證,您需要先創(chuàng)建Web函數(shù),具體操作,請參見創(chuàng)建函數(shù)

  1. 登錄函數(shù)計算控制臺,在左側(cè)導(dǎo)航欄,單擊函數(shù)

  2. 在頂部菜單欄,選擇地域,然后在函數(shù)頁面,單擊目標(biāo)函數(shù)。

  3. 在函數(shù)配置頁面,選擇配置頁簽,在左側(cè)導(dǎo)航欄,單擊觸發(fā)器,然后單擊目標(biāo)觸發(fā)器右側(cè)操作列的編輯

  4. 在編輯觸發(fā)器面板,將認(rèn)證方式修改為簽名認(rèn)證,然后單擊確定

    image.png

通過簽名訪問HTTP觸發(fā)器地址

HTTP觸發(fā)器的簽名方式遵循阿里云SDK的簽名機制,詳情請參見簽名機制。阿里云SDK提供了各種語言的簽名方法,使用流程如下:

  1. 通過阿里云SDK生成簽名字符串。

  2. 將簽名字符串設(shè)置到HTTP請求的Authorization Header中。

  3. 使用任意的HTTP客戶端發(fā)起請求。

各語言簽名SDK

阿里云提供的各語言簽名SDK,您可以使用如下快捷方式安裝。

語言類型

SDK

Golang

go get github.com/alibabacloud-go/openapi-util/service

Python

pip install alibabacloud-openapi-util

Node.js

npm install @alicloud/openapi-util

Java

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>openapiutil</artifactId>
  <version>0.2.1</version>
</dependency>
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>tea-openapi</artifactId>
  <version>0.3.1</version>
</dependency>    

各語言請求示例

本文提供以下請求示例,僅供參考,完整的簽名機制請參見簽名機制

# -*- coding: utf-8 -*-

import os
from datetime import datetime
from urllib.parse import urlparse, parse_qs
import requests
from alibabacloud_openapi_util.client import Client as util
from Tea.request import TeaRequest

accessKeyId = os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID']
accessKeySecret = os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']
securityToken = os.environ.get('ALIBABA_CLOUD_SECURITY_TOKEN', '')   # 可選,使用STS時需要提供

method = 'POST'
body = 'hello world'
url = 'https://xx.cn-shanghai.fcapp.run/hello?foo=bar'   # 你的HTTP觸發(fā)器地址
date = datetime.utcnow().isoformat('T')[:19]+'Z'
headers = {
    'x-acs-date': date,
    'x-acs-security-token': securityToken
}

parsedUrl = urlparse(url)
authRequest = TeaRequest()
authRequest.method = method
authRequest.pathname = parsedUrl.path.replace('$', '%24')
authRequest.headers = headers
authRequest.query = {k: v[0] for k, v in parse_qs(parsedUrl.query).items()}

auth = util.get_authorization(authRequest, 'ACS3-HMAC-SHA256', '', accessKeyId, accessKeySecret)
headers['authorization'] = auth

resp = requests.post(url, body, headers=headers)

print(resp.text)
const util = require("@alicloud/openapi-util");
const axios = require('axios');

async function main() {
    const accessKeyId = process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'];
    const accessKeySecret = process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'];
    const securityToken = process.env['ALIBABA_CLOUD_SECURITY_TOKEN'] || ''; // 可選,使用STS時需要提供

    const method = 'POST';
    const body = 'hello world';
    const url = 'https://xx.cn-shanghai.fcapp.run/hello?foo=bar' // 你的HTTP觸發(fā)器地址
    const date = new Date().toISOString();
    let headers = {
        'x-acs-date': date,
        'x-acs-security-token': securityToken
    };

    const parsedUrl = new URL(url);
    const authRequest = {
        method: method,
        pathname: parsedUrl.pathname.replace('$', '%24'),
        headers: headers,
        query: Object.fromEntries(parsedUrl.searchParams),
    };
    console.log('auth: ', authRequest);
    const auth = util.default.getAuthorization(authRequest, 'ACS3-HMAC-SHA256', '', accessKeyId, accessKeySecret);
    headers['authorization'] = auth;
    
    const resp = await axios.post(url, body, {
        headers: headers,
    });
    console.log('resp: ', resp.data);
}

main().catch(console.error);
package main

import (
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"strings"
	"time"

	openapiutil "github.com/alibabacloud-go/openapi-util/service"
	"github.com/alibabacloud-go/tea/tea"
)

func main() {
	accessKeyId := tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
	accessKeySecret := tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
	securityToken := tea.String(os.Getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // 可選,使用STS時需要提供

	method := "POST"
	body := "hello world"
	url := "https://xx.cn-shanghai.fcapp.run/hello?foo=bar" // 你的HTTP觸發(fā)器地址
	req, err := http.NewRequest(method, url, strings.NewReader(body))
	if err != nil {
		log.Printf("new request error: %v", err)
		return
	}
	date := time.Now().UTC().Format(time.RFC3339)
	req.Header.Set("x-acs-date", date)
	req.Header.Set("x-acs-security-token", *securityToken)

	authRequest := &tea.Request{
		Method:   &method,
		Pathname: tea.String(strings.ReplaceAll(req.URL.Path, "$", "%24")),
		Headers:  make(map[string]*string),
		Query:    make(map[string]*string),
	}
	for k := range req.URL.Query() {
		authRequest.Query[k] = tea.String(req.URL.Query().Get(k))
	}
	for k := range req.Header {
		authRequest.Headers[k] = tea.String(req.Header.Get(k))
	}
	auth := openapiutil.GetAuthorization(authRequest, tea.String("ACS3-HMAC-SHA256"), nil, accessKeyId, accessKeySecret)
	req.Header.Set("authorization", *auth)

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Printf("post error: %v", err)
		return
	}
	defer resp.Body.Close()
	buf, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Printf("read body error: %v", err)
		return
	}
	log.Printf("resp: %v, body: %s", resp, string(buf))
}
package com.aliyun.sample;

import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
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 java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

import com.aliyun.tea.*;

public class Sample {
    public static void main(String[] args_) throws Exception {
        String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        String securityToken = System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN");  // 可選,使用STS時需要提供
        if (securityToken == null) {
            securityToken = "";
        }

        String method = "POST";
        String body = "hello world";
        String url = "https://xx.cn-shanghai.fcapp.run/hello?foo=bar";  // 你的HTTP觸發(fā)器地址
        Map<String, String> headers = new HashMap<String, String>();
        String date = Instant.now().toString();
        headers.put("x-acs-date", date);
        headers.put("x-acs-security-token", securityToken);

        URI uri = new URI(url);
        Map<String, String> query = new HashMap<String, String>();
        for (NameValuePair pair : URLEncodedUtils.parse(uri, StandardCharsets.UTF_8)) {
            query.put(pair.getName(), pair.getValue());
        }
        TeaRequest req = new TeaRequest();
        req.method = method;
        req.pathname = uri.getPath().replace("$", "%24");
        req.headers = headers;
        req.query = query;

        String auth = com.aliyun.openapiutil.Client.getAuthorization(
            req, "ACS3-HMAC-SHA256", "", accessKeyId, accessKeySecret);
        headers.put("authorization", auth);

        HttpPost request = new HttpPost(url);
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            request.setHeader(entry.getKey(), entry.getValue());
        }
        StringEntity entity = new StringEntity(body);
        request.setEntity(entity);

        // Execute the request
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            org.apache.http.HttpResponse response = httpClient.execute(request);
            String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
            System.out.println(responseString);
        }
    }
}

操作步驟

本文以Go語言為例,演示如何通過簽名訪問HTTP觸發(fā)器。

  1. 執(zhí)行go get github.com/alibabacloud-go/openapi-util/service安裝SDK。

  2. 在本地準(zhǔn)備代碼文件main.go

    您可以直接使用本文提供的示例代碼,詳情請參見各語言請求示例

  3. 執(zhí)行go run main.go運行代碼。

    執(zhí)行成功后返回結(jié)果如下,表示已正確地獲取函數(shù)的Response。

    2024/02/22 17:21:31 resp: &{200 OK 200 HTTP/1.1 1 1 map[Access-Control-Expose-Headers:[Date,x-fc-request-id] Content-Disposition:[attachment] Content-Length:[14] Content-Type:[text/plain; charset=utf-8] Date:[Thu, 22 Feb 2024 09:21:31 GMT] X-Fc-Request-Id:[1-65d71219-15d63510-fecf237c590c]] 0xc000120040 14 [] false false map[] 0xc000100100 0xc0000e0370}, body: Hello, Golang!

常見問題

為什么HTTP觸發(fā)器開啟簽名認(rèn)證之后,通過HTTP觸發(fā)器訪問函數(shù)提示:required HTTP header Date was not specified?

該提示說明認(rèn)證失敗,可能原因如下:

  1. 沒有在請求中進(jìn)行簽名。

  2. 在請求中做了簽名,但是沒有提供Date這個Header。

為什么HTTP觸發(fā)器開啟簽名認(rèn)證之后,通過HTTP觸發(fā)器訪問函數(shù)提示:the difference between the request time 'Thu, 04 Jan 2024 01:33:13 GMT' and the current time 'Thu, 04 Jan 2024 08:34:58 GMT' is too large?

該提示說明簽名過期,請您重新使用當(dāng)前時間進(jìn)行簽名。

為什么HTTP觸發(fā)器開啟簽名認(rèn)證之后,通過HTTP觸發(fā)器訪問函數(shù)提示:The request signature we calculated does not match the signature you provided. Check your access key and signing method?

請求中的簽名與函數(shù)計算計算得到的簽名不一致,認(rèn)證失敗。