I'm stuck on getting sessions to work in GoLang. Specifically within Iris.
Calling a route with "Login" will print out a token; then when I call a route with Restricted, the token is empty. Am I doing something wrong or do I misunderstand the concept behind sessions in GoLang?
func Login(c *iris.Context) {
username := c.FormValueString("email")
password := c.FormValueString("password")
test := "unauthorized"
if username == "jon@snow.com" && password == "123abc!@#" {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"name": "Jon Snow",
"exp": time.Now().Add(time.Hour * 72).Unix(),
})
t, err := token.SignedString([]byte("SecretKey"))
if err != nil {
panic(err)
}
c.Session().Set("token", t)
test = c.Session().GetString("token")
}
c.Text(200, test)
}
func Restricted(c *iris.Context) {
tokenString := c.Session().GetString("token")
if tokenString != "" {
c.Text(iris.StatusOK, tokenString)
} else {
c.Text(iris.StatusOK, "no Token")
}
}
您应该先导入会话包,然后首先go get "github.com/kataras/iris/sessions"
然后使用var sess = sessions.New(sessions.Config{Cookie: "cookie-name" })
go get "github.com/kataras/iris/sessions"
var sess = sessions.New(sessions.Config{Cookie: "cookie-name" })
创建会话, 请阅读此文档以查看这项工作如何
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.