2024-09-27 20:52:14 +08:00
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
//"net/http"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 跨域访问:cross origin resource share
|
|
|
|
|
|
func CrosHandler() gin.HandlerFunc {
|
|
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Origin", "*") // 或者设置为具体的源
|
|
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
|
|
|
|
|
c.Writer.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token, Session, X-Requested-With, Accept, Origin, Host, Connection, Accept-Encoding, Accept-Language, DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Pragma, Token, OpenId, OpenToken")
|
|
|
|
|
|
c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma, FooBar")
|
|
|
|
|
|
c.Writer.Header().Set("Access-Control-Max-Age", "172800")
|
|
|
|
|
|
// 如果需要发送cookies,请确保设置了具体的源而不是*,并将Allow-Credentials设置为true
|
|
|
|
|
|
// c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
|
|
|
|
|
|
|
|
|
|
|
if c.Request.Method == "OPTIONS" {
|
|
|
|
|
|
c.AbortWithStatus(http.StatusOK)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.Next()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|