如何从公用文件夹发送回Go中的图像?

问题描述 投票:0回答:1

如果有明显问题,我是新手,对不起,但我什么都没找到,所以我在这里发布。

这实际上是一个两部分的问题。

1)因此,我的项目目录中有此Web文件夹。它是公共的,我有一个中间件功能正在检查JWT。

我正在使用此逻辑来避免JWT身份验证,但现在可以正常工作。我收到unauthorized

// List of endpoints that don't require auth by an access token
        notAuth := []string{"/", "/refreshtokens", "/students/signup", "/students/signin", "/students/forgotpassword",
            "/images"}

        // Current request path
        requestPath := r.URL.Path

        // Check if the request does not need authentication, serve the request if it doesn't need it
        for _, value := range notAuth {

            if value == requestPath {
                next.ServeHTTP(w, r)
                return
            }
        }

因此,如何将该URL放入数组中,以便可以逃避JWT授权?

2)这是我将文件夹公开的方式,但我不希望这样做。

router.PathPrefix("/images").Handler(http.StripPrefix("/images", http.FileServer(http.Dir("./web/"))))

我想要的是类似的东西

router.Handle("/image/{imageName}",func(w http.ResponseWriter, request *http.Request){
http.ServeFile(this image name file)
})

所以我可以发回所请求的文件。

任何帮助将不胜感激。

go gorilla
1个回答
0
投票

对于#1:显然,请求路径不是notAuth中的请求路径之一。也许您需要检查路径是否具有notAuth中包含的前缀?如果是这样,您需要注意/,因为它会通过所有路径。如果没有,请记录请求路径并查看问题所在。

对于#2:您应该可以:

http.ServeFile(w,request,filepath.Join(baseDir,mux.Vars(request)["imageName"])
© www.soinside.com 2019 - 2024. All rights reserved.