auth.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package middleware
  2. import (
  3. "Medical_OAuth/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(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: "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. SaveNewestToken: handler.SaveNewestToken,
  34. GetNewestToken: handler.GetNewestToken,
  35. SetEnterDeptId: handler.SetEnterDeptId,
  36. })
  37. }