允许使用gorilla handlers的来源

huangapple go评论105阅读模式
英文:

Allowing origins using gorilla handlers

问题

我目前正在编写一个 RESTful Web 服务器,并希望从 Angular2 前端进行测试。由于在开发过程中服务器托管在另一个域上,我需要使用 Access-Control-Allow-Origin: *(我认为是这样)。我尝试使用 gorilla handlers 包来实现这一点,具体如下所示:

  1. origins := handlers.AllowedOrigins([]string{"*"})
  2. log.Fatal(http.ListenAndServe(":"+os.Getenv(util.Port),
  3. handlers.LoggingHandler(os.Stdout, handlers.CORS(origins)(router))))

现在,当我尝试使用以下 curl 请求服务器时:

  1. curl -H "Origin: http://example.com" \
  2. -H "Access-Control-Request-Method: POST" \
  3. -H "Access-Control-Request-Headers: X-Requested-Width" \
  4. -X OPTIONS --verbose localhost:8000

我在服务器上收到一个 OPTIONS 请求,返回 403 错误。我还尝试添加头部和允许的方法:

  1. handlers.AllowedHeaders([]string{"X-Requested-With"})
  2. handlers.AllowedMethods([]string{"GET", "POST", "PUT", "OPTIONS"})

但没有任何改变。我该如何解决这个问题?

英文:

I'm currently writing a restful web server, that I would like to test from angular2 frontend. Since the server is hosted on another domain while developing I need Access-Control-Allow-Origin: * (I think). I tried to achieve that by using the gorilla handlers package, namely the following:

  1. origins := handlers.AllowedOrigins([]string{"*"})
  2. log.Fatal(http.ListenAndServe(":"+os.Getenv(util.Port),
  3. handlers.LoggingHandler(os.Stdout, handlers.CORS(origins)(router))))

now when trying to request the server using the following curl:

  1. curl -H "Origin: http://example.com" \
  2. -H "Access-Control-Request-Method: POST" \
  3. -H "Access-Control-Request-Headers: X-Requested-Width" \
  4. -X OPTIONS --verbose localhost:8000

I get an OPTIONS request on the server, which returns 403. I have also tried adding headers and allowed methods:

  1. handlers.AllowedHeaders([]string{"X-Requested-With"})
  2. handlers.AllowedMethods([]string{"GET", "POST", "PUT", "OPTIONS"})

but it made no difference. How can I resolve this?

答案1

得分: 2

这是我的翻译:

这对我有效:

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "os"
  6. "github.com/gorilla/handlers"
  7. "github.com/gorilla/mux"
  8. )
  9. func main() {
  10. router := mux.NewRouter()
  11. log.Fatal(http.ListenAndServe(":8080",
  12. handlers.LoggingHandler(os.Stdout, handlers.CORS(
  13. handlers.AllowedMethods([]string{"POST"}),
  14. handlers.AllowedOrigins([]string{"*"}),
  15. handlers.AllowedHeaders([]string{"X-Requested-With"}),
  16. )(router))))
  17. }

在你的curl示例中,X-Requested-With被拼写错误:

  1. $ curl -H "Origin: http://example.com" -H "Access-Control-Request-Method: POST" -H "Access-Control-Request-Headers: X-Requested-With" -X OPTIONS --verbose localhost:8080
  2. * Rebuilt URL to: localhost:8080/
  3. * Trying ::1...
  4. * Connected to localhost (::1) port 8080 (#0)
  5. > OPTIONS / HTTP/1.1
  6. > Host: localhost:8080
  7. > User-Agent: curl/7.50.1
  8. > Accept: */*
  9. > Origin: http://example.com
  10. > Access-Control-Request-Method: POST
  11. > Access-Control-Request-Headers: X-Requested-With
  12. >
  13. < HTTP/1.1 200 OK
  14. < Access-Control-Allow-Headers: X-Requested-With
  15. < Access-Control-Allow-Origin: http://example.com
  16. < Date: Thu, 16 Feb 2017 22:58:24 GMT
  17. < Content-Length: 0
  18. < Content-Type: text/plain; charset=utf-8
  19. <
  20. * Connection #0 to host localhost left intact
英文:

This works for me:

  1. package main
  2. import (
  3. &quot;log&quot;
  4. &quot;net/http&quot;
  5. &quot;os&quot;
  6. &quot;github.com/gorilla/handlers&quot;
  7. &quot;github.com/gorilla/mux&quot;
  8. )
  9. func main() {
  10. router := mux.NewRouter()
  11. log.Fatal(http.ListenAndServe(&quot;:8080&quot;,
  12. handlers.LoggingHandler(os.Stdout, handlers.CORS(
  13. handlers.AllowedMethods([]string{&quot;POST&quot;}),
  14. handlers.AllowedOrigins([]string{&quot;*&quot;}),
  15. handlers.AllowedHeaders([]string{&quot;X-Requested-With&quot;}))(router))))
  16. }

The X-Requested-With is mistyped in your curl example:

  1. $ curl -H &quot;Origin: http://example.com&quot; -H &quot;Access-Control-Request-Method: POST&quot; -H &quot;Access-Control-Request-Headers: X-Requested-With&quot; -X OPTIONS --verbose localhost:8080
  2. * Rebuilt URL to: localhost:8080/
  3. * Trying ::1...
  4. * Connected to localhost (::1) port 8080 (#0)
  5. &gt; OPTIONS / HTTP/1.1
  6. &gt; Host: localhost:8080
  7. &gt; User-Agent: curl/7.50.1
  8. &gt; Accept: */*
  9. &gt; Origin: http://example.com
  10. &gt; Access-Control-Request-Method: POST
  11. &gt; Access-Control-Request-Headers: X-Requested-With
  12. &gt;
  13. &lt; HTTP/1.1 200 OK
  14. &lt; Access-Control-Allow-Headers: X-Requested-With
  15. &lt; Access-Control-Allow-Origin: http://example.com
  16. &lt; Date: Thu, 16 Feb 2017 22:58:24 GMT
  17. &lt; Content-Length: 0
  18. &lt; Content-Type: text/plain; charset=utf-8
  19. &lt;
  20. * Connection #0 to host localhost left intact

huangapple
  • 本文由 发表于 2017年2月10日 15:39:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/42153963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定