ai之對接電信ds后端服務,通過nginx代理轉發https為http,對外請求,保持到達第三方后請求頭不變

前置環境:

  • 在微信小程序中嵌入H5頁面(智能客服),需要讓h5頁面在https的域名服務器上。即通過 nginx 部署成web服務,還得配置域名和端口443訪問。
  • 電信的第三方deepseek服務 ,只接收http請求,暫未支持https請求。
  • 所以我們要使用https的話 就需要在智文網關xxx:31170前面 自行加一個網關進行處理(https -> http)。

下面配置nginx 代理,及調試。主要麻煩在了我方與第三方簽名驗證不通過上 (postJsonWithAuth)。

1. 構建請求頭的方法

1.1

public class SignUtils 工具類

//    public static final String PARAM_HEADER_DATE = "Date";public static final String PARAM_HEADER_X_DATE = "x-date";private static final List<String> SIGNED_HEADERS = new ArrayList<>();private static final String algorithm = "HmacSHA256";private static final String hmacAlgorithm = "hmac-sha256";static {SIGNED_HEADERS.add("x-tenantid");SIGNED_HEADERS.add("x-userid");SIGNED_HEADERS.add("x-source");}
1.2
private static Map<String, String> buildHeaders() {Map<String, String> headers = new HashMap<>();headers.put("Content-Type", "application/json");headers.put("x-userid", RequestParam.getUserId());headers.put("x-tenantid", RequestParam.getTenantId());headers.put("x-source", RequestParam.getSource());LocalDateTime utcTime = LocalDateTime.now(ZoneOffset.UTC);// 定義日期時間格式DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E, dd MMM yyyy HH:mm:ss 'GMT'", Locale.ENGLISH);// 格式化時間并輸出String formattedUtcTime = utcTime.format(formatter);headers.put(SignUtils.PARAM_HEADER_X_DATE, formattedUtcTime);return headers;}
1.3

/**
API 常量類
**/
public class ApiConstants

// 對話接口: 智文-SSE對話接口public static final String CHAT_OPENAPI = "/ais/bot/openapi/dcc/sseDialog";// 智文-同步對話接口public static final String CHAT_OPENAPI_SYNC = "/ais/bot/openapi/dcc/dialog";// 數科官網本地https 轉發public static final String API_DIANXIN_PROXY = "/apidx";public static final String CHAT_OPENAPI_SYNC_PROXY = API_DIANXIN_PROXY + CHAT_OPENAPI_SYNC;

在這里插入圖片描述

主要問題就忽略了 我們簽名時,參數uri 加了"/api" 前綴,通過nginx 代理轉發后,過濾了 /api 前綴,到達第三方后,對方用的原始 uri驗簽,所以總是不通過。

過程中 在ds這臺服務器上裝了 tcpdump 抓包工具,比對本地debug(不代理)成功的報文,和訪問https生產服務器 nginx代理失敗的報文

在這里插入圖片描述

在這里插入圖片描述

/*** @param api 第三方接口uri* @param reqBody post請求體參數*/public static String postJsonWithAuth(String api, String reqBody) throws Exception {
//        String url = RequestParam.getHost() + api;// 通過nginx 代理轉發時,加上前綴 String url = RequestParam.getHost() + ApiConstants.API_DIANXIN_PROXY + api;long startTime = System.currentTimeMillis();try {Map<String, String> headers = buildHeaders();// 簽名時,與第三方保持一致,使用原始api不變 ,不使用代理后的apiString sign = SignUtils.generateAuth(RequestParam.getAccessKey(), RequestParam.getSecretKey(),"POST", api, headers, new HashMap<>(), reqBody);headers.put("Authorization", sign);// 請求開始監控log.info(">>>> HTTP請求開始 [URL: {},API: {}]", url, api);log.info(">>>> HTTP請求頭: {}", headers);log.info(">>>> HTTP請求參數 [Body: {}]", reqBody);RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), reqBody);Request request = new Request.Builder().url(url).headers(Headers.of(headers)).post(requestBody).build();try (Response response = httpClient.newCall(request).execute()) {String responseBody = response.body().string();log.info("<<<< 原始響應結果:[狀態碼: {}, 內容: {}]", response.code(), response);if (response.isSuccessful()) {return responseBody;} else {log.error("詳細錯誤響應: {}", responseBody);throw new IOException("HTTP請求失敗: " + response.code() + " - " + response.message());}}} catch (Exception e) {long cost = System.currentTimeMillis() - startTime;log.error("<<<< 請求異常 [耗時: {} ms, URL: {}, 錯誤: {}]", cost, url, e.getMessage(), e);throw e;} finally {long totalCost = System.currentTimeMillis() - startTime;log.info("==== 請求結束 [總耗時: {} ms] ====", totalCost);}}

測試方法:

@Test(timeOut = 60000)public void testSSEChat() throws Exception {String question="石家莊有什么好玩的";
//        String agentCode = queryAgentCode();String agentCode = "agent1048486107377569792";String messageId = UUID.randomUUID().toString();String sessionId = UUID.randomUUID().toString();String userId = RequestParam.getUserId();MessageRequest messageRequest = new MessageRequest();messageRequest.setMessageId(messageId);messageRequest.setSessionId(sessionId);messageRequest.setMsgType("TEXT");messageRequest.setUserId(userId);messageRequest.setContent(question);messageRequest.setQuery(question);messageRequest.setAgentCode(agentCode);messageRequest.setTest(1);messageRequest.setChatType("chat");messageRequest.setRequestTime(System.currentTimeMillis());messageRequest.setEntry("default");// 添加extraData參數Map<String, Object> extraData = new HashMap<>();Map<String, Object> filterMap = new HashMap<>();filterMap.put("knowledgeFilters", Collections.singletonList(Collections.singletonMap("knowledgeBaseCode", "1050656046175358976")));extraData.put("filter", JsonUtil.toJSONString(filterMap));extraData.put("range", "all");messageRequest.setExtraData(extraData);CountDownLatch latch = new CountDownLatch(5);AtomicBoolean received = new AtomicBoolean(false);String resp = OkHttpUtils.postJsonWithAuth(ApiConstants.CHAT_OPENAPI_SYNC, JsonUtil.toJSONString(messageRequest));log.info("同步結果: {}", resp);Thread.sleep(10000);   }

下面是Nginx配置:

# 添加詳細日志記錄
log_format proxy_debug '$remote_addr - $remote_user [$time_local] ''"$request" $status $body_bytes_sent ''"$http_referer" "$http_user_agent" ''Authorization: "$http_authorization" ''x-source: "$http_x_source" ''x-userid: "$http_x_userid" ''x-tenantid: "$http_x_tenantid" ''x-date: "$http_x_date" ''content-type: "$http_content_type" ''cookie_header: "$http_cookie" ''host_header: "$host"';server {listen       443 ssl;charset   utf-8;server_name stdai.sjzwltszkj.com ;ssl_certificate      /usr/local/nginx/conf/cert/cert.pem;ssl_certificate_key  /usr/local/nginx/conf/cert/cert.key;ssl_session_cache    shared:SSL:1m;ssl_session_timeout  5m;#charset koi8-r;#access_log  logs/host.access.log  main;# location / {location ~* \.txt$ {root /data/std/authentication;}location /ai/ {alias /www/wwwroot/static.ltkj.com/std_applet/dist/build/h5/ ;#root /www/wwwroot/static.ltkj.com/std_applet/h5/dist/;try_files $uri $uri/ /ai/index.html;index  index.html index.htm;}location /static/ {alias /www/wwwroot/static.ltkj.com/std_applet/dist/build/h5/static/;expires 1y;add_header Cache-Control "public";}# API代理 關鍵配置location /apidx/ {# 1. 保持原始HTTP版本和連接行為proxy_http_version 1.1;proxy_set_header Connection "";# 2. 保持原始Host頭proxy_set_header Host $proxy_host;proxy_set_header x-forwarded-host $host;# 傳遞所有頭并保持原始順序proxy_pass_request_headers on;# 3. 禁用不必要的頭傳遞proxy_pass_header Server;proxy_pass_header Date;proxy_hide_header 'Access-Control-Allow-Origin';proxy_hide_header 'Access-Control-Allow-Methods';proxy_hide_header 'Access-Control-Allow-Headers';# 4. 精確傳遞鑒權相關頭proxy_set_header Authorization $http_authorization;proxy_set_header x-source $http_x_source;proxy_set_header x-userid $http_x_userid;proxy_set_header x-tenantid $http_x_tenantid;proxy_set_header x-date $http_x_date;# 5. 代理到后端服務器 https -> http. 過濾掉/api/ 后保持原始請求路徑proxy_pass http://222.223.xxx.xxx:xxx70/;# 超時設置proxy_connect_timeout 60s;proxy_send_timeout 60s;proxy_read_timeout 60s;# 禁用緩沖proxy_buffering off;# CORS配置add_header 'Access-Control-Allow-Origin' 'https://stdai.sjzwltszkj.com' always;add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, X-Requested-With, X-CSRF-Token, x-source, x-userid, x-tenantid, x-date' always;add_header 'Access-Control-Allow-Credentials' 'true' always;add_header 'Access-Control-Expose-Headers' 'Authorization' always;# 處理OPTIONS預檢請求if ($request_method = 'OPTIONS') {add_header 'Access-Control-Allow-Origin' 'https://stdai.sjzwltszkj.com' always;add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,X-CSRF-Token,x-source,x-userid,x-tenantid,x-date' always;add_header 'Access-Control-Max-Age' 1728000 always;add_header 'Content-Type' 'text/plain; charset=utf-8' always;add_header 'Content-Type' 'text/plain; charset=utf-8' always;add_header 'Content-Length' 0 always;return 204;}}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}access_log /usr/local/nginx/logs/access.log proxy_debug ;}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/88080.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/88080.shtml
英文地址,請注明出處:http://en.pswp.cn/web/88080.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

第十四節:Vben Admin 最新 v5.0 (vben5) + Python Flask 快速入門 - Flask 后端 生產部署講解

Vben5 系列文章目錄 ?? 基礎篇 ? 第一節:Vben Admin 最新 v5.0 (vben5) + Python Flask 快速入門 ? 第二節:Vben Admin 最新 v5.0 (vben5) + Python Flask 快速入門 - Python Flask 后端開發詳解(附源碼) ? 第三節:Vben Admin 最新 v5.0 (vben5) + Python Flask 快速入…

Unity開發如何解決iOS閃退問題

一、iOS閃退常見原因及排查方法1. 內存問題&#xff08;最常見原因&#xff09; 癥狀表現&#xff1a; 設備發熱后閃退 加載大型場景時崩潰 控制臺出現EXC_RESOURCE RESOURCE_TYPE_MEMORY日志 解決方案&#xff1a; // 內存監控代碼 void Update() { Debug.Log($"內存使用…

【機器學習筆記 Ⅲ】5 強化學習

強化學習&#xff08;Reinforcement Learning, RL&#xff09; 強化學習是機器學習的一個分支&#xff0c;其核心思想是讓智能體&#xff08;Agent&#xff09;通過與環境&#xff08;Environment&#xff09;的交互學習最優策略&#xff08;Policy&#xff09;&#xff0c;以最…

pytorch深度學習-卷積神經網絡CNN-MNIST-gpu加速

一、為什么需要 CNN&#xff1f;從圖像識別的 “麻煩” 說起假設你想讓電腦識別一張圖片里有沒有貓。 如果用傳統神經網絡&#xff1a;一張 100100 的彩色圖片&#xff0c;有 100100330000 個像素點&#xff0c;每個像素點都是一個輸入神經元。傳統網絡需要每個輸入神經元和隱藏…

【阿里巴巴JAVA開發手冊】IDE的text file encoding設置為UTF-8; IDE中文件的換行符使用Unix格式,不要使用Windows格式。

問題&#xff1a;當使用 IDEA SSH 遠程開發時&#xff0c;SFTP 同步的 Windows 本地編輯的 config/plugin_config 文件文本內容中 “換行符”與 Unix、Linux 的文件文本內容換行符字符集不一致&#xff0c;導致 docker 容器中自定義 /opt/seatunnel/bin/install_plugin 在執行以…

自動駕駛ROS2應用技術詳解

自動駕駛ROS2應用技術詳解 目錄 自動駕駛ROS2節點工作流程自動駕駛感知融合技術詳解多傳感器數據同步技術詳解ROS2多節點協作與自動駕駛系統最小節點集 1. 自動駕駛ROS2節點工作流程 1.1 感知輸出Topic的后續處理 在自動駕駛系統中&#xff0c;感知節點輸出的各種Topic會被…

Redis底層實現原理之訂閱發布機制

文章目錄1. 通知類型2. 實現原理2.1 Pub/Sub2.1.1 基礎知識點2.1.2 頻道和訂閱者的存儲通知原理2.1.3 鍵空間通知2.1.4 客戶端消費2.1.5 缺陷2.2 Redis Stream2.2.1 基礎知識點2.2.2 基礎數據結構2.2.3 消費者組管理2.2.4 消息和消費者持久化2.2.5 消息生產和消費2.2.6 消費者拉…

【MATLAB代碼】AOA與TDOA混合定位例程,自適應基站數量,二維,可調節錨點數量。訂閱專欄后,可直接查看matlab源代碼

本文給出一個matlab代碼,用于在二維平面上,使用AOA的角度測量和TDOA的到達時間差的測量,來達到對未知點的精確定位。最后輸出定位示意圖、真實點坐標、僅AOA定位坐標與誤差、僅TDOA定位的坐標與誤差、AOA+TDOA混合定位的坐標與誤差。訂閱專欄后可直接查看源代碼,粘貼到MATL…

Node.js 所有主要版本的發布時間、穩定版本(Stable)和長期支持版本(LTS) 的整理

以下是 Node.js 所有主要版本的發布時間、穩定版本&#xff08;Stable&#xff09;和長期支持版本&#xff08;LTS&#xff09; 的整理&#xff0c;涵蓋從早期版本到當前最新版本的信息。 &#x1f4c5; Node.js 版本發布規律 每 6 個月發布一個新主版本&#xff08;偶數月&am…

【牛客刷題】小紅的v三元組

文章目錄 一、題目介紹1.1 題目描述1.2 輸入描述1.3 輸出描述1.4 示例二、解題思路2.1 核心算法設計2.2 性能優化關鍵2.3 算法流程圖三、算法實現四、算法分析4.1 時間復雜度4.2 空間復雜度4.3 正確性證明五、為什么選擇離散化+樹狀數組的解法?5.1 問題本質分析5.2 解法設計思…

c語言學習_函數遞歸

今天學習函數遞歸。函數遞歸通俗來說就是函數自己調用自己&#xff0c;遞歸的主要思考方式在于&#xff1a;把大事化小。例子&#xff1a;接受一個整型值&#xff0c;按照順序打印它的每一位。void print(unsigned int n) {if (n > 9){print(n / 10);}printf("%d"…

Bash與Zsh與Fish:在Linux中你應該使用哪個Shell

命令行 shell 是與操作系統交互的重要工具&#xff0c;使用戶能夠高效地執行命令、自動化任務和運行腳本。 雖然有各種外殼選項可供選擇&#xff0c;但Bash、Zsh和Fish作為最受歡迎的選擇脫穎而出&#xff0c;每種都提供獨特的功能&#xff0c;因此理解它們的差異對于選擇適合…

Peek-Ubuntu上Gif錄制工具-24.04LTS可裝

安裝方法&#xff08;Ubuntu24.04.2LTS測試通過&#xff09; sudo apt update sudo apt install peek純無語&#xff0c;&#x1f9df; 一個軟件&#xff0c;仨網站&#xff0c;四份重復的教程&#xff1a; 添加 PPA更新源報錯&#xff08;不支持 noble&#xff09;搜到 4 篇教…

DVWA靶場通關筆記-驗證碼繞過reCAPTCHA(High級別)

目錄 一、reCAPTCHA 二、代碼審計&#xff08;High級別&#xff09; 1、滲透準備 &#xff08;1&#xff09;配置security為High級別。 &#xff08;2&#xff09;配置RECAPTCHA參數 &#xff08;3&#xff09;再次打開靶場 2、源碼分析 &#xff08;1&#xff09;inde…

【Java安全】RMI基礎

文章目錄介紹實現服務端 Server客戶端 Client通信過程數據端與注冊中心(1099 端口)建立通訊客戶端與服務端建立 TCP 通訊客戶端序列化傳輸 調用函數的輸入參數至服務端總結介紹 RMI 全稱 Remote Method Invocation&#xff08;遠程方法調用&#xff09;&#xff0c;即在一個 J…

MySQL索引面試問題梳理

本文系統剖析MySQL索引的核心機制&#xff1a; ?索引分類全景圖?&#xff1a;詳解聚簇/非聚簇索引的邏輯差異與物理存儲特點?B樹的統治性優勢?&#xff1a;通過對比Hash/B樹揭示InnoDB的底層選擇邏輯 一、索引分類的常見困惑解析 1. 按物理存儲分類 類型 存儲內容 數量限…

SSH密鑰 與 Ed25519密鑰 是什么關系

SSH 密鑰與 Ed25519 密鑰的關系可以從技術實現和應用場景兩個角度理解。簡而言之&#xff1a;Ed25519 密鑰是 SSH 密鑰的一種類型&#xff0c;用于在 SSH 協議中實現安全認證。以下是詳細解釋&#xff1a; 1. SSH 密鑰的通用概念 SSH&#xff08;Secure Shell&#xff09;是一…

【PyTorch 當前版本不支持 NVIDIA GeForce RTX 5060 Ti處理辦法】

報錯信息 X:\Stable-Diffusion-WebUI\system\python\lib\site-packages\torch\cuda\__init__.py:215: UserWarning: NVIDIA GeForce RTX 5060 Ti with CUDA capability sm_120 is not compatible with the current PyTorch installation. The current PyTorch install supports…

Docker部署PaddleX實現PaddleOcr

Docker部署PaddleX實現PaddleOcr 一、版本說明二、安裝PaddleX三、基礎服務化部署四、接口調用實現表格識別五、調參優化一、版本說明 PaddleX 3.0Ubuntu20.04.1Docker version 26.1.3, build 26.1.3-0ubuntu1~20.04.1二、安裝PaddleX 查看服務器信息 docker安裝PaddleX 根據 …

深入學習c++之---AVL樹

VL樹簡介? AVL樹是一種自平衡二叉搜索樹&#xff0c;通過平衡因子&#xff08;Balance Factor, BF&#xff09;?和旋轉操作&#xff0c;確保樹始終保持平衡&#xff0c;避免退化成鏈表&#xff0c;從而保證查找、插入、刪除的時間復雜度穩定在 ?O(log n)?。 ?核心特點?…