{"id":43395,"date":"2024-12-01T13:48:28","date_gmt":"2024-12-01T05:48:28","guid":{"rendered":"https:\/\/fwq.ai\/blog\/43395\/"},"modified":"2024-12-01T13:48:28","modified_gmt":"2024-12-01T05:48:28","slug":"%e4%bd%bf%e7%94%a8-jwt-%e8%bf%9b%e8%a1%8c-graphql-golang-%e8%ba%ab%e4%bb%bd%e9%aa%8c%e8%af%81-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/43395\/","title":{"rendered":"\u4f7f\u7528 JWT \u8fdb\u884c GraphQL Golang \u8eab\u4efd\u9a8c\u8bc1"},"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>\u4f7f\u7528 JWT \u8fdb\u884c GraphQL Golang \u8eab\u4efd\u9a8c\u8bc1<\/span><\/p>\n<h1>\u4f7f\u7528 JWT \u8fdb\u884c GraphQL Golang \u8eab\u4efd\u9a8c\u8bc1<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-05-01 13:48:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4f7f\u7528 JWT \u8fdb\u884c GraphQL Golang \u8eab\u4efd\u9a8c\u8bc1\u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709\u4e00\u4e2a graphql api\uff0c\u6211\u4e00\u76f4\u5728 <code>go<\/code> \u4e2d\u7f16\u5199\uff0c\u5e76\u4e14\u60f3\u77e5\u9053\u5f53\u60a8\u5df2\u7ecf\u4f7f\u7528 <code>context<\/code> \u6765\u4f20\u9012\u6570\u636e\u6e90\u65f6\u5982\u4f55\u7ba1\u7406 jwt \u8eab\u4efd\u9a8c\u8bc1\u3002<\/p>\n<p>\u6240\u4ee5\u6211\u7684 <code>main<\/code> \u51fd\u6570\u7684\u7f29\u5199\u7248\u672c\u662f\uff1a<\/p>\n<pre>import (\n    \"net\/http\"\n    \"github.com\/graphql-go\/graphql\"\n    gqlhandler \"github.com\/graphql-go\/handler\"\n)\n\nfunc queryHandler(ds *sources.DataSources, gql *gqlhandler.Handler) http.Handler {\n    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n        ctx := context.WithValue(context.Background(), sources.CtxSourcesKey, ds)\n        gql.ContextHandler(ctx, w, r)\n    })\n}\n\nfunc main() {\n    apiSchema, _ := schema.CompileSchema(schema.QueryType, schema.MutationType)\n    gql := gqlhandler.New(&amp;gqlhandler.Config{\n        Schema:     &amp;apiSchema,\n        GraphiQL:   !isDeployed,\n        Pretty:     false,\n        Playground: false,\n    })\n    http.ListenAndServe(\":41000\", util.CreateChiRouter(healthCheckHandler(), queryHandler(ctxSources, gql)))\n}\n<\/pre>\n<p>\u5982\u60a8\u6240\u89c1\uff0c\u6211\u5df2\u7ecf\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684 <code>context<\/code> \u5b9e\u4f8b\u6765\u5b58\u50a8\u5404\u79cd\u6570\u636e\u6e90\u7684\u6620\u5c04\u5e76\u5c06\u5176\u4f20\u9012\u5230\u67e5\u8be2\u89e3\u6790\u51fd\u6570\uff0c\u4f46\u8fd8\u9700\u8981\u80fd\u591f\u89e3\u6790 <code>authorization<\/code> \u6807\u5934\u4ee5\u83b7\u53d6\u53ef\u80fd\u7684 jwt\u4e3a\u7ecf\u8fc7\u8eab\u4efd\u9a8c\u8bc1\u7684\u8def\u7531\u4f20\u9012\u3002<\/p>\n<p>\u9274\u4e8e\u6211\u76ee\u524d\u7684\u60c5\u51b5\uff0c\u89e3\u51b3\u6b64\u95ee\u9898\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f \uff08\u5c06 jwt \u4e0e\u6570\u636e\u6e90\u4e0a\u4e0b\u6587\u7ed3\u5408\u8d77\u6765\uff1f\u4ee5\u4e0d\u540c\u65b9\u5f0f\u5904\u7406\u6570\u636e\u6e90\u4ee5\u91ca\u653e <code>context<\/code>\uff1f\uff09<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5904\u7406\u6b64\u7c7b\u8eab\u4efd\u9a8c\u8bc1\u6807\u5934\u7684\u5e38\u89c1\u65b9\u6cd5\u662f\u4f7f\u7528\u4e2d\u95f4\u4ef6\u6765\u5904\u7406\u8eab\u4efd\u9a8c\u8bc1\uff0c\u5e76\u5c06\u8eab\u4efd\u9a8c\u8bc1\u4fe1\u606f\u6dfb\u52a0\u5230\u5f53\u524d\u4e0a\u4e0b\u6587\u4e2d\u3002 <\/p>\n<p>\u5f53\u524d\uff0c\u60a8\u6b63\u5728\u521b\u5efa\u4e00\u4e2a\u65b0\u4e0a\u4e0b\u6587\u3002\u6211\u5efa\u8bae\u4f7f\u7528\u73b0\u6709\u7684 http \u4e0a\u4e0b\u6587\u5e76\u6dfb\u52a0\u5230\u5176\u4e2d\uff0c\u4ee5\u4fbf\u60a8\u53ef\u4ee5\u94fe\u63a5\u4e8b\u7269\uff1a<\/p>\n<pre>ctx := context.withvalue(r.context(), sources.ctxsourceskey, ds)\nnewreq:=r.withcontext(ctx)\ngql.contexthandler(ctx, w, newreq)<\/pre>\n<p>\u60a8\u53ef\u4ee5\u5b89\u88c5\u4e00\u4e2a\u5177\u6709\u76f8\u540c\u529f\u80fd\u7684\u4e2d\u95f4\u4ef6\uff1a<\/p>\n<pre>type autoinfokeytype int\n\nconst authinfokey authinfokeytype=iota\n\nfunc getauthinfo(ctx context.context) *authinfo {\n   if v:=ctx.value(authinfokey); v!=nil {\n     return v.(*authinfo)\n   }\n   return nil\n}\n\nfunc authmiddleware(next http.handler) http.handler {\n  return http.handlerfunc(func(w http.responsewriter, r *http.request) {\n    authinfo:=processjwt(...)\n    if authinfo!=nil {\n       ctx := context.withvalue(r.context(), authinfokey, authinfo)\n       r=r.withcontext(ctx)\n    }\n    next.servehttp(w,r)\n  }\n}<\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u68c0\u67e5\u4e0a\u4e0b\u6587\u662f\u5426\u6709\u8eab\u4efd\u9a8c\u8bc1\u4fe1\u606f\uff0c\u5982\u679c\u6709\uff0c\u5219\u4f7f\u7528\u5b83\u3002<\/p>\n<pre>if authInfo:=GetAuthInfo(req.Context()); authInfo!=nil {\n  ...\n}<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u4f7f\u7528 JWT \u8fdb\u884c GraphQL Golang \u8eab\u4efd\u9a8c\u8bc1 \u4f7f\u7528 JWT \u8fdb\u884c GraphQL Golang \u8eab\u4efd\u9a8c\u8bc1 \u6765\u6e90\uff1astackoverflow 2024-05-01 13:48:33 0\u6d4f\u89c8 \u6536\u85cf \u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587\u300a\u4f7f\u7528 JWT \u8fdb\u884c GraphQL Golang \u8eab\u4efd\u9a8c\u8bc1\u300b\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591aGolang\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~ \u95ee\u9898\u5185\u5bb9 \u6211\u6709\u4e00\u4e2a graphql api\uff0c\u6211\u4e00\u76f4\u5728 go \u4e2d\u7f16\u5199\uff0c\u5e76\u4e14\u60f3\u77e5\u9053\u5f53\u60a8\u5df2\u7ecf\u4f7f\u7528 context \u6765\u4f20\u9012\u6570\u636e\u6e90\u65f6\u5982\u4f55\u7ba1\u7406 jwt \u8eab\u4efd\u9a8c\u8bc1\u3002 \u6240\u4ee5\u6211\u7684 main \u51fd\u6570\u7684\u7f29\u5199\u7248\u672c\u662f\uff1a import ( &#8220;net\/http&#8221; &#8220;github.com\/graphql-go\/graphql&#8221; gqlhandler &#8220;github.com\/graphql-go\/handler&#8221; ) func queryHandler(ds *sources.DataSources, gql *gqlhandler.Handler) http.Handler { [&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-43395","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43395","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=43395"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43395\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=43395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=43395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=43395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}