SignedCookie/AddSignedCookie
SignedCookie则会根据初始化Elton时配置的Keys来校验cookie与sig cookie是否符合,符合才返回。AddSignedCookie则根据当前的Cookie以及初化Elton时配置的Keys再生成一个校验cookie(Name为当前Cookie的Name + “.sig”)。
Example
package main
import (
"math/rand"
"net/http"
"strconv"
"github.com/vicanso/elton"
"github.com/vicanso/elton/middleware"
)
func main() {
e := elton.New()
e.SignedKeys = new(elton.RWMutexSignedKeys)
e.SignedKeys.SetKeys([]string{
"secret key",
})
e.Use(middleware.NewDefaultResponder())
e.GET("/", func(c *elton.Context) (err error) {
cookie, _ := c.SignedCookie("jt")
if cookie == nil {
_ = c.AddSignedCookie(&http.Cookie{
Name: "jt",
Value: strconv.Itoa(rand.Int()),
})
}
c.Body = cookie
return
})
err := e.ListenAndServe(":3000")
if err != nil {
panic(err)
}
}