package route import ( gs "gitee.ltd/go-oauth2/gin-server" "github.com/gin-gonic/gin" "goweb/core" "goweb/middleware" "net/http" ) func initTestRoute(r *gin.RouterGroup) { group := r.Group("/test", middleware.AuthorizeJWT()) group.GET("/pub", func(context *gin.Context) { core.R(context).OkWithMessage("已登录且无需权限可访问") }) group.GET("/hello", middleware.AuthorityVerify(), func(context *gin.Context) { core.R(context).OkWithMessage("已登录且有权限") }) // 鉴权失败处理函数 errorHandle := func(ctx *gin.Context, err error) { core.R(ctx).FailWithMessageAndCode("请先登录", http.StatusUnauthorized) ctx.Abort() return } group1 := r.Group("/test1", gs.HandleTokenVerify(gs.Config{ErrorHandleFunc: errorHandle})) group1.GET("/pub", func(context *gin.Context) { ti, _ := context.Get(gs.DefaultConfig.TokenKey) core.R(context).OkDetailed(ti, "[oauth2]已登录且无需权限可访问") }) group1.GET("/hello", middleware.AuthorityVerify(), func(context *gin.Context) { ti, _ := context.Get(gs.DefaultConfig.TokenKey) core.R(context).OkDetailed(ti, "[oauth2]已登录且有权限") }) }