在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:gtoken开源软件地址:https://gitee.com/goflyfox/gtoken开源软件介绍:gtoken介绍基于 全面适配GoFrame v2.0.0 ; GoFrame v1.X.X 请使用gtoken v1.4.X相关版本;
gtoken优势
特性说明
# 缓存模式 1 gcache 2 gredis 3 fileCacheCacheMode = 2
// 注:通过MaxRefresh,默认当用户第五天访问时,自动续期// 超时时间 默认10天Timeout int// 缓存刷新时间 默认为超时时间的一半MaxRefresh int
安装教程
分组中间件实现GoFrame官方推荐使用Group方式实现路由和中间件; 使用说明推荐使用分组方式实现 // 启动gtoken gfToken := >oken.GfToken{ LoginPath: "/login", LoginBeforeFunc: loginFunc, LogoutPath: "/user/logout", } s.Group("/admin", 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")) }) ……………… }) 登录方法实现,通过username返回空或者r.ExitAll()\r.Exit()处理认证失败; 特别提示:这里注册的路径严格按照GF group方式,所以注册的路径是 func Login(r *ghttp.Request) (string, interface{}) { username := r.GetPostString("username") passwd := r.GetPostString("passwd") // TODO 进行登录校验 if username == "" || passwd == "" { r.Response.WriteJson(gtoken.Fail("账号或密码错误.")) r.ExitAll() } // 第一个字段是用户唯一标识,第二个字段是扩展参数user data return username, ""} 通过 路径拦截规则 AuthExcludePaths: g.SliceStr{"/user/info", "/system/user/*"}, // 不拦截路径 /user/info,/system/user/info,/system/user,
逻辑测试参考sample项目,先运行main.go,然后可运行api_test.go进行测试并查看结果;验证逻辑说明:
=== RUN TestAdminSystemUser api_admin_test.go:22: 1. not login and visit user api_admin_test.go:29: {"code":-401,"msg":"请求错误或登录超时","data":""} api_admin_test.go:42: 2. execute login and visit user api_admin_test.go:45: {"code":0,"msg":"success","data":"system user"} api_admin_test.go:51: 3. execute logout api_admin_test.go:54: {"code":0,"msg":"success","data":"Logout success"} api_admin_test.go:60: 4. visit user api_admin_test.go:65: {"code":-401,"msg":"请求错误或登录超时","data":""} 全局中间件实现使用说明只需要配置登录路径、登出路径、拦截路径以及登录校验实现即可 // 启动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() 登录方法实现,通过username返回空或者r.ExitAll()\r.Exit()处理认证失败; func Login(r *ghttp.Request) (string, interface{}) { username := r.GetPostString("username") passwd := r.GetPostString("passwd") // TODO 进行登录校验 if username == "" || passwd == "" { r.Response.WriteJson(gtoken.Fail("账号或密码错误.")) r.ExitAll() } // 第一个字段是用户唯一标识,第二个字段是扩展参数user data return username, ""} 通过 路径拦截规则 AuthPaths: g.SliceStr{"/user", "/system"}, // 这里是按照前缀拦截,拦截/user /user/list /user/add ... AuthExcludePaths: g.SliceStr{"/user/info", "/system/user/*"}, // 不拦截路径 /user/info,/system/user/info,/system/user, GlobalMiddleware: true, // 开启全局拦截,默认关闭
逻辑测试参考sample1项目,先运行main.go,然后可运行api_test.go进行测试并查看结果;验证逻辑说明:
=== RUN TestSystemUser api_test.go:43: 1. not login and visit user api_test.go:50: {"code":-401,"msg":"请求错误或登录超时","data":""} 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":-401,"msg":"请求错误或登录超时","data":""} 返回码及配置项
SUCCESS = 0 // 正常FAIL = -1 // 失败ERROR = -99 // 异常UNAUTHORIZED = -401 // 未认证 配置项说明具体可参考
文档https://goframe.org/pages/viewpage.action?pageId=1115974 感谢项目支持
|
请发表评论