{"id":42871,"date":"2024-12-01T13:48:47","date_gmt":"2024-12-01T05:48:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42871\/"},"modified":"2024-12-01T13:48:47","modified_gmt":"2024-12-01T05:48:47","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8go%e5%87%bd%e6%95%b0%e9%aa%8c%e8%af%81netlify%e8%ba%ab%e4%bb%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42871\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Go\u51fd\u6570\u9a8c\u8bc1Netlify\u8eab\u4efd"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u5982\u4f55\u4f7f\u7528Go\u51fd\u6570\u9a8c\u8bc1Netlify\u8eab\u4efd<\/span><\/p>\n<h1>\u5982\u4f55\u4f7f\u7528Go\u51fd\u6570\u9a8c\u8bc1Netlify\u8eab\u4efd<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-26 21:48:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528Go\u51fd\u6570\u9a8c\u8bc1Netlify\u8eab\u4efd\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5728 Netlify \u51fd\u6570\u7684\u6587\u6863\u4e2d\uff0c\u5b83\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 Node \u6765\u8bc6\u522b\u8bf7\u6c42\u662f\u5426\u6765\u81ea\u767b\u5f55\u7528\u6237\u3002 <\/p>\n<p>https:\/\/www.netlify.com\/docs\/functions\/#identity-and-functions<\/p>\n<p>\u67e5\u770b Go \u7ed3\u6784\u4f53\u7684\u4e0a\u4e0b\u6587\uff0c\u7528\u6237\u6570\u636e\u4f3c\u4e4e\u5728 Go \u4e2d\u4e0d\u53ef\u7528\u3002\u6211\u80af\u5b9a\u9519\u8fc7\u4e86\u4ec0\u4e48\u3002\u5982\u4f55\u4f7f\u7528 Netlify \u4e2d\u7684 Go Lamda \u51fd\u6570\u9a8c\u8bc1\u7528\u6237\u5e76\u4ece JWT \u83b7\u53d6\u4ed6\u4eec\u7684\u4fe1\u606f\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u67e5\u770b\u6b64\u5e16\u5b50\uff1a<\/p>\n<\/p>\n<pre>type Bearer struct {\n    Identity *Identity `json:\"identity\"`\n    User     *User     `json:\"user\"`\n    SiteUrl  string    `json:\"site_url\"`\n    Alg      string    `json:\"alg\"`\n}\n\ntype Identity struct {\n    URL   string `json:\"url\"`\n    Token string `json:\"token\"`\n}\n\ntype User struct {\n    AppMetaData  *AppMetaData  `json:\"app_metadata\"`\n    Email        string        `json:\"email\"`\n    Exp          int           `json:\"exp\"`\n    Sub          string        `json:\"sub\"`\n    UserMetadata *UserMetadata `json:\"user_metadata\"`\n}\ntype AppMetaData struct {\n    Provider string `json:\"provider\"`\n}\ntype UserMetadata struct {\n    FullName string `json:\"full_name\"`\n}\n\nfunc AuthMiddleware(h *Handler) http.Handler {\n    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n        lc, ok := lambdacontext.FromContext(r.Context())\n        if !ok {\n            err := errors.New(\"server error\")\n            h.logger.Errorf(\"error retrieving context %+v\", r.Context())\n            http.Error(w, err.Error(), http.StatusInternalServerError)\n            return\n        }\n        bearer := lc.ClientContext.Custom[\"netlify\"]\n        raw, err := base64.StdEncoding.DecodeString(bearer)\n        if err != nil {\n            h.logger.Error(err)\n            http.Error(w, err.Error(), http.StatusInternalServerError)\n            return\n        }\n        data := Bearer{}\n        err = json.Unmarshal(raw, &amp;data)\n        if err != nil {\n            h.logger.Error(err)\n            http.Error(w, err.Error(), http.StatusInternalServerError)\n            return\n        }\n        if data.User == nil {\n            err := errors.New(\"forbidden\")\n            h.logger.Errorf(\"Unauthenticated request bearer: %+v\", bearer)\n            http.Error(w, err.Error(), http.StatusForbidden)\n            return\n        }\n        h.ServeHTTP(w, r)\n    })\n}<\/pre>\n<p>\u8fd8\u6709\u4e00\u4e2a\u535a\u5ba2\uff1a\u4e86\u89e3\u66f4\u591a\u8be6\u60c5<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528Go\u51fd\u6570\u9a8c\u8bc1Netlify\u8eab\u4efd\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u5982\u4f55\u4f7f\u7528Go\u51fd\u6570\u9a8c\u8bc1Netlify\u8eab\u4efd \u5982\u4f55\u4f7f\u7528Go\u51fd\u6570\u9a8c\u8bc1Netlify\u8eab\u4efd \u6765\u6e90\uff1astackoverflow 2024-04-26 21:48:34 0\u6d4f\u89c8 \u6536\u85cf \u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\u300a\u5982\u4f55\u4f7f\u7528Go\u51fd\u6570\u9a8c\u8bc1Netlify\u8eab\u4efd\u300b\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~ \u95ee\u9898\u5185\u5bb9 \u5728 Netlify \u51fd\u6570\u7684\u6587\u6863\u4e2d\uff0c\u5b83\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 Node \u6765\u8bc6\u522b\u8bf7\u6c42\u662f\u5426\u6765\u81ea\u767b\u5f55\u7528\u6237\u3002 https:\/\/www.netlify.com\/docs\/functions\/#identity-and-functions \u67e5\u770b Go \u7ed3\u6784\u4f53\u7684\u4e0a\u4e0b\u6587\uff0c\u7528\u6237\u6570\u636e\u4f3c\u4e4e\u5728 Go \u4e2d\u4e0d\u53ef\u7528\u3002\u6211\u80af\u5b9a\u9519\u8fc7\u4e86\u4ec0\u4e48\u3002\u5982\u4f55\u4f7f\u7528 Netlify \u4e2d\u7684 Go Lamda \u51fd\u6570\u9a8c\u8bc1\u7528\u6237\u5e76\u4ece JWT \u83b7\u53d6\u4ed6\u4eec\u7684\u4fe1\u606f\uff1f \u89e3\u51b3\u65b9\u6848 \u67e5\u770b\u6b64\u5e16\u5b50\uff1a type Bearer struct { Identity *Identity `json:&#8221;identity&#8221;` User *User `json:&#8221;user&#8221;` SiteUrl string `json:&#8221;site_url&#8221;` Alg string `json:&#8221;alg&#8221;` } type Identity struct { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-42871","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42871","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=42871"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42871\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}