Gin 使用示例(三十一):SecureJSON


使用 SecureJSON 可以防止 JSON 劫持:

func main() {
	r := gin.Default()
    
	// You can also use your own secure json prefix
	// r.SecureJsonPrefix(")]}',\n")
    
	r.GET("/someJSON", func(c *gin.Context) {
		names := []string{"lena", "austin", "foo"}
    
		// Will output  :   while(1);["lena","austin","foo"]
		c.SecureJSON(http.StatusOK, names)
	})
    
	// Listen and serve on 0.0.0.0:8080
	r.Run(":8080")
} 

如果给定结构是数组的话,默认会添加 "while(1)," 前缀到响应实体:

-w643


点赞 取消点赞 收藏 取消收藏

<< 上一篇: Gin 使用示例(三十):运行多个服务器

>> 下一篇: Gin 使用示例(三十二):下载文件