使用 ?SecureJSON
防止 ?json
劫持。如果給定的結(jié)構(gòu)是數(shù)組值,則默認(rèn)預(yù)置 ?"while(1),"
? 到響應(yīng)體。
func main() {
r := gin.Default()
// 你也可以使用自己的 SecureJSON 前綴
// r.SecureJsonPrefix(")]}',\n")
r.GET("/someJSON", func(c *gin.Context) {
names := []string{"lena", "austin", "foo"}
// 將輸出:while(1);["lena","austin","foo"]
c.SecureJSON(http.StatusOK, names)
})
// 監(jiān)聽并在 0.0.0.0:8080 上啟動服務(wù)
r.Run(":8080")
}
更多建議: