mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
Refactor out setCorsHeaders()
This commit is contained in:
parent
329a6a8132
commit
577bf91d25
@ -29,21 +29,23 @@ const (
|
|||||||
headerAllowHeaders = "Access-Control-Allow-Headers"
|
headerAllowHeaders = "Access-Control-Allow-Headers"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func setCorsHeaders(ctx *context.Context, origin string) {
|
||||||
|
ctx.Output.Header(headerAllowOrigin, origin)
|
||||||
|
ctx.Output.Header(headerAllowMethods, "POST, GET, OPTIONS, DELETE")
|
||||||
|
ctx.Output.Header(headerAllowHeaders, "Content-Type, Authorization")
|
||||||
|
}
|
||||||
|
|
||||||
func CorsFilter(ctx *context.Context) {
|
func CorsFilter(ctx *context.Context) {
|
||||||
origin := ctx.Input.Header(headerOrigin)
|
origin := ctx.Input.Header(headerOrigin)
|
||||||
originConf := conf.GetConfigString("origin")
|
originConf := conf.GetConfigString("origin")
|
||||||
|
|
||||||
if ctx.Request.Method == "POST" && ctx.Request.RequestURI == "/api/login/oauth/access_token" {
|
if ctx.Request.Method == "POST" && ctx.Request.RequestURI == "/api/login/oauth/access_token" {
|
||||||
ctx.Output.Header(headerAllowOrigin, origin)
|
setCorsHeaders(ctx, origin)
|
||||||
ctx.Output.Header(headerAllowMethods, "POST, GET, OPTIONS, DELETE")
|
|
||||||
ctx.Output.Header(headerAllowHeaders, "Content-Type, Authorization")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctx.Request.RequestURI == "/api/userinfo" {
|
if ctx.Request.RequestURI == "/api/userinfo" {
|
||||||
ctx.Output.Header(headerAllowOrigin, origin)
|
setCorsHeaders(ctx, origin)
|
||||||
ctx.Output.Header(headerAllowMethods, "POST, GET, OPTIONS, DELETE")
|
|
||||||
ctx.Output.Header(headerAllowHeaders, "Content-Type, Authorization")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,9 +56,7 @@ func CorsFilter(ctx *context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ok {
|
if ok {
|
||||||
ctx.Output.Header(headerAllowOrigin, origin)
|
setCorsHeaders(ctx, origin)
|
||||||
ctx.Output.Header(headerAllowMethods, "POST, GET, OPTIONS, DELETE")
|
|
||||||
ctx.Output.Header(headerAllowHeaders, "Content-Type, Authorization")
|
|
||||||
} else {
|
} else {
|
||||||
ctx.ResponseWriter.WriteHeader(http.StatusForbidden)
|
ctx.ResponseWriter.WriteHeader(http.StatusForbidden)
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user