【问题标题】:Go Mux Middlware not using my CORS handlerGo Mux 中间件不使用我的 CORS 处理程序
【发布时间】:2021-09-20 05:49:51
【问题描述】:
mx := mux.NewRouter()

mx.Use(CorsHandler)


sch := mx.NewRoute().Subrouter()
sch.Use(middleware.ValidateSchoolToken)

teacher := mx.NewRoute().Subrouter()
teacher.Use(middleware.ValidateToken)

代码运行时不使用CorsHandler

【问题讨论】:

  • 您好@nator 欢迎来到SO,您能描述一下您遇到的错误吗?另一件事,可以帮助你的问题,是张贴minimal reproducible example
  • 描述您遇到的错误。并解释你的问题

标签: go cors mux


【解决方案1】:

您是否已将http.MethodOptions 添加到您的方法调用中?我在文档中找到了这个示例:

func main() {
    r := mux.NewRouter()

    // IMPORTANT: you must specify an OPTIONS method matcher for the middleware to set CORS headers
    r.HandleFunc("/foo", fooHandler).Methods(http.MethodGet, http.MethodPut, http.MethodPatch, http.MethodOptions)
    r.Use(mux.CORSMethodMiddleware(r))
    
    http.ListenAndServe(":8080", r)
}

完整文档:https://pkg.go.dev/github.com/gorilla/mux#readme-handling-cors-requests

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-26
    • 2014-12-22
    • 2015-04-02
    • 1970-01-01
    • 2016-02-12
    • 2015-03-25
    • 2017-03-10
    • 1970-01-01
    相关资源
    最近更新 更多