[golang] json序列化时自动将id加密

栏目: Go · 发布时间: 7年前

内容简介:对于有规则的数据索引,比如用户ID,商品ID,订单编号,很容易第三方直接遍历抓取资源。采用

对于有规则的数据索引,比如用户ID,商品ID,订单编号,很容易第三方直接遍历抓取资源。采用 liamylian/json-hashids 进行加密,可以有效避免这类情况发生。但因为是对等加密,需注意该加密算法并不安全,使用时需注意场景,详情请见 hashids

package main

import(
    "fmt"
    "github.com/liamylian/json-hashids"
    "time"
)

var json = jsonhashids.NewConfigWithHashIDs("abcdefg", 10)

type Book struct {
    Id    int    `json:"id" hashids:"true"` // 这里要给id打上hashids的tag
    Name  string `json:"name"`
}

func main() {
    book := Book {
        Id:          1,
        Name:        "Jane Eyre",
    }
    
    bytes, _ := json.Marshal(book)
    
    // 输出: {"id":"gYEL5rKBnd","name":"Jane Eyre"}
    fmt.Printf("%s", bytes)
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Hibernate

Hibernate

James Elliott / O'Reilly Media, Inc. / 2004-05-10 / USD 24.95

Do you enjoy writing software, except for the database code? Hibernate:A Developer's Notebook is for you. Database experts may enjoy fiddling with SQL, but you don't have to--the rest of the appl......一起来看看 《Hibernate》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具