登录
首页 >  Golang >  Go问答

示例:使用go-swagger进行Oauth2身份验证

来源:stackoverflow

时间:2024-02-29 20:00:28 376浏览 收藏

“纵有疾风来,人生不言弃”,这句话送给正在学习Golang的朋友们,也希望在阅读本文《示例:使用go-swagger进行Oauth2身份验证》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新Golang相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!

问题内容

https://github.com/go-swagger/go-swagger/blob/master/examples/oauth2/restapi/configure_oauth_sample.go

谁能解释一下这段代码的用途吗?

// This demonstrates how to enrich and pass custom context keys.
// In this case, we cache the current responseWriter in context.
type customContextKey int8

const (
    _ customContextKey = iota
    ctxResponseWriter
)

// The middleware configuration is for the handler executors. These do not apply to the swagger.json document.
// The middleware executes after routing but before authentication, binding and validation
func setupMiddlewares(handler http.Handler) http.Handler {
    ourFunc := func(w http.ResponseWriter, r *http.Request) {
        rctx := context.WithValue(r.Context(), ctxResponseWriter, w)
        handler.ServeHTTP(w, r.WithContext(rctx))
    }
    return http.HandlerFunc(ourFunc)

}

如何丰富和传递自定义上下文键?


解决方案


简而言之:这是将自定义中间件应用于您的 go-swagger 应用程序服务的所有路由。该中间件将 responsewriter 添加为请求上下文中的自定义值。可以肯定的是,这与 oauth 无关。

一步一步进行:

context 是一种特殊类型,可以跨代码层携带请求范围的值、截止日期和取消信号。

type customcontextkey int8

这里我们定义了一个未导出的上下文键类型。我们这样做的原因是,当我们将值添加到上下文时,我们希望该值不会与其他也可能与上下文交互的包设置的值发生冲突。例如,如果我们只使用字符串“customcontextkey”,而其他一些包碰巧使用相同的字符串,则可能会发生这种情况。更多信息here

const (
    _ customcontextkey = iota
    ctxresponsewriter
)

在这里,我们创建一个名为 ctxresponsewritercustomcontextkey 值,值为 1。请注意,我们忽略 (_) iota 的第一个值 0,而是使用下一个值 1。这是类型 -用于在上下文中存储实际 responsewriter 值的安全密钥。

中间件是采用 http.handler 并返回 http.handler 的函数,它们可以组合在一起,是一种以通用方式向应用程序处理程序添加额外功能的模式。如需更深入的了解,请查看 Making and Using HTTP Middleware

func setupmiddlewares(handler http.handler) http.handler {
    ourfunc := func(w http.responsewriter, r *http.request) {
        rctx := context.withvalue(r.context(), ctxresponsewriter, w)
        handler.servehttp(w, r.withcontext(rctx))
    }
    return http.handlerfunc(ourfunc)
}

这里的函数以这样的方式包装给定的处理程序:

  • 从请求中提取上下文 - r.context()
  • 使用我们的新密钥和 w responsewriter 值“丰富” - context.withvalue(..., ctxresponsewriter, w)
  • 请求的上下文被更新后的上下文替换 - r.withcontext(rctx)
  • 包装的处理程序使用此更新的请求运行 - handler.servehttp(w, ...)

在某个 http.handler 中,您可能会像这样提取值:

func someHandler(w http.ResponseWriter, r *http.Request) {
    value, ok := r.Context().Value(ctxResponseWriter).(http.ResponseWriter)
    if ok {
        // we have the value!
    }
}

这只是一个用法示例。像这样传递 responsewriter 是非常愚蠢的,因为它已经作为参数传递给任何 http.handler,如您在上面看到的,并且从应用程序的更深层依赖它是糟糕的设计。例如,更好的用途可能是传递请求范围的记录器对象。

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>