auth.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package middleware
  2. import (
  3. "time"
  4. "git.cestong.com.cn/cecf/config-center-server/common/middleware/handler"
  5. "github.com/go-admin-team/go-admin-core/sdk/config"
  6. jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
  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(876010) * 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: "test zone",
  20. Key: []byte(config.JwtConfig.Secret),
  21. Timeout: timeout,
  22. MaxRefresh: time.Hour,
  23. PayloadFunc: handler.PayloadFunc,
  24. IdentityHandler: handler.IdentityHandler,
  25. Authenticator: handler.Authenticator,
  26. Authorizator: handler.Authorizator,
  27. Unauthorized: handler.Unauthorized,
  28. TokenLookup: "header: Authorization, query: token, cookie: jwt",
  29. TokenHeadName: "Bearer",
  30. TimeFunc: time.Now,
  31. })
  32. }