当前位置: > > > > 从“github.com/graphql-go/graphql”中的请求中获取查询名称
从“github.com/graphql-go/graphql”中的请求中获取查询名称
来源:stackoverflow
2024-04-20 16:18:25
0浏览
收藏
哈喽!今天心血来潮给大家带来了《从“github.com/graphql-go/graphql”中的请求中获取查询名称》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!
问题内容
我正在 golang 中创建一个 graphql api 使用 “github.com/gin-gonic/gin” “github.com/graphql-go/graphql” 为了保护我的 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
}
}
解决方案
它是 json – 你可以检查 [string] 是否包含 login …
…但这与安全有关…您正在绕过…
- 检查请求是否包含仅登录查询,没有其他注入(无侧面/并行查询)…(删除新行/白色字符…正则表达式)…确切的短语 – 也必须完全等于预定义的模板、长度!!!
- 并提供了所需的变量
终于介绍完啦!小伙伴们,这篇关于《从“github.com/graphql-go/graphql”中的请求中获取查询名称》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~米云公众号也会发布Golang相关知识,快来关注吧!
