Skip to content

Commit

Permalink
Merge remote-tracking branch 'github/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangb committed Aug 24, 2021
2 parents 4616035 + 6132d4d commit d14b368
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 2 deletions.
20 changes: 20 additions & 0 deletions example/sample/test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand Down
119 changes: 119 additions & 0 deletions example/sample/test/server/server.go
Original file line number Diff line number Diff line change
@@ -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 = &gtoken.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 = &gtoken.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()
}
7 changes: 5 additions & 2 deletions example/sample1/test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down
98 changes: 98 additions & 0 deletions example/sample1/test/server1/server.go
Original file line number Diff line number Diff line change
@@ -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 = &gtoken.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()
}

0 comments on commit d14b368

Please sign in to comment.