我有一个IsAuthenticated
函数来检查请求是否经过身份验证(检查Authorization
标头中的JWT )
func IsAuthenticated(a *framework.AppContext, r *http.Request) (int, error) {
// ... do authentication. user is authenticated User object
ctx := context.WithValue(r.Context(), "user", user)
r = r.WithContext(ctx)
return 200, nil
}
Run Code Online (Sandbox Code Playgroud)
我发现它似乎r = r.WithContext(ctx)
没有覆盖请求对象?我该如何实现呢?我需要重新订购请求吗?
我不清楚你所展示的是什么是"中间件",或者它是如何被执行的.由于您只是更改了本地变量r
,并且从未将其提供给其他任何人,因此您的更改无法在您的函数外部显示.
在go版本中有各种各样的中间件风格,但从1.7开始,我看到了更多的中间件功能:
func IsAuthenticated(next http.Handler) http.Handler{
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
//authenticate. Set cookies.
//403 if not authenticated, etc.
ctx := context.WithValue(r.Context(), "user",u)
next(w,r.WithContext(ctx))
})
}
Run Code Online (Sandbox Code Playgroud)
这可以用于将处理程序链接在一起,如下所示:
http.Handle("/foo/bar", IsAuthenticated(myHandler))
Run Code Online (Sandbox Code Playgroud)
(或使用像alice这样的东西来使链接中间件更容易.)
这样,当myHandler
被调用时,它将创建新的请求IsAuthenticated
,并且可以通过以下方式获取用户:
user := r.Context().Get("user").(*User)