我使用ruby on rails构建,我想为我的模型自动生成唯一的Base64标记。在我所见过的所有示例中,都是为名称或密码生成令牌。一般情况下,如何生成令牌?
我的模型看起来像这样:
{
"id": 2,
"name": "cookies",
"description": "box 150g",
"logo_url": null,
"token": null
}发布于 2016-08-11 03:43:26
您可以简单地使用SecureRandom#urlsafe_base64生成一个随机的base64安全的URL字符串,并填充token字段。
before_create :generate_token
def generate_token
self.token ||= SecureRandom.urlsafe_base64
endhttps://stackoverflow.com/questions/38882082
复制相似问题