Github:https://github.com/rs/cors
根据框架选择对应的cors包:https://github.com/rs/cors#more-examples。
以Gin为例:
安装包:go get -u github.com/rs/cors/wrapper/gin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
func CorsMiddleware() gin.HandlerFunc { return cors.New(cors.Options{ AllowedOrigins: []string{"*"}, AllowedMethods: []string{ http.MethodHead, http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, }, AllowedHeaders: []string{"*"}, AllowCredentials: true, OptionsPassthrough: true, }) } |
使用步骤
一、创建cors实例: cors.New()
,
二、cors.Options{}
跨域配置。
有哪些配置可以看这里:https://github.com/rs/cors#parameters