Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling customization of allow-header with CORs. #3365

Closed
kissIce opened this issue Jun 20, 2023 · 10 comments
Closed

Enabling customization of allow-header with CORs. #3365

kissIce opened this issue Jun 20, 2023 · 10 comments
Assignees
Labels
fixed Indicates an issue that has been fixed in latest version

Comments

@kissIce
Copy link

kissIce commented Jun 20, 2023

目前项目中遇到了一个问题,前端组件使用ant-design-vue 的上传组件进行文件上传时会自动带上 X-Requested-With:
XMLHttpRequest。 但是go-zero的 allowHeadersVal 里面并没有这个参数。所以会导致前端提示跨域问题。 我个人认为go-zero组件不应该写死allow-header。应该开放一个方法或者配置让我们可以自由设置 allowHeadersVal。 虽然可以通过nginx进行代理,但是当个人本地同时开发前端和后端时,一般是直接启动服务,并不会去进行nginx的相关配置。所以就会导致前后端切换开发很麻烦。希望官方能支持自定义设置header

@Mikaelemmmm
Copy link
Member

in main.go:

rest.MustNewServer(c.RestConf, rest.WithCors)
or
rest.MustNewServer(c.RestConf, rest.WithCustomCors)

@kissIce
Copy link
Author

kissIce commented Jun 20, 2023

用了这个, 但是框架是设置了默认的 allowHeadersVal = "Content-Type, Origin, X-CSRF-Token, Authorization, AccessToken, Token, Range" 。 所以如果前端请求带上了不符合的header比如 X-Requested-With: XMLHttpRequest,就会报跨域问题

@kissIce
Copy link
Author

kissIce commented Jun 20, 2023

而且我觉得项目中自定义header 也是一个普遍的需求, 所以框架不应该限制我们只能传 allowHeadersVal = "Content-Type, Origin, X-CSRF-Token, Authorization, AccessToken, Token, Range" 这里面的值

@kissIce
Copy link
Author

kissIce commented Jun 22, 2023

image image image image

就像我上面图的情况,当请求带上了allowHeadersVal 没有的值就会发生跨域问题。而go-zero里面我已经设置了允许跨域。

建议官方开放 跨域中间件 allowHeadersVal的值设定

@kevwan kevwan changed the title 关于跨域中间件 allow-header的问题 Enabling customization of allow-header with CORs. Jun 25, 2023
@kevwan kevwan self-assigned this Jun 25, 2023
@tarihub
Copy link

tarihub commented Dec 13, 2023

我这也遇到同样问题

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


I also encountered the same problem here

@daerfy
Copy link

daerfy commented Feb 1, 2024

我遇到过 但是解决了 自定义跨域处理
server := rest.MustNewServer(svcContext.Config.RestConf, rest.WithCustomCors(cors, nil, "*"))

func cors(header http.Header) {
header.Set("Access-Control-Allow-Headers", "*")
header.Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH")
header.Set("Access-Control-Expose-Headers", "Content-Length, Content-Type, Access-Control-Allow-Origin, Access-Control-Allow-Headers")
header.Set("Access-Control-Allow-Credentials", "true")
}

@kokais
Copy link

kokais commented May 15, 2024

我这边cors跨域,发现是在浏览器发起options时,自定义的header字段没有在Access-Control-Allow-Headers中,我使用的版本比较老,是v1.3.4,一开始我daerfy的方案使用,将设置的header在这里的middlewareFn注入
func WithCustomCors(middlewareFn func(header http.Header), notAllowedFn func(http.ResponseWriter), origin ...string) RunOption { return func(server *Server) { server.router.SetNotAllowedHandler(cors.NotAllowedHandler(notAllowedFn, origin...)) server.Use(cors.Middleware(middlewareFn, origin...)) } }
但发现在实际运行中options的请求路由并不会走到middlewareFn里面。后来我debug跟踪到options的请求会进入cors.NotAllowedHandler,于是我调整将注入header设置放到 notAllowedFn里,调整为下面类似代码
`
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(nil, notAllowed, "*"), rest.WithUnauthorizedCallback(unAuthorizationMw.Handle))

func notAllowed(w http.ResponseWriter) {
var allowOrigin = "Access-Control-Allow-Origin"
var allOrigins = "*"
var allowMethods = "Access-Control-Allow-Methods"
var allowHeaders = "Access-Control-Allow-Headers"
var exposeHeaders = "Access-Control-Expose-Headers"
var methods = "GET, HEAD, POST, PATCH, PUT, DELETE, OPTIONS"
var allowHeadersVal = "Content-Type, Origin, X-CSRF-Token, Authorization, AccessToken, Token, Range" // 新增自定义的header字段放这里
var exposeHeadersVal = "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers"
var maxAgeHeader = "Access-Control-Max-Age"
var maxAgeHeaderVal = "86400"
header := w.Header()
header.Set(allowOrigin, allOrigins)
header.Set(allowMethods, methods)
header.Set(allowHeaders, allowHeadersVal)
header.Set(exposeHeaders, exposeHeadersVal)
header.Set(maxAgeHeader, maxAgeHeaderVal)
}
`
手动发起options请求,发现response的header里包含了自己的字段,浏览器访问就正常了。

@kevwan
Copy link
Contributor

kevwan commented Jul 25, 2024

Use rest.WithCustomCors.

@kevwan kevwan added the fixed Indicates an issue that has been fixed in latest version label Sep 8, 2024
@kevwan
Copy link
Contributor

kevwan commented Sep 8, 2024

The feature is supported in the latest releases.

@kevwan kevwan closed this as completed Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed Indicates an issue that has been fixed in latest version
Projects
None yet
Development

No branches or pull requests

7 participants