|
| 1 | +--- |
| 2 | +title: CORS |
| 3 | +description: allow list またはカスタム origin 関数で Cross-Origin Resource Sharing を有効にします。 |
| 4 | +sidebar: |
| 5 | + order: 4 |
| 6 | +--- |
| 7 | + |
| 8 | +[CORS ミドルウェア](/ja/middleware/cors/)は、どの origin が API にアクセスできるかを制御します。 |
| 9 | +許可する origin の固定リストを渡すことも、リクエストごとに判断する関数を指定することもできます。 |
| 10 | + |
| 11 | +## origin の allow list |
| 12 | + |
| 13 | +許可する origin を `middleware.CORS` に直接渡します。 |
| 14 | + |
| 15 | +```go |
| 16 | +package main |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "net/http" |
| 21 | + |
| 22 | + "github.com/labstack/echo/v5" |
| 23 | + "github.com/labstack/echo/v5/middleware" |
| 24 | +) |
| 25 | + |
| 26 | +var ( |
| 27 | + users = []string{"Joe", "Veer", "Zion"} |
| 28 | +) |
| 29 | + |
| 30 | +func getUsers(c *echo.Context) error { |
| 31 | + return c.JSON(http.StatusOK, users) |
| 32 | +} |
| 33 | + |
| 34 | +func main() { |
| 35 | + e := echo.New() |
| 36 | + e.Use(middleware.RequestLogger()) |
| 37 | + e.Use(middleware.Recover()) |
| 38 | + |
| 39 | + // CORS default |
| 40 | + // Allows requests from any origin wth GET, HEAD, PUT, POST or DELETE method. |
| 41 | + // e.Use(middleware.CORS("*")) |
| 42 | + |
| 43 | + // CORS restricted |
| 44 | + // Allows requests from any `https://labstack.com` or `https://labstack.net` origin |
| 45 | + e.Use(middleware.CORS("https://labstack.com", "https://labstack.net")) |
| 46 | + |
| 47 | + e.GET("/api/users", getUsers) |
| 48 | + |
| 49 | + sc := echo.StartConfig{Address: ":1323"} |
| 50 | + if err := sc.Start(context.Background(), e); err != nil { |
| 51 | + e.Logger.Error("failed to start server", "error", err) |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +## カスタム origin 関数 |
| 57 | + |
| 58 | +動的ポリシーには、`UnsafeAllowOriginFunc` を指定した `CORSWithConfig` を使います。 |
| 59 | +この関数はリクエストコンテキストと origin を受け取り、返す origin、リクエストを許可するか、 |
| 60 | +任意のエラーを返します。 |
| 61 | + |
| 62 | +```go |
| 63 | +package main |
| 64 | + |
| 65 | +import ( |
| 66 | + "context" |
| 67 | + "net/http" |
| 68 | + "strings" |
| 69 | + |
| 70 | + "github.com/labstack/echo/v5" |
| 71 | + "github.com/labstack/echo/v5/middleware" |
| 72 | +) |
| 73 | + |
| 74 | +var ( |
| 75 | + users = []string{"Joe", "Veer", "Zion"} |
| 76 | +) |
| 77 | + |
| 78 | +func getUsers(c *echo.Context) error { |
| 79 | + return c.JSON(http.StatusOK, users) |
| 80 | +} |
| 81 | + |
| 82 | +// allowOrigin takes the origin as an argument and returns: |
| 83 | +// - origin to add to the response Access-Control-Allow-Origin header |
| 84 | +// - whether the request is allowed or not |
| 85 | +// - an optional error. this will stop handler chain execution and return an error response. |
| 86 | +// |
| 87 | +// return origin, true, err // blocks request with error |
| 88 | +// return origin, true, nil // allows CSRF request through |
| 89 | +// return origin, false, nil // falls back to legacy token logic |
| 90 | +func allowOrigin(c *echo.Context, origin string) (string, bool, error) { |
| 91 | + // In this example we use a naive suffix check but we can imagine various |
| 92 | + // kind of custom logic. For example, an external datasource could be used |
| 93 | + // to maintain the list of allowed origins. |
| 94 | + if strings.HasSuffix(origin, ".example.com") { |
| 95 | + return origin, true, nil |
| 96 | + } |
| 97 | + return "", false, nil |
| 98 | +} |
| 99 | + |
| 100 | +func main() { |
| 101 | + e := echo.New() |
| 102 | + e.Use(middleware.RequestLogger()) |
| 103 | + e.Use(middleware.Recover()) |
| 104 | + |
| 105 | + // CORS restricted with a custom function to allow origins |
| 106 | + // and with the GET, PUT, POST or DELETE methods allowed. |
| 107 | + e.Use(middleware.CORSWithConfig(middleware.CORSConfig{ |
| 108 | + UnsafeAllowOriginFunc: allowOrigin, |
| 109 | + AllowMethods: []string{http.MethodGet, http.MethodPut, http.MethodPost, http.MethodDelete}, |
| 110 | + })) |
| 111 | + |
| 112 | + e.GET("/api/users", getUsers) |
| 113 | + |
| 114 | + sc := echo.StartConfig{Address: ":1323"} |
| 115 | + if err := sc.Start(context.Background(), e); err != nil { |
| 116 | + e.Logger.Error("failed to start server", "error", err) |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
0 commit comments