Skip to content

Commit

Permalink
fix:跨域中间件为适应移动端兼容性不再特别返回204状态码
Browse files Browse the repository at this point in the history
  • Loading branch information
keepchen committed Aug 9, 2024
1 parent 0c0e2f4 commit 5487e0b
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions http/middleware/withcors.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ func WithCorsOnlyOptions(headers map[string]string) gin.HandlerFunc {

//处理浏览器跨域options探测请求
if c.Request.Method == http.MethodOptions {
c.Writer.WriteHeader(http.StatusNoContent)
//手机端浏览器中返回 204 No Content 状态的跨域请求不被允许,主要是出于安全性和规范性的考虑。
//浏览器的同源策略和 CORS 规范旨在确保跨域请求的透明性和安全性,
//而 204 响应的无内容特性可能导致安全隐患或兼容性问题,因此在跨域环境中可能会被浏览器禁止。
//c.Writer.WriteHeader(http.StatusNoContent)
c.Abort()
return
}
Expand Down Expand Up @@ -68,9 +71,12 @@ func WithCors(headers map[string]string) gin.HandlerFunc {
c.Writer.Header().Set(key, value)
}

if c.Request.Method == http.MethodOptions {
statusCode = http.StatusNoContent
}
//手机端浏览器中返回 204 No Content 状态的跨域请求不被允许,主要是出于安全性和规范性的考虑。
//浏览器的同源策略和 CORS 规范旨在确保跨域请求的透明性和安全性,
//而 204 响应的无内容特性可能导致安全隐患或兼容性问题,因此在跨域环境中可能会被浏览器禁止。
//if c.Request.Method == http.MethodOptions {
// statusCode = http.StatusNoContent
//}

c.Writer.WriteHeader(statusCode)

Expand Down

0 comments on commit 5487e0b

Please sign in to comment.