Skip to content

Commit 919eaf1

Browse files
authored
fix: fix CORS error after sucessful OPTION (beego#838)
1 parent cd902a2 commit 919eaf1

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

‎routers/cors_filter.go‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1+
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package routers
216

317
import (
418
"net/http"
519

620
"github.com/astaxie/beego/context"
21+
"github.com/casdoor/casdoor/conf"
722
"github.com/casdoor/casdoor/object"
823
)
924

@@ -15,17 +30,20 @@ const (
1530
)
1631

1732
func CorsFilter(ctx *context.Context) {
18-
if ctx.Input.Method() == "OPTIONS" {
19-
origin := ctx.Input.Header(headerOrigin)
20-
33+
origin := ctx.Input.Header(headerOrigin)
34+
if origin != "" && origin != conf.GetConfigString("origin") {
2135
if object.IsAllowOrigin(origin) {
2236
ctx.Output.Header(headerAllowOrigin, origin)
2337
ctx.Output.Header(headerAllowMethods, "POST, GET, OPTIONS")
2438
ctx.Output.Header(headerAllowHeaders, "Content-Type, Authorization")
25-
ctx.ResponseWriter.WriteHeader(http.StatusOK)
2639
} else {
2740
ctx.ResponseWriter.WriteHeader(http.StatusForbidden)
41+
return
42+
}
43+
44+
if ctx.Input.Method() == "OPTIONS" {
45+
ctx.ResponseWriter.WriteHeader(http.StatusOK)
46+
return
2847
}
29-
return
3048
}
3149
}

0 commit comments

Comments
 (0)