SecureJSON

使用 SecureJSON 防止 json 劫持。如果给定的结构是数组值,则默认预置 "while(1)," 到响应体。

  1. func main() {
  2. r := gin.Default()
  3. // 你也可以使用自己的 SecureJSON 前缀
  4. // r.SecureJsonPrefix(")]}',\n")
  5. r.GET("/someJSON", func(c *gin.Context) {
  6. names := []string{"lena", "austin", "foo"}
  7. // 将输出:while(1);["lena","austin","foo"]
  8. c.SecureJSON(http.StatusOK, names)
  9. })
  10. // 监听并在 0.0.0.0:8080 上启动服务
  11. r.Run(":8080")
  12. }