登录 用户中心() [退出] 后台管理 注册
   
您的位置: 首页 >> SoftHub关联区 >> 主题: [golang]http.FileServer 中如何控制不让访问目录及添加 "Access-Control-Allow-Origin"    [最新]     [回主站]
[golang]http.FileServer 中如何控制不让访问目录及添加 "Access-Control-Allow-Origin"
clq
浏览(314) - 2019-09-20 15:10:16 发表 编辑

关键字: golang_bug

[2019-09-20 15:12:46 最后更新]
[golang]http.FileServer 中如何控制不让访问目录及添加 "Access-Control-Allow-Origin"

golang 默认的 http 模块可以很简单的搭建一个文件访问服务器,例如这样
http.Handle("/html/", http.FileServer(http.Dir(""))); //只有这样才能正确的访问 http://127.0.0.1:8888/html/mail_vip.html

但这样出来的文件系统有个问题,即是不能跨域的,而且还会暴露整个文件夹下的全部文件。找了好久总觉得应该有什么属性能配置,不过未果。目前能找到的方法基本上都是扩展上面代码中的

http.FileServer 部分,这个函数有么有后会得到一个 fileHandler 。然后再定义一个 http.HandleFunc 来代替前面的 http.Handle ,这样就可以在 http.HandleFunc 函数中做完想做的事情后再
调用 fileHandler.ServeHTTP(w ResponseWriter, r *Request) 就可以了。一旦理解了这个原理,倒也是蛮简单的。

相关源码在 D:\Go\src\net\http\fs.go

示例如下
--------------------------------------------------------
    var h = http.FileServer(http.Dir(""));
    static_fs = h;

http.HandleFunc("/html/", staticHandler);

//2019 静态文件扩展
//原文链接:https://blog.csdn.net/fyxichen/article/details/60570484
//这里可以自行定义安全策略
var static_fs http.Handler;
//func static(w http.ResponseWriter, r *http.Request) {
func staticHandler(w http.ResponseWriter, r *http.Request) {
   
    defer PrintError("staticHandler");
   
    fmt.Printf("访问静态文件:%s\n", r.URL.Path)
    //old := r.URL.Path
    //r.URL.Path = strings.Replace(old, "/static", "/client", 1)
    //staticfs.ServeHTTP(w, r);
   
    //Ajax跨域问题的两种解决方法之一,据说 html5 后的才支持
    w.Header().Set("Access-Control-Allow-Origin", "*");
   
    static_fs.ServeHTTP(w, r);
}//



clq  2019-09-20 15:12:46 发表 编辑

参考
Golang1.8标准库http.Fileserver跟http.ServerFile小例子


2017年03月06日 09:33:18 JieLinDee 阅读数 4853 文章标签: golang 标准 库 更多
分类专栏: Golang
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/fyxichen/article/details/60570484

package main

import (
    "fmt"
    "net/http"
    "os"
    "path"
    "strings"
)

var staticfs = http.FileServer(http.Dir("D:\\code\\20160902\\src\\"))

func main() {
    //浏览器打开的时候显示的就是D:\\code\\20160902\\src\\client目录下的内容"
    http.Handle("/client/", http.FileServer(http.Dir("D:\\code\\20160902\\src\\")))
    http.HandleFunc("/static/", static)
    http.HandleFunc("/js/", js)
    http.HandleFunc("/", route)
    http.ListenAndServe(":1789", nil)
}

func route(w http.ResponseWriter, r *http.Request) {
    fmt.Println(r.URL)
    fmt.Fprintln(w, "welcome")
    r.Body.Close()
}

//这里可以自行定义安全策略
func static(w http.ResponseWriter, r *http.Request) {
    fmt.Printf("访问静态文件:%s\n", r.URL.Path)
    old := r.URL.Path
    r.URL.Path = strings.Replace(old, "/static", "/client", 1)
    staticfs.ServeHTTP(w, r)
}

//设置单文件访问,不能访问目录
func js(w http.ResponseWriter, r *http.Request) {
    fmt.Printf("不能访问目录:%s\n", r.URL.Path)
    old := r.URL.Path
    name := path.Clean("D:/code/20160902/src" + strings.Replace(old, "/js", "/client", 1))
    info, err := os.Lstat(name)
    if err == nil {
        if !info.IsDir() {
            http.ServeFile(w, r, name)
        } else {
            http.NotFound(w, r)
        }
    } else {
        http.NotFound(w, r)
    }
}




总数:1 页次:1/1 首页 尾页  
总数:1 页次:1/1 首页 尾页  


所在合集/目录
golang http.FileServer 专题 更多



发表评论:
文本/html模式切换 插入图片 文本/html模式切换


附件:



NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.

Copyright © 2005-2020 clq, All Rights Reserved
版权所有
桂ICP备15002303号-1