gtoken v1.2.0 release, gf plug the token, token support contract

gtoken

This version mainly joined the token renewals and cache optimized, since gtoken has fully support online authentication token, and support for clustering mode;

Introduction

Based token gf plug frame, achieved by the authentication server authentication token;

  1. Gcache supports stand-alone and cluster gredis mode;
# 配置文件
# 缓存模式 1 gcache 2 gredis
cache-mode = 2
  1. It supports simple authentication token
  2. Added caching automatic renewal feature
// 注:通过MaxRefresh,默认当用户第五天访问时,自动再进行五天续期
// 超时时间 默认10天
Timeout int
// 缓存刷新时间 默认为超时时间的一半
MaxRefresh int
  1. Using simple framework, only login authentication method, and login, logout, the path to the interception;

Installation Tutorial

gopath mode: go get https://github.comgoflyfox/gtoken

Or add the use of go.mod:require github.comgoflyfox/gtoken last

Instructions for use

Only you need to configure the login path, the path out, blocking the path and log on to achieve parity

	// 启动gtoken
	gtoken := &gtoken.GfToken{
		LoginPath:       "/login",
		LoginBeforeFunc: loginFunc,
		LogoutPath:      "/user/logout",
		AuthPaths:       g.SliceStr{"/user/*", "/system/*"},
	}
	gtoken.Start()

Login method implementation

func Login(r *ghttp.Request) (string, interface{}) {
	username := r.GetPostString("username")
	passwd := r.GetPostString("passwd")

	// TODO 进行登录校验

	return username, ""
}

Logic test

Api_test.go run tests and view the results; validation logic Description:

  1. Access to user information, tips do not carry token
  2. Once logged in, carry normal token access
  3. Logout successful
  4. Before carrying token visit, suggesting that the unregistered
--- PASS: TestSystemUser (0.00s)
    api_test.go:43: 1. not login and visit user
    api_test.go:50: {"code":-1,"data":"","msg":"query token fail"}
    api_test.go:63: 2. execute login and visit user
    api_test.go:66: {"code":0,"msg":"success","data":"system user"}
    api_test.go:72: 3. execute logout
    api_test.go:75: {"code":0,"msg":"success","data":"logout success"}
    api_test.go:81: 4. visit user
    api_test.go:86: {"code":-1,"msg":"login timeout or not login","data":""}

thank

  1. gf framework  https://github.com/gogf/gf

Guess you like

Origin www.oschina.net/news/107496/gtoken-1-2-0-released