From b69467e699df58f86a24308f3bdba3b800ca662c Mon Sep 17 00:00:00 2001 From: lijie2000 <63935127@qq.com> Date: Thu, 7 Nov 2024 12:35:24 +0800 Subject: [PATCH] branch-add-cors-in-example-plugin --- server/plugins/example/main.go | 13 +++++++++++++ server/plugins/example/templates/example_login.html | 4 +++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/server/plugins/example/main.go b/server/plugins/example/main.go index 19dcbe16..5171367b 100644 --- a/server/plugins/example/main.go +++ b/server/plugins/example/main.go @@ -186,6 +186,8 @@ func AuthWithHttp(ctx *gin.Context, req *common.HttpKnockRequest, helper *plugin return } + corsMiddleware(ctx) + switch { case strings.EqualFold(action, "valid"): ackMsg, err = authRegular(ctx, req, res, helper) @@ -316,6 +318,17 @@ func AuthWithNHP(req *common.NhpAuthRequest, helper *plugins.NhpServerPluginHelp return ackMsg, err } +func corsMiddleware(ctx *gin.Context) { + originResource := ctx.Request.Header.Get("Origin") + + if (originResource != "") { + // HTTP headers for CORS + ctx.Writer.Header().Set("Access-Control-Allow-Origin", originResource) // allow cross-origin resource sharing + } + + ctx.Next() +} + func main() { } diff --git a/server/plugins/example/templates/example_login.html b/server/plugins/example/templates/example_login.html index 68511541..af9d5a75 100644 --- a/server/plugins/example/templates/example_login.html +++ b/server/plugins/example/templates/example_login.html @@ -289,7 +289,9 @@
"&password=" + encodeURIComponent(password); console.log(nhpValidUrl); - fetch(nhpValidUrl) + fetch(nhpValidUrl,{ + credentials: "include" + }) .then(response => response.json()) .then(result => { console.log(result);