2

我正在使用“github.com/gin-gonic/gin”“github.com/graphql-go/graphql”在golang中创建一个graphql api为了保护我的api,我将使用jwt令牌并且我想保留我的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

    }
}
4

1 回答 1

1

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

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

  • 检查请求是否仅包含登录查询,没有其他注入(无侧/并行查询)...(剥离新行/白字符...正则表达式)...精确短语 -必须完全等于预定义的模板、长度!!!
  • 并提供了所需的变量
于 2020-03-08T09:56:34.767 回答