duan19913 2019-07-02 18:09
浏览 280
已采纳

自定义HTTP标头打破了CORS

My API has following CORS setup:
(I am the owner, I can change these settings)

Middleware function:

// HeaderMiddleware ...
func HeaderMiddleware(next httprouter.Handle) httprouter.Handle {
    return httprouter.Handle(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
        w.Header().Set("Content-Type", "application/json")
        w.Header().Set("Access-Control-Allow-Origin", "*")
        w.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, X-APIKEY")
        // ! Production
        // if r.Header.Get("X-APIKEY") != "fdfdsf5df6d541cd6" || r.RemoteAddr != frontendURL {
        //  w.WriteHeader(http.StatusForbidden)
        //  json.NewEncoder(w).Encode(NoContentResponse{Success: false, Error: "You aren't allowed to request the api here."})
        //  return
        // }
        // ! Production

        next(w, r, p)
    })
}

The X-APIKEY header is not necessary yet, a request without it just works fine:

fetch('http://localhost:8013/tobi@gmx.at/usage', { headers: { } })
.then(response => response.json())
.then(console.log)

returns {used: false} (expected response)

However, if I add the X-APIKEY header:

fetch('http://localhost:8013/tobi@gmx.at/usage', { headers: { 'X-APIKEY': 'sdfsdfsafsf' } })
.then(response => response.json())
.then(console.log)

following error is thrown:
Access to fetch at 'http://localhost:8013/tobiwibu@gmx.at/usage' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

If I do the request with the X-APIKEY header in Postman, it says that the Access-Control-Allow-Origin header is sent along: Postman says the header is sent along

P.S.: I already tried other headers, it works! If I do the request with chrome (without X-APIKEY header), the Access-Control-Allow-Origin header is sent.

Thanks for your help!

  • 写回答

1条回答 默认 最新

  • 普通网友 2019-07-05 17:57
    关注

    I've now fixed it like this:
    I misuse the http Accept header for my API-Token.

    Example:

    fetch('http://10.0.0.11:8013/lopm@htl/usage',
    {"headers":{ "Accept": "fdfdsf5df6d541cd6++" }})
    .then(response => response.json())
    .then(console.log)
    


    Of course, this isn't a really nice solution, but it does its job.

    Thanks for all of you for giving me helpful tips!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看