-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'github/master'
- Loading branch information
Showing
4 changed files
with
242 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = >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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = >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() | ||
} |