特定路由的 Gorilla 中间件身份验证不起作用
来源:stackoverflow
时间:2024-04-03 16:18:33 184浏览 收藏
学习Golang要努力,但是不要急!今天的这篇文章《特定路由的 Gorilla 中间件身份验证不起作用》将会介绍到等等知识点,如果你想深入学习Golang,可以关注我!我会持续更新相关文章的,希望对大家都能有所帮助!
我正在为 api 编写一个 http 路由器,有些路由需要身份验证,而有些则不需要。
我不想在每条路线上都要求身份验证,所以我将它们分开。
但是有一个问题:
post /account <--这是帐户注册端点,不需要身份验证
delete /account <--这确实需要身份验证才能删除当前帐户
我不知道如何正确分离它们,而且我目前尝试使两者的中间件不同的尝试也失败了:
package httpServer import ( "log" "net/http" "httpServer/handlers" "httpServer/middlewares" "github.com/gorilla/mux" "github.com/justinas/alice" ) func Init() { log.Println("Initializing http routes...") defaultmiddlewares := alice.New(middlewares.Logger, middlewares.Recover) authmiddlewares := alice.New(middlewares.Authenticator) var mainRouter = mux.NewRouter() var authRouter = mux.NewRouter() // No auth required to call this mainRouter.HandleFunc("/health", handlers.HealthGet).Methods("GET") // Get API health // authrouter should be a extension of main router (i think) mainRouter.Handle("/", authmiddlewares.Then(authRouter)) // Authentication is not required for this mainRouter.HandleFunc("/account", handlers.AccountPost).Methods("POST") // Create an account // Authentication is required for this authRouter.HandleFunc("/account", handlers.AccountDelete).Methods("DELETE") // Delete my account // WebSocket endpoint: authRouter.HandleFunc("/ws", handlers.UpgradeWs) authRouter.HandleFunc("/ws/", handlers.UpgradeWs) // If i dont add this it doesnt work?? // Register mainRouter http.Handle("/", defaultmiddlewares.Then(mainRouter)) }
调用 get /health 就可以了: 但对 delete /account 的调用失败,并显示 404 not found:
(也在 init() 函数末尾附近,我注册了一个 websocket 端点,出于某种原因,如果我不注册这两个端点,它就无法连接?)
正确答案
您初始化了两个路由器 mainrouter
和 authrouter
,并且仅启动 mainrouter
。 delete /account
绑定到 authrouter
。那就是没有启动和监听。这就是 404 出现的原因。
您可以将自定义中间件实现编写为 gorilla/mux Middleware interface 并与 gorilla/mux
路由器一起使用。示例代码如下
func Init() { log.Println("Initializing http routes...") r := mux.NewRouter() middleware := Middleware{ // inject any dependency if you need } r.Use(middleware.MiddlewareFunc) // No auth required to call this r.HandleFunc("/health", handlers.HealthGet).Methods("GET") // Get API health // authrouter should be a extension of main router (i think) r.Handle("/", authmiddlewares.Then(authRouter)) // Authentication is not required for this r.HandleFunc("/account", handlers.AccountPost).Methods("POST") // Create an account // Authentication is required for this r.HandleFunc("/account", handlers.AccountDelete).Methods("DELETE") // Delete my account http.ListenAndServe(":8080", r) } // Middleware your custom middleware implementation type Middleware struct {} func (m Middleware) MiddlewareFunc(handler http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // you can check request method and paths and you can do authentications here //eg := method = DELETE and path = /account, do authentication handler.ServeHTTP(w, r) }) }
今天关于《特定路由的 Gorilla 中间件身份验证不起作用》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习