我正在开发一个网络应用程序,我依赖以下代码进行身份验证(我正在使用 github.com/dgrijalva/jwt-go 包):
func ValidateProtectedPage(protectedPage http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
// If no Auth cookie is set then return a 404 not found
cookie, err := req.Cookie("Auth")
if err != nil {
Forbidden(res)
return
}
// Return a Token using the cookie
token, err := jwt.ParseWithClaims(cookie.Value, &Claims{}, func(token *jwt.Token) (interface{}, error) {
// Make sure token's signature wasn't changed
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected siging method")
}
return []byte(util.Secret), nil
})
if err != nil {
Forbidden(res)
return
}
// Grab the tokens claims and pass it into the original request
if claims, ok := token.Claims.(*Claims); ok && token.Valid {
ctx := context.WithValue(req.Context(), "Auth", *claims)
protectedPage(res, req.WithContext(ctx))
} else {
Forbidden(res)
}
})}
问题是,我正在尝试将我的应用程序部署到 appengine,但出现以下错误:
planilla-go/controllers/auth.go:45: req.Context undefined (type *http.Request has no field or method Context)
planilla-go/controllers/auth.go:46: req.WithContext undefined (type *http.Request has no field or method WithContext)
根据我的阅读,这是由于 appengine 的请求与 go 库中的请求不兼容,但我还找不到解决方法
有没有办法将 http.Request 包装或转换为使用 appengine 的?