From a4b760ecb4df3f190ab97338705ad01b04d51c28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=96=87=E9=9D=99?= <1319697849@qq.com> Date: Wed, 25 Jun 2025 10:12:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0AppKey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + c2c.http | 26 +++++++++++++++++++++++--- router/router.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 94efd67..6a2eb22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - 增加音乐专辑字段 - 修改日志打印 - 故事返回增加专辑字段 +- http入口新增需要AppKey校验 ## [0.0.1-beta.3] - 2025.6.19 ### Changed diff --git a/c2c.http b/c2c.http index ab4eed0..c733167 100644 --- a/c2c.http +++ b/c2c.http @@ -1,3 +1,22 @@ +POST https://testnlu.gree.com/semantic/unisoundc2c/child/querys +#POST http://localhost:19992/semantic/unisoundc2c/child/querys/v2 +Content-Type: application/json + +{ + "reqParam": { + "common": { + "remoteIP": "14.215.222.17", + "trafficParameter": "ver=3.0;scenario=smarthome;filterName=nlu2;req_nlu_length=2;returnType=json;fullDuplex=true;appendLength=1;additionalService=geli_nlu;mid=10f04;version=0.5;macWifi=28b77c23817b;macVoice=28b77c23817b" + }, + "nluRet": { + "asr_recongize": "", + "text": "暂停闹钟" + } + }, + "version": "v0" +} + +### POST https://testnlu.gree.com/semantic/unisoundc2c/speech-nlu-parse Content-Type: application/json @@ -5,7 +24,7 @@ Content-Type: application/json "ip": "14.215.222.17", "mac": "ece154a865eb", "mid": "11011", - "query":"我想听儿歌", + "query":"暂停闹钟", "requestId": "123456111" } @@ -13,6 +32,7 @@ Content-Type: application/json #POST http://172.28.54.27:13302/semantic/unisoundc2c/speech-nlu-parse POST http://172.28.124.110:13302/semantic/unisoundc2c/speech-nlu-parse Content-Type: application/json +X-AppKey: 391b50b5ed634fc49a3db7cfd6bc40db { "ip": "14.215.222.17", @@ -39,7 +59,7 @@ Content-Type: application/json "trafficParameter": "mode=childxxx;macWifi=28b77c218ed1;mid=10f05;vender=7e000025;macVoice=testyuntiancloud;ver=3.0;scenario=smarthome;filterName=nlu;req_nlu_length=1;returnType=json;fullDuplex=true;appendLength=1;additionalService=geli_nlu2;version=0.5;filterUrl=https://testnlu.gree.com:443/semantic/unisoundc2c/querys;" }, "nluRet": { - "text": "播放音乐" + "text": "暂停闹钟" }, "postProc": {} } @@ -80,7 +100,7 @@ Content-Type: application/json "trafficParameter": "mode=childxx;macWifi=ece154a73d38;mid=11011;macVoice=testyuntiancloud;ver=3.0;scenario=smarthome;filterName=nlu;req_nlu_length=1;returnType=json;fullDuplex=true;appendLength=1;additionalService=geli_nlu2;version=0.5;filterUrl=https://testnlu.gree.com:443/semantic/unisoundc2c/querys;" }, "nluRet": { - "text": "播放故事" + "text": "暂停闹钟" }, "postProc": {} } diff --git a/router/router.go b/router/router.go index 2486f05..666e399 100644 --- a/router/router.go +++ b/router/router.go @@ -5,6 +5,7 @@ import ( "github.com/gin-gonic/gin" "github.com/google/uuid" "github.com/tidwall/gjson" + "net/http" "speech-nlu-parse/app" "speech-nlu-parse/global" "speech-nlu-parse/model" @@ -20,6 +21,9 @@ func InitRouter() (engine *gin.Engine) { engine = gin.New() engine.Use(gin.Recovery()) + // 校验AppKey + engine.Use(VerifyAppKeyMiddleware()) + // 超时限制测试 //engine.Use(middleware.TimeLimitMiddleware(time.Second * 5)) engine.POST(global.ServerSetting.UriPath, Controller) @@ -84,3 +88,30 @@ func SpeechNluParseSemanticRequest(req SnpReq) *model.SemanticReq { return semanticReq } + +// 校验AppKey +func VerifyAppKeyMiddleware() gin.HandlerFunc { + validAppKeys := map[string]bool{ + "391b50b5ed634fc49a3db7cfd6bc40db": true, + } + + return func(c *gin.Context) { + appKey := c.GetHeader("X-AppKey") + + if appKey == "" { + c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{ + "error": "App key is required", + }) + return + } + + if !validAppKeys[appKey] { + c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{ + "error": "Invalid app key", + }) + return + } + + c.Next() + } +} -- GitLab