Redis密钥“github.com/gin-contrib/sessions“或密钥“github.com/gin-gonic/contrib/sessions“中的gin会话管理出现问题

jhiyze9q  于 2023-10-14  发布在  Go
关注(0)|答案(1)|浏览(160)

我遇到了一些麻烦后,杜松子酒gonic书,这有助于到目前为止顺利移动杜松子酒。当我到了会议的一部分,似乎我击中了一个拦网。
我正在使用:

  • macOS ventura上的macbook pro m1
  • 转到1.21.1
  • 杜松子酒1.9.1
  • gin-contrib/sessions v0.0.5

我从gin-contrib/session开始使用以下代码

  1. //main.go
  2. import (
  3. "context"
  4. "github.com/gin-gonic/contrib/sessions"
  5. //redisStore "github.com/gin-contrib/sessions/redis"
  6. "github.com/gin-gonic/gin"
  7. )
  8. router := gin.Default()
  9. store, _ := sessions.NewRedisStore(10, "tcp", "localhost:6379", "", []byte("secret"))
  10. //store, _ := redisStore.NewStore(10, "tcp", "localhost:6379", "", []byte("secret"))
  11. router.Use(sessions.Sessions("recipes_api", store))
  12. //auth handler
  13. import (
  14. //...
  15. "crypto/sha256"
  16. "github.com/dgrijalva/jwt-go"
  17. //"github.com/gin-contrib/sessions"
  18. sessions2 "github.com/gin-gonic/contrib/sessions"
  19. //...
  20. )
  21. func (handler *AuthHandler) SignInHandler(c *gin.Context) {
  22. //...
  23. if cur.Err() != nil {
  24. c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid username or password"})
  25. return
  26. }
  27. sessionToken := xid.New().String()
  28. session := sessions2.Default(c)
  29. session.Set("username", user.Username)
  30. session.Set("token", sessionToken)
  31. session.Save()
  32. c.JSON(http.StatusOK, gin.H{"message": "User signed in"})
  33. }

上面的代码似乎在session.Default(c)或session2.Default(c)中中断了,所以无论我使用的是来自"github.com/gin-gonic/contrib/sessions"还是来自"github.com/gin-contrib/sessions"的session,它总是找不到键。它抛出如下错误

  1. Key "github.com/gin-gonic/contrib/sessions" does not exist
  2. ~/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:273 (0x10484b67f)
  3. (*Context).MustGet: panic("Key \"" + key + "\" does not exist")
  4. ~/go/pkg/mod/github.com/gin-gonic/[email protected]/sessions/sessions.go:146 (0x10484b61c)
  5. Default: return c.MustGet(DefaultKey).(Session)
  6. ~/Tutorials/distributed-golang-app-gin/handlers/auth.go:59 (0x104995dc7)
  7. (*AuthHandler).SignInHandler: session := sessions2.Default(c)
  8. ~/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:174 (0x104845c5f)
  9. (*Context).Next: c.handlers[c.index](c)
  10. ~/go/pkg/mod/github.com/gin-gonic/[email protected]/recovery.go:102 (0x104845c44)
  11. CustomRecoveryWithWriter.func1: c.Next()
  12. ~/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:174 (0x104844fff)
  13. (*Context).Next: c.handlers[c.index](c)
  14. ~/go/pkg/mod/github.com/gin-gonic/[email protected]/logger.go:240 (0x104844fd0)
  15. LoggerWithConfig.func1: c.Next()
  16. ~/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:174 (0x104844133)
  17. (*Context).Next: c.handlers[c.index](c)
  18. ~/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:620 (0x104843e5c)
  19. (*Engine).handleHTTPRequest: c.Next()
  20. ~/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:576 (0x104843baf)
  21. (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
  22. /opt/homebrew/opt/go/libexec/src/net/http/server.go:2938 (0x1046d95fb)
  23. serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
  24. /opt/homebrew/opt/go/libexec/src/net/http/server.go:2009 (0x1046d6777)
  25. (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
  26. /opt/homebrew/opt/go/libexec/src/runtime/asm_arm64.s:1197 (0x104520b63)
  27. goexit: MOVD R0, R0 // NOP

我在这里做错了一些事情,如果有人能为我提供一些帮助,我会很感激,这样我就可以向前推进了。谢谢

wqlqzqxt

wqlqzqxt1#

问题已解决。我的IDE自动导入了错误的会话引用。我已经手动删除了整个项目中对“github.com/gin-gonic/contrib/sessions“的引用。

相关问题