Go 将JWT存储在httpOnly cookie中的最佳方法
我已经找到了这个模块,我想用它将JWT令牌(访问和刷新)存储在httpOnly Cookie中,以便在restapi中使用 下面的代码引用自上面提到的官方Github页面-我不确定“值字符串映射”是什么意思:Go 将JWT存储在httpOnly cookie中的最佳方法,go,cookies,jwt,Go,Cookies,Jwt,我已经找到了这个模块,我想用它将JWT令牌(访问和刷新)存储在httpOnly Cookie中,以便在restapi中使用 下面的代码引用自上面提到的官方Github页面-我不确定“值字符串映射”是什么意思: func SetCookieHandler(w http.ResponseWriter, r *http.Request) { value := map[string]string{ "foo": "bar", }
func SetCookieHandler(w http.ResponseWriter, r *http.Request) {
value := map[string]string{
"foo": "bar",
}
if encoded, err := s.Encode("cookie-name", value); err == nil {
cookie := &http.Cookie{
Name: "cookie-name",
Value: encoded,
Path: "/",
Secure: true,
HttpOnly: true,
}
http.SetCookie(w, cookie)
}
}
换句话说:我应该在哪里保存我的代币?它们是指向“值”字符串映射,还是应该是在encode函数之后创建的“cookie”对象中的属性
我也在使用Gin框架,但这不会改变任何事情。SecureCookie唯一做的事情就是将您的令牌值转换为编码字符串。 然后,在将cookie设置为客户端时,可以将其视为令牌的新值
Encode
可以处理任何数据类型,但不一定必须是map[string]字符串
,您可以直接将jwt令牌传递给它
encoded, err := s.Encode("jwt-token", "value.of.jwt.token.here")
在此之后,不能比示例中的键值格式更简单:
cookie := &http.Cookie{
Name: "jwt-token", // <- should be any unique key you want
Value: encoded, // <- the token after encoded by SecureCookie
Path: "/",
Secure: true,
HttpOnly: true,
}
http.SetCookie(w, cookie)
cookie:=&http.cookie{
名称:“jwt令牌”//