feat: support sub domain for cors (#1827)

This commit is contained in:
Kevin Wan
2022-04-25 21:56:59 +08:00
committed by GitHub
parent ec3e02624c
commit 5c9fae7e62
2 changed files with 8 additions and 1 deletions

View File

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

View File

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