gpt4 book ai didi

go - 从 "github.com/graphql-go/graphql"中的请求中获取查询名称

转载 作者:行者123 更新时间:2023-12-01 21:11:23 26 4
gpt4 key购买 nike

我正在 golang 中创建一个 graphql api
使用
“github.com/gin-gonic/gin”
“github.com/graphql-go/graphql”
为了保护我的 api,我将使用 jwt token ,并且我想让我的 api 完全是 graphql(允许的唯一路由是 localhost:9000/graphql)
有没有办法从请求中获取查询名称,所以我只会对除登录之外的所有其他查询进行 jwtparsing

我的句柄文件

package graphql

import (
"fmt"
"log"

"*****/graphql/mutations"
"*****/graphql/queries"
"github.com/gin-gonic/gin"
"github.com/graphql-go/graphql"
"github.com/graphql-go/handler"
)

func Handler() gin.HandlerFunc {

schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(
graphql.ObjectConfig{Name: "QueryType", Fields: graphql.Fields{
"book": queries.BookQuery,
"books": queries.GetAllBooks,
"login": queries.Login,
}},
),
Mutation: graphql.NewObject(
graphql.ObjectConfig{Name: "MutationType", Fields: graphql.Fields{
"insertOneBook": mutations.InsertOneBook,
"updateOneBook": mutations.UpdateOneBook,
"deleteOneBook": mutations.DeleteOneBook,
}},
),
})

if err != nil {

log.Fatal("error Parsing")
}

h := handler.New(&handler.Config{
Schema: &schema,
Pretty: true,
GraphiQL: true,
Playground: true,
})
return func(c *gin.Context) {
// Get the header authorisation
// fmt.Println(c.Request.Header)
// authHeader := c.GetHeader("Authorization")

// Get the token by removing the "Bearer" string
// tokenString := strings.SplitN(authHeader, " ", -1)
// fmt.Println("this is token string", tokenString)
// if len(tokenString) < 2 {
// c.AbortWithStatus(http.StatusUnauthorized)
// } else {
// authState := utils.JwtValidate(tokenString[1])
// if authState != http.StatusAccepted {
// c.AbortWithStatus(authState)
// } else {
// h.ServeHTTP(c.Writer, c.Request)
// }
// }
h.ServeHTTP(c.Writer, c.Request)
// Check is tokens validity

}
}

最佳答案

它是 json - 你可以检查 [string] 是否包含 login ...

...但这是关于安全性...您正在绕过...

  • 检查请求是否包含 仅登录查询,无其他注入(inject) (无边/并行查询)...(去除新行/白字符...正则表达式)...精确短语 - 也必须完全等于预定义的模板、长度 !!!
  • 并提供了所需的变量
  • 关于go - 从 "github.com/graphql-go/graphql"中的请求中获取查询名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60581987/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com