SignedCookie/AddSignedCookie

SignedCookie则会根据初始化Elton时配置的Keys来校验cookie与sig cookie是否符合,符合才返回。AddSignedCookie则根据当前的Cookie以及初化Elton时配置的Keys再生成一个校验cookie(Name为当前Cookie的Name + “.sig”)。

Example

  1. package main
  2. import (
  3. "math/rand"
  4. "net/http"
  5. "strconv"
  6. "github.com/vicanso/elton"
  7. "github.com/vicanso/elton/middleware"
  8. )
  9. func main() {
  10. e := elton.New()
  11. e.SignedKeys = new(elton.RWMutexSignedKeys)
  12. e.SignedKeys.SetKeys([]string{
  13. "secret key",
  14. })
  15. e.Use(middleware.NewDefaultResponder())
  16. e.GET("/", func(c *elton.Context) (err error) {
  17. cookie, _ := c.SignedCookie("jt")
  18. if cookie == nil {
  19. _ = c.AddSignedCookie(&http.Cookie{
  20. Name: "jt",
  21. Value: strconv.Itoa(rand.Int()),
  22. })
  23. }
  24. c.Body = cookie
  25. return
  26. })
  27. err := e.ListenAndServe(":3000")
  28. if err != nil {
  29. panic(err)
  30. }
  31. }