diff --git a/example/sample/test/api_test.go b/example/sample/test/api_test.go index cc6dbf3..c9ef99c 100644 --- a/example/sample/test/api_test.go +++ b/example/sample/test/api_test.go @@ -2,9 +2,12 @@ package test import ( "encoding/json" + "fmt" + "github.com/goflyfox/gtoken/example/sample/test/server" "github.com/goflyfox/gtoken/gtoken" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/net/ghttp" + "os" "testing" ) @@ -17,6 +20,23 @@ var ( Username = "flyFox" ) +func setup() { + fmt.Println("start...") + server.Start() +} + +func teardown() { + server.Stop() + fmt.Println("stop.") +} + +func TestMain(m *testing.M) { + setup() + code := m.Run() + teardown() + os.Exit(code) +} + func TestHello(t *testing.T) { t.Log("visit hello and no auth") if r, e := g.Client().Post(TestURL+"/hello", "username="+Username); e != nil { diff --git a/example/sample/test/server/server.go b/example/sample/test/server/server.go new file mode 100644 index 0000000..7539bc6 --- /dev/null +++ b/example/sample/test/server/server.go @@ -0,0 +1,119 @@ +package server + +import ( + "github.com/goflyfox/gtoken/gtoken" + "github.com/gogf/gf/frame/g" + "github.com/gogf/gf/net/ghttp" + "github.com/gogf/gf/os/glog" +) + +var TestServerName string + +//var TestServerName string = "gtoken" + +var server *ghttp.Server + +func Start() { + glog.Info("########service start...") + + g.Cfg().SetPath("../config") + server = g.Server(TestServerName) + initRouter(server) + + glog.Info("########service finish.") + server.Start() +} + +func Stop() { + server.Shutdown() +} + +var gfToken *gtoken.GfToken +var gfAdminToken *gtoken.GfToken + +/* +统一路由注册 +*/ +func initRouter(s *ghttp.Server) { + // 不认证接口 + s.Group("/", func(group *ghttp.RouterGroup) { + group.Middleware(CORS) + + // 调试路由 + group.ALL("/hello", func(r *ghttp.Request) { + r.Response.WriteJson(gtoken.Succ("hello")) + }) + }) + + // 认证接口 + loginFunc := Login + // 启动gtoken + gfToken = >oken.GfToken{ + ServerName: TestServerName, + LoginPath: "/login", + LoginBeforeFunc: loginFunc, + LogoutPath: "/user/logout", + AuthExcludePaths: g.SliceStr{"/user/info", "/system/user/info"}, // 不拦截路径 /user/info,/system/user/info,/system/user, + MultiLogin: g.Config().GetBool("gToken.MultiLogin"), + } + s.Group("/", func(group *ghttp.RouterGroup) { + group.Middleware(CORS) + gfToken.Middleware(group) + + group.ALL("/system/user", func(r *ghttp.Request) { + r.Response.WriteJson(gtoken.Succ("system user")) + }) + group.ALL("/user/data", func(r *ghttp.Request) { + r.Response.WriteJson(gfToken.GetTokenData(r)) + }) + group.ALL("/user/info", func(r *ghttp.Request) { + r.Response.WriteJson(gtoken.Succ("user info")) + }) + group.ALL("/system/user/info", func(r *ghttp.Request) { + r.Response.WriteJson(gtoken.Succ("system user info")) + }) + }) + + // 启动gtoken + gfAdminToken = >oken.GfToken{ + ServerName: TestServerName, + //Timeout: 10 * 1000, + LoginPath: "/login", + LoginBeforeFunc: loginFunc, + LogoutPath: "/user/logout", + AuthExcludePaths: g.SliceStr{"/admin/user/info", "/admin/system/user/info"}, // 不拦截路径 /user/info,/system/user/info,/system/user, + MultiLogin: g.Config().GetBool("gToken.MultiLogin"), + } + s.Group("/admin", func(group *ghttp.RouterGroup) { + group.Middleware(CORS) + gfAdminToken.Middleware(group) + + group.ALL("/system/user", func(r *ghttp.Request) { + r.Response.WriteJson(gtoken.Succ("system user")) + }) + group.ALL("/user/info", func(r *ghttp.Request) { + r.Response.WriteJson(gtoken.Succ("user info")) + }) + group.ALL("/system/user/info", func(r *ghttp.Request) { + r.Response.WriteJson(gtoken.Succ("system user info")) + }) + }) +} + +func Login(r *ghttp.Request) (string, interface{}) { + username := r.GetString("username") + passwd := r.GetString("passwd") + + if username == "" || passwd == "" { + r.Response.WriteJson(gtoken.Fail("账号或密码错误.")) + r.ExitAll() + } + // 唯一标识,扩展参数user data + return username, "1" +} + +// 跨域 +func CORS(r *ghttp.Request) { + r.Response.CORSDefault() + r.Middleware.Next() +} diff --git a/example/sample1/test/api_test.go b/example/sample1/test/api_test.go index 752be89..f37621c 100644 --- a/example/sample1/test/api_test.go +++ b/example/sample1/test/api_test.go @@ -3,6 +3,7 @@ package test import ( "encoding/json" "fmt" + "github.com/goflyfox/gtoken/example/sample1/test/server1" "github.com/goflyfox/gtoken/gtoken" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/net/ghttp" @@ -20,11 +21,13 @@ var ( ) func setup() { - fmt.Println("Before all tests") + fmt.Println("start...") + server1.Start() } func teardown() { - fmt.Println("After all tests") + server1.Stop() + fmt.Println("stop.") } func TestMain(m *testing.M) { diff --git a/example/sample1/test/server1/server.go b/example/sample1/test/server1/server.go new file mode 100644 index 0000000..e124840 --- /dev/null +++ b/example/sample1/test/server1/server.go @@ -0,0 +1,98 @@ +package server1 + +import ( + "github.com/goflyfox/gtoken/gtoken" + "github.com/gogf/gf/frame/g" + "github.com/gogf/gf/net/ghttp" + "github.com/gogf/gf/os/glog" +) + +var TestServerName string + +//var TestServerName string = "gtoken" + +var server *ghttp.Server + +func Start() { + glog.Info("########service start...") + + g.Cfg().SetPath("../config") + server = g.Server(TestServerName) + initRouter(server) + + glog.Info("########service finish.") + server.Start() +} + +func Stop() { + server.Shutdown() +} + +var gfToken *gtoken.GfToken + +/* +统一路由注册 +*/ +func initRouter(s *ghttp.Server) { + s.Group("/", func(group *ghttp.RouterGroup) { + group.Middleware(CORS) + + // 调试路由 + group.ALL("/hello", func(r *ghttp.Request) { + r.Response.WriteJson(gtoken.Succ("hello")) + }) + // 获取登录扩展属性 + group.ALL("/system/data", func(r *ghttp.Request) { + r.Response.WriteJson(gfToken.GetTokenData(r).Data) + }) + group.ALL("/system/user", func(r *ghttp.Request) { + r.Response.WriteJson(gtoken.Succ("system user")) + }) + group.ALL("/user/info", func(r *ghttp.Request) { + r.Response.WriteJson(gtoken.Succ("user info")) + }) + group.ALL("/system/user/info", func(r *ghttp.Request) { + r.Response.WriteJson(gtoken.Succ("system user info")) + }) + }) + + loginFunc := Login + // 启动gtoken + gfToken = >oken.GfToken{ + ServerName: TestServerName, + //Timeout: 10 * 1000, + CacheMode: g.Cfg().GetInt8("gToken.CacheMode"), + CacheKey: g.Cfg().GetString("gToken.CacheKey"), + Timeout: g.Cfg().GetInt("gToken.Timeout"), + MaxRefresh: g.Cfg().GetInt("gToken.MaxRefresh"), + TokenDelimiter: g.Cfg().GetString("gToken.TokenDelimiter"), + EncryptKey: g.Cfg().GetBytes("gToken.EncryptKey"), + AuthFailMsg: g.Cfg().GetString("gToken.AuthFailMsg"), + MultiLogin: g.Config().GetBool("gToken.MultiLogin"), + LoginPath: "/login", + LoginBeforeFunc: loginFunc, + LogoutPath: "/user/logout", + AuthPaths: g.SliceStr{"/user", "/system"}, // 这里是按照前缀拦截,拦截/user /user/list /user/add ... + AuthExcludePaths: g.SliceStr{"/user/info", "/system/user/info"}, // 不拦截路径 /user/info,/system/user/info,/system/user, + GlobalMiddleware: true, // 开启全局拦截 + } + gfToken.Start() +} + +func Login(r *ghttp.Request) (string, interface{}) { + username := r.GetString("username") + passwd := r.GetString("passwd") + + if username == "" || passwd == "" { + r.Response.WriteJson(gtoken.Fail("账号或密码错误.")) + r.ExitAll() + } + // 唯一标识,扩展参数user data + return username, "1" +} + +// 跨域 +func CORS(r *ghttp.Request) { + r.Response.CORSDefault() + r.Middleware.Next() +}