This update:
1. gf upgrade to V1.12.1
2. join the global interception support, easy to adjust the execution order of certification and other middleware
// 启动gtoken
gtoken := >oken.GfToken{
LoginPath: "/login",
LoginBeforeFunc: loginFunc,
LogoutPath: "/user/logout",
AuthPaths: g.SliceStr{"/user", "/system"}, // 这里是按照前缀拦截,拦截/user /user/list /user/add ...
GlobalMiddleware: true, // 开启全局拦截,默认关闭
}
gtoken.Start()
gtoken Introduction
Based token plug-gf framework, implemented by the server authentication token authentication; already fully support online authentication token, and support for clustering mode; simple to use, you can be assured;
- Gcache supports stand-alone and cluster gredis mode;
# 配置文件
# 缓存模式 1 gcache 2 gredis
cache-mode = 2
- It supports simple authentication token
- Added caching automatic renewal feature
// 注:通过MaxRefresh,默认当用户第五天访问时,自动再进行五天续期
// 超时时间 默认10天
Timeout int
// 缓存刷新时间 默认为超时时间的一半
MaxRefresh int
- Support Global intercept or intercept path depth, easily customizable interceptors based on individual needs
// 是否是全局认证
GlobalMiddleware bool
- Using simple framework, only login authentication method, and login, logout, the path to the interception;
- github Address: https://github.com/goflyfox/gtoken
- gitee Address: https://gitee.com/goflyfox/gtoken
gtoken advantage
- Jwt effectively avoid the problem server can not quit;
- You can not solve jwt invalid token has been issued;
- Extended User information is still stored on the server, can effectively reduce the transmission space;
- gtoken support single-point applications use memory storage, also supports the use of cluster redis storage;
- Supports caching automatic renewal, and does not require the client to achieve;
Installation Tutorial
- gopath mode:
go get github.com/goflyfox/gtoken
- Or add the use of go.mod:
require github.com/goflyfox/gtoken latest
Instructions for use
Only you need to configure the login path, the path out, blocking the path and log on to achieve parity
// Start gtoken gtoken : = . & Gtoken GfToken { LOGINPATH : " / Login " , LoginBeforeFunc : loginFunc, LogoutPath : " / User / Zimbabwe Logout " , AuthPaths :. G SliceStr { " / User " , " / System " }, // this is in accordance with the prefix interception, interception / the User / the User / List / the User / the Add ... GlobalMiddleware : to true , // open global interception, off by default } gtoken.Start()
Login method implementation
func Login(r *ghttp.Request) (string, interface{}) { username := r.GetPostString("username") passwd := r.GetPostString("passwd") // TODO login verification return username, "" }
Logic test
Api_test.go run tests and view the results; validation logic Description:
- Access to user information, tips do not carry token
- Once logged in, carry normal token access
- Logout successful
- Before carrying token visit, suggesting that the unregistered
--- PASS: Test System User ( 0:00 s) 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
- gf framework https://github.com/gogf/gf