22 lines
535 B
Go
22 lines
535 B
Go
|
|
package middlewares
|
||
|
|
|
||
|
|
import (
|
||
|
|
"net/http"
|
||
|
|
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
)
|
||
|
|
|
||
|
|
func Cors() gin.HandlerFunc {
|
||
|
|
return func(c *gin.Context) {
|
||
|
|
c.Header("Access-Control-Allow-Origin", "*") // 可替换为具体域名
|
||
|
|
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
||
|
|
c.Header("Access-Control-Allow-Headers", "Origin, Content-Type, Authorization")
|
||
|
|
c.Header("Access-Control-Allow-Credentials", "true")
|
||
|
|
if c.Request.Method == "OPTIONS" {
|
||
|
|
c.AbortWithStatus(http.StatusNoContent)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
c.Next()
|
||
|
|
}
|
||
|
|
}
|