ソースを参照

feat: support sub domain for cors (#1827)

Kevin Wan 3 年 前
コミット
5c9fae7e62
2 ファイル変更8 行追加1 行削除
  1. 2 1
      rest/internal/cors/handlers.go
  2. 6 0
      rest/internal/cors/handlers_test.go

+ 2 - 1
rest/internal/cors/handlers.go

@@ -2,6 +2,7 @@ package cors
 
 
 import (
 import (
 	"net/http"
 	"net/http"
+	"strings"
 
 
 	"github.com/zeromicro/go-zero/rest/internal/response"
 	"github.com/zeromicro/go-zero/rest/internal/response"
 )
 )
@@ -81,7 +82,7 @@ func isOriginAllowed(allows []string, origin string) bool {
 			return true
 			return true
 		}
 		}
 
 
-		if o == origin {
+		if strings.HasSuffix(origin, o) {
 			return true
 			return true
 		}
 		}
 	}
 	}

+ 6 - 0
rest/internal/cors/handlers_test.go

@@ -31,6 +31,12 @@ func TestCorsHandlerWithOrigins(t *testing.T) {
 			reqOrigin: "http://local",
 			reqOrigin: "http://local",
 			expect:    "http://local",
 			expect:    "http://local",
 		},
 		},
+		{
+			name:      "allow sub origins",
+			origins:   []string{"local", "remote"},
+			reqOrigin: "sub.local",
+			expect:    "sub.local",
+		},
 		{
 		{
 			name:      "allow all origins",
 			name:      "allow all origins",
 			reqOrigin: "http://local",
 			reqOrigin: "http://local",