auth.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package middleware
  2. import (
  3. "cold-logistics/common/middleware/handler"
  4. jwt "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth"
  5. "gogs.baozhida.cn/zoie/OAuth-core/sdk/config"
  6. "time"
  7. )
  8. // AuthInit jwt验证new
  9. func AuthInit() (*jwt.GinJWTMiddleware, error) {
  10. timeout := time.Hour
  11. if config.ApplicationConfig.Mode == "dev" {
  12. timeout = time.Duration(876000) * time.Hour
  13. } else {
  14. if config.JwtConfig.Timeout != 0 {
  15. timeout = time.Duration(config.JwtConfig.Timeout) * time.Second
  16. }
  17. }
  18. return jwt.New(&jwt.GinJWTMiddleware{
  19. Realm: "bzd oauth",
  20. Key: []byte(config.JwtConfig.Secret),
  21. Timeout: timeout,
  22. SendCookie: true,
  23. // token 最大刷新时间
  24. MaxRefresh: 2 * time.Hour,
  25. PayloadFunc: handler.PayloadFunc,
  26. IdentityHandler: handler.IdentityHandler,
  27. Authenticator: handler.Authenticator,
  28. Authorizator: handler.Authorizator,
  29. Unauthorized: handler.Unauthorized,
  30. TokenLookup: "header: Authorization, query: token, cookie: jwt",
  31. TokenHeadName: "Bearer",
  32. TimeFunc: time.Now,
  33. })
  34. }